Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 61 lines (54 sloc) 1.301 kb
cdbc880 @neerajdotname adding documentation for OrderedHash and OrderedOptions
neerajdotname authored
1 # Usually key value pairs are handled something like this:
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -exec ...
spastorino authored
2 #
67af97c @r00k Fix incorrect example.
r00k authored
3 # h = {}
cdbc880 @neerajdotname adding documentation for OrderedHash and OrderedOptions
neerajdotname authored
4 # h[:boy] = 'John'
5 # h[:girl] = 'Mary'
e0a0638 @fxn commit review: say clearly that AS::OrderedHash is about insertion order...
fxn authored
6 # h[:boy] # => 'John'
7 # h[:girl] # => 'Mary'
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -exec ...
spastorino authored
8 #
67af97c @r00k Fix incorrect example.
r00k authored
9 # Using <tt>OrderedOptions</tt>, the above code could be reduced to:
cdbc880 @neerajdotname adding documentation for OrderedHash and OrderedOptions
neerajdotname authored
10 #
11 # h = ActiveSupport::OrderedOptions.new
12 # h.boy = 'John'
13 # h.girl = 'Mary'
e0a0638 @fxn commit review: say clearly that AS::OrderedHash is about insertion order...
fxn authored
14 # h.boy # => 'John'
15 # h.girl # => 'Mary'
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -exec ...
spastorino authored
16 #
c08547d @josh Namespace Inflector, Dependencies, OrderedOptions, and TimeZone under Ac...
josh authored
17 module ActiveSupport #:nodoc:
cd641fa @vatrai ordered_options will work if inherited from Hash, remove OrderedHash usa...
vatrai authored
18 class OrderedOptions < Hash
5a51848 @thedarkone Try to use Hash's native #[] for speed.
thedarkone authored
19 alias_method :_get, :[] # preserve the original #[] method
20 protected :_get # make it protected
21
c08547d @josh Namespace Inflector, Dependencies, OrderedOptions, and TimeZone under Ac...
josh authored
22 def []=(key, value)
23 super(key.to_sym, value)
24 end
d31830c @jeremy fix test warnings
jeremy authored
25
c08547d @josh Namespace Inflector, Dependencies, OrderedOptions, and TimeZone under Ac...
josh authored
26 def [](key)
27 super(key.to_sym)
28 end
99307b9 @dhh Added calculations: Base.count, Base.average, Base.sum, Base.minimum, Ba...
dhh authored
29
c08547d @josh Namespace Inflector, Dependencies, OrderedOptions, and TimeZone under Ac...
josh authored
30 def method_missing(name, *args)
bb1813c @semaperepelitsa Replace regexp matching with a simple string manipulation.
semaperepelitsa authored
31 name_string = name.to_s
32 if name_string.chomp!('=')
33 self[name_string] = args.first
c08547d @josh Namespace Inflector, Dependencies, OrderedOptions, and TimeZone under Ac...
josh authored
34 else
35 self[name]
36 end
99307b9 @dhh Added calculations: Base.count, Base.average, Base.sum, Base.minimum, Ba...
dhh authored
37 end
266b80c @chriseppstein OrderedOptions must implement respond_to? if it implements method_missin...
chriseppstein authored
38
3ea70f9 @marcandre Use respond_to_missing? for OrderedOptions
marcandre authored
39 def respond_to_missing?(name, include_private)
266b80c @chriseppstein OrderedOptions must implement respond_to? if it implements method_missin...
chriseppstein authored
40 true
41 end
99307b9 @dhh Added calculations: Base.count, Base.average, Base.sum, Base.minimum, Ba...
dhh authored
42 end
6640903 Move InheritableOptions into ActiveSupport
Carlhuda authored
43
44 class InheritableOptions < OrderedOptions
8cda132 @thedarkone Make InheritableOptions's constructor more flexible.
thedarkone authored
45 def initialize(parent = nil)
5a51848 @thedarkone Try to use Hash's native #[] for speed.
thedarkone authored
46 if parent.kind_of?(OrderedOptions)
47 # use the faster _get when dealing with OrderedOptions
48 super() { |h,k| parent._get(k) }
49 elsif parent
8cda132 @thedarkone Make InheritableOptions's constructor more flexible.
thedarkone authored
50 super() { |h,k| parent[k] }
51 else
52 super()
53 end
6640903 Move InheritableOptions into ActiveSupport
Carlhuda authored
54 end
918dc27 @thedarkone Compile ActionController::Base.config's methods to avoid method_missing ...
thedarkone authored
55
56 def inheritable_copy
57 self.class.new(self)
58 end
6640903 Move InheritableOptions into ActiveSupport
Carlhuda authored
59 end
d31830c @jeremy fix test warnings
jeremy authored
60 end
Something went wrong with that request. Please try again.