diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2005-09-30 06:51:42 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2005-09-30 06:51:42 +0000 |
commit | 9f146ea0ac904252b16be1c4017604aed0c383f7 (patch) | |
tree | 3df5a66f95796d53ecedc982b56e7f50edc55f12 /activesupport | |
parent | 3f1aad8ac958419c11703140e697a4dfc245b161 (diff) | |
download | rails-9f146ea0ac904252b16be1c4017604aed0c383f7.tar.gz rails-9f146ea0ac904252b16be1c4017604aed0c383f7.tar.bz2 rails-9f146ea0ac904252b16be1c4017604aed0c383f7.zip |
Added OrderedOptions array/hash for use with initializer
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2419 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/ordered_options.rb | 23 | ||||
-rw-r--r-- | activesupport/test/ordered_options_test.rb | 23 |
2 files changed, 46 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/ordered_options.rb b/activesupport/lib/active_support/ordered_options.rb new file mode 100644 index 0000000000..3952ba41f2 --- /dev/null +++ b/activesupport/lib/active_support/ordered_options.rb @@ -0,0 +1,23 @@ +class OrderedOptions < Array + def []=(key, value) + key = key.to_sym + + if pair = find_pair(key) + pair.pop + pair << value + else + self << [key, value] + end + end + + def [](key) + pair = find_pair(key.to_sym) + pair ? pair.last : nil + end + + private + def find_pair(key) + self.each { |i| return i if i.first == key } + return false + end +end
\ No newline at end of file diff --git a/activesupport/test/ordered_options_test.rb b/activesupport/test/ordered_options_test.rb new file mode 100644 index 0000000000..c47e945c24 --- /dev/null +++ b/activesupport/test/ordered_options_test.rb @@ -0,0 +1,23 @@ +require 'test/unit' + +require File.dirname(__FILE__) + '/../lib/active_support/ordered_options' + +class OrderedOptionsTest < Test::Unit::TestCase + def test_usage + a = OrderedOptions.new + + assert_nil a[:not_set] + + a[:allow_concurreny] = true + assert_equal 1, a.size + assert a[:allow_concurreny] + + a[:allow_concurreny] = false + assert_equal 1, a.size + assert !a[:allow_concurreny] + + a["else_where"] = 56 + assert_equal 2, a.size + assert_equal 56, a[:else_where] + end +end |