Froze rails gems
[depot.git] / vendor / rails / activesupport / lib / active_support / core_ext / object / misc.rb
1 class Object
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.
6 #
7 # ==== Examples
8 #
9 # # Without returning
10 # def foo
11 # values = []
12 # values << "bar"
13 # values << "baz"
14 # return values
15 # end
16 #
17 # foo # => ['bar', 'baz']
18 #
19 # # returning with a local variable
20 # def foo
21 # returning values = [] do
22 # values << 'bar'
23 # values << 'baz'
24 # end
25 # end
26 #
27 # foo # => ['bar', 'baz']
28 #
29 # # returning with a block argument
30 # def foo
31 # returning [] do |values|
32 # values << 'bar'
33 # values << 'baz'
34 # end
35 # end
36 #
37 # foo # => ['bar', 'baz']
38 def returning(value)
39 yield(value)
40 value
41 end
42
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.
48 #
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
53 # end
54 #
55 # Can also be used with an explicit receiver:
56 #
57 # map.with_options :controller => "people" do |people|
58 # people.connect "/people", :action => "index"
59 # people.connect "/people/:id", :action => "show"
60 # end
61 #
62 def with_options(options)
63 yield ActiveSupport::OptionMerger.new(self, options)
64 end
65
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.
71 def acts_like?(duck)
72 respond_to? "acts_like_#{duck}?"
73 end
74 end