diff options
author | Joshua Peek <josh@joshpeek.com> | 2008-05-14 12:48:37 -0500 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2008-05-14 12:48:37 -0500 |
commit | 49846f85864f4b59d5593c30ee3c59b584d2975d (patch) | |
tree | 7f746da60755000b4281ce2b2969508c116c7d3b /activesupport/lib/active_support | |
parent | 7708650f73ddb4db300ea2059c60c1d907a4384e (diff) | |
download | rails-49846f85864f4b59d5593c30ee3c59b584d2975d.tar.gz rails-49846f85864f4b59d5593c30ee3c59b584d2975d.tar.bz2 rails-49846f85864f4b59d5593c30ee3c59b584d2975d.zip |
Create a seperate file for ActiveSupport::OrderedHash.
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r-- | activesupport/lib/active_support/ordered_hash.rb | 43 | ||||
-rw-r--r-- | activesupport/lib/active_support/ordered_options.rb | 44 |
2 files changed, 43 insertions, 44 deletions
diff --git a/activesupport/lib/active_support/ordered_hash.rb b/activesupport/lib/active_support/ordered_hash.rb new file mode 100644 index 0000000000..6993621ef9 --- /dev/null +++ b/activesupport/lib/active_support/ordered_hash.rb @@ -0,0 +1,43 @@ +# OrderedHash is namespaced to prevent conflicts with other implementations +module ActiveSupport + # 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 + end + + def [](key) + pair = assoc(key) + pair ? pair.last : nil + end + + def delete(key) + pair = assoc(key) + pair ? array_index = index(pair) : nil + array_index ? delete_at(array_index).last : nil + end + + def keys + collect { |key, value| key } + end + + def values + collect { |key, value| value } + end + + def to_hash + returning({}) do |hash| + each { |array| hash[array[0]] = array[1] } + end + end + end + end +end diff --git a/activesupport/lib/active_support/ordered_options.rb b/activesupport/lib/active_support/ordered_options.rb index 3172f62f0d..306376e9ae 100644 --- a/activesupport/lib/active_support/ordered_options.rb +++ b/activesupport/lib/active_support/ordered_options.rb @@ -1,47 +1,3 @@ -# OrderedHash is namespaced to prevent conflicts with other implementations -module ActiveSupport - # 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 - end - - def [](key) - pair = assoc(key) - pair ? pair.last : nil - end - - def delete(key) - pair = assoc(key) - pair ? array_index = index(pair) : nil - array_index ? delete_at(array_index).last : nil - end - - def keys - collect { |key, value| key } - end - - def values - collect { |key, value| value } - end - - def to_hash - returning({}) do |hash| - each { |array| hash[array[0]] = array[1] } - end - end - end - end -end - class OrderedOptions < ActiveSupport::OrderedHash #:nodoc: def []=(key, value) super(key.to_sym, value) |