2 # Returns +value+ after yielding +value+ to the block. This simplifies the
3 # process of constructing an object, performing work on the object, and then
4 # returning the object from a method. It is a Ruby-ized realization of the K
5 # combinator, courtesy of Mikael Brockman.
17 # foo # => ['bar', 'baz']
19 # # returning with a local variable
21 # returning values = [] do
27 # foo # => ['bar', 'baz']
29 # # returning with a block argument
31 # returning [] do |values|
37 # foo # => ['bar', 'baz']
43 # An elegant way to factor duplication out of options passed to a series of
44 # method calls. Each method called in the block, with the block variable as
45 # the receiver, will have its options merged with the default +options+ hash
46 # provided. Each method called on the block variable must take an options
47 # hash as its final argument.
49 # with_options :order => 'created_at', :class_name => 'Comment' do |post|
50 # post.has_many :comments, :conditions => ['approved = ?', true], :dependent => :delete_all
51 # post.has_many :unapproved_comments, :conditions => ['approved = ?', false]
52 # post.has_many :all_comments
55 # Can also be used with an explicit receiver:
57 # map.with_options :controller => "people" do |people|
58 # people.connect "/people", :action => "index"
59 # people.connect "/people/:id", :action => "show"
62 def with_options(options
)
63 yield ActiveSupport
::OptionMerger.new(self, options
)
66 # A duck-type assistant method. For example, Active Support extends Date
67 # to define an acts_like_date? method, and extends Time to define
68 # acts_like_time?. As a result, we can do "x.acts_like?(:time)" and
69 # "x.acts_like?(:date)" to do duck-type-safe comparisons, since classes that
70 # we want to act like Time simply need to define an acts_like_time? method.
72 respond_to
? "acts_like_#{duck}?"