2006-04-30 16:36:37 -04:00
|
|
|
# OrderedHash is namespaced to prevent conflicts with other implementations
|
2006-05-31 18:43:53 -04:00
|
|
|
module ActiveSupport
|
2007-10-09 03:48:59 -04:00
|
|
|
# Hash is ordered in Ruby 1.9!
|
|
|
|
if RUBY_VERSION >= '1.9'
|
|
|
|
OrderedHash = ::Hash
|
|
|
|
else
|
|
|
|
class OrderedHash < Array #:nodoc:
|
|
|
|
def []=(key, value)
|
|
|
|
if pair = assoc(key)
|
|
|
|
pair.pop
|
|
|
|
pair << value
|
|
|
|
else
|
|
|
|
self << [key, value]
|
|
|
|
end
|
2006-05-31 18:43:53 -04:00
|
|
|
end
|
2005-09-30 02:51:42 -04:00
|
|
|
|
2007-10-09 03:48:59 -04:00
|
|
|
def [](key)
|
|
|
|
pair = assoc(key)
|
|
|
|
pair ? pair.last : nil
|
|
|
|
end
|
2008-05-05 11:03:11 -04:00
|
|
|
|
|
|
|
def delete(key)
|
|
|
|
pair = assoc(key)
|
|
|
|
pair ? array_index = index(pair) : nil
|
|
|
|
array_index ? delete_at(array_index).last : nil
|
|
|
|
end
|
2005-09-30 03:14:37 -04:00
|
|
|
|
2007-10-09 03:48:59 -04:00
|
|
|
def keys
|
|
|
|
collect { |key, value| key }
|
|
|
|
end
|
2006-06-02 13:28:15 -04:00
|
|
|
|
2007-10-09 03:48:59 -04:00
|
|
|
def values
|
|
|
|
collect { |key, value| value }
|
|
|
|
end
|
2008-03-02 22:31:57 -05:00
|
|
|
|
|
|
|
def to_hash
|
|
|
|
returning({}) do |hash|
|
|
|
|
each { |array| hash[array[0]] = array[1] }
|
|
|
|
end
|
|
|
|
end
|
2005-09-30 02:51:42 -04:00
|
|
|
end
|
2006-05-31 18:43:53 -04:00
|
|
|
end
|
2006-02-25 18:06:04 -05:00
|
|
|
end
|
|
|
|
|
2006-04-30 16:36:37 -04:00
|
|
|
class OrderedOptions < ActiveSupport::OrderedHash #:nodoc:
|
2006-02-25 18:06:04 -05:00
|
|
|
def []=(key, value)
|
|
|
|
super(key.to_sym, value)
|
|
|
|
end
|
2006-05-31 18:43:53 -04:00
|
|
|
|
2006-02-25 18:06:04 -05:00
|
|
|
def [](key)
|
|
|
|
super(key.to_sym)
|
|
|
|
end
|
|
|
|
|
|
|
|
def method_missing(name, *args)
|
|
|
|
if name.to_s =~ /(.*)=$/
|
|
|
|
self[$1.to_sym] = args.first
|
|
|
|
else
|
|
|
|
self[name]
|
|
|
|
end
|
|
|
|
end
|
2006-05-31 18:43:53 -04:00
|
|
|
end
|