Skip to content

Commit

Permalink
Simpler example for deferred.
Browse files Browse the repository at this point in the history
RestClient is a poor example for deferred, as deferred is executed on every read, meaning you're going to get a new RestClient instance on every single read of the value, which seems not like something you want to recommend.
  • Loading branch information
jrochkind committed Jan 4, 2012
1 parent e220f27 commit 3532ed2
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions README.md
Expand Up @@ -103,21 +103,21 @@ hash or a struct:
Any configuration value of class `Confstruct::Deferred` will be evaluated on access, allowing you to
define read-only, dynamic configuration attributes

config[:github][:client] = Confstruct::Deferred.new { |c| RestClient::Resource.new(c.url) }
=> #<Proc:0x00000001035eb240>
config.github.client
=> #<RestClient::Resource:0x1035e3b30 @options={}, @url="http://www.github.com/mbklein/confstruct", @block=nil>
config.github.url = 'http://www.github.com/somefork/other-project'
=> "http://www.github.com/somefork/other-project"
config.github.client
=> #<RestClient::Resource:0x1035d5bc0 @options={}, @url="http://www.github.com/somefork/other-project", @block=nil>
config.app_name = "iWidgetCloud"
config.msgs.welcome = Confstruct::Deferred.new {|c| "Welcome to #{c.app_name}!"}
config.msgs.welcome
=> "Welcome to iWidgetCloud!"
config.app_name = "Enterprisey-Webscale"
=> "Enterprisey-Webscale"
config.welcome_msg
=> "Welcome to Enterprisey-Webscale"
As a convenience, `Confstruct.deferred(&block)` and `Confstruct::HashWithStructAccess#deferred!(&block)`
act like `lambda`, making the following two assignments equivalent to the above:
will create a Confstruct::Deferred for you, making the following two assignments equivalent to the above:

config.github.client = Confstruct.deferred { |c| RestClient::Resource.new(c.url) }
config.github do
client deferred! { |c| RestClient::Resource.new(c.url) }
config.welcome_msg = Confstruct.deferred { |c| "Welcome to #{c.app_name}!" }
config do
welcome_msg deferred! { |c| RestClient::Resource.new(c.url) }
end

`push!` and `pop!` methods allow you to temporarily override some or all of your configuration values. This can be
Expand Down

0 comments on commit 3532ed2

Please sign in to comment.