forked from mojombo/grit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Conflicts: lib/grit.rb lib/grit/tree.rb
- Loading branch information
Showing
4 changed files
with
29 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,4 +30,4 @@ class << self | |
self.debug = false | ||
|
||
VERSION = '0.7.0' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,31 @@ | ||
## | ||
# Allows attributes to be declared as lazy, meaning that they won't be | ||
# computed until they are asked for. Just mix this module in: | ||
# computed until they are asked for. | ||
# | ||
# class Foo | ||
# include Lazy | ||
# ... | ||
# end | ||
# | ||
# To specify a lazy reader: | ||
# | ||
# lazy_reader :att | ||
# Works by delegating each lazy_reader to a cached lazy_source method. | ||
# | ||
# Then, define a method called __bake__ that computes all your lazy | ||
# attributes: | ||
# class Person | ||
# lazy_reader :eyes | ||
# | ||
# def __bake__ | ||
# @att = ... | ||
# def lazy_source | ||
# OpenStruct.new(:eyes => 2) | ||
# end | ||
# end | ||
# | ||
# If you happen to have already done all the hard work, you can mark an instance | ||
# as already baked by calling: | ||
# >> Person.new.eyes | ||
# => 2 | ||
# | ||
# __baked__ | ||
# | ||
# That's it! (Tom Preston-Werner: rubyisawesome.com) | ||
module Lazy | ||
module ClassMethods | ||
def lazy_reader(*args) | ||
args.each do |arg| | ||
define_method(arg) do | ||
val = instance_variable_get("@#{arg}") | ||
return val if val | ||
self.__prebake__ | ||
instance_variable_get("@#{arg}") | ||
end | ||
def lazy_reader(*args) | ||
args.each do |arg| | ||
ivar = "@#{arg}" | ||
define_method(arg) do | ||
val = instance_variable_get(ivar) | ||
return val if val | ||
instance_variable_set(ivar, (@lazy_source ||= lazy_source).send(arg)) | ||
end | ||
end | ||
end | ||
|
||
def __prebake__ | ||
return if @__baked__ | ||
self.__bake__ | ||
@__baked__ = true | ||
end | ||
|
||
def __baked__ | ||
@__baked__ = true | ||
end | ||
|
||
def self.included(base) | ||
base.extend(ClassMethods) | ||
end | ||
end | ||
end | ||
|
||
Object.extend Lazy unless Object.ancestors.include? Lazy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters