diff options
-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 |