Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/branches/1-2-stable/activesupport/lib/active_support/ordered_options.rb

Revision 4817, 0.8 kB (checked in by bitsweat, 2 years ago)

Use Array#assoc in ActiveSupport::OrderedHash.

Line 
1 # OrderedHash is namespaced to prevent conflicts with other implementations
2 module ActiveSupport
3   class OrderedHash < Array #:nodoc:
4     def []=(key, value)
5       if pair = assoc(key)
6         pair.pop
7         pair << value
8       else
9         self << [key, value]
10       end
11     end
12
13     def [](key)
14       pair = assoc(key)
15       pair ? pair.last : nil
16     end
17
18     def keys
19       collect { |key, value| key }
20     end
21
22     def values
23       collect { |key, value| value }
24     end
25   end
26 end
27
28 class OrderedOptions < ActiveSupport::OrderedHash #:nodoc:
29   def []=(key, value)
30     super(key.to_sym, value)
31   end
32
33   def [](key)
34     super(key.to_sym)
35   end
36
37   def method_missing(name, *args)
38     if name.to_s =~ /(.*)=$/
39       self[$1.to_sym] = args.first
40     else
41       self[name]
42     end
43   end
44 end
Note: See TracBrowser for help on using the browser.