Permalink
Browse files

Added OrderedOptions array/hash for use with initializer

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2419 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 3f1aad8 commit 9f146ea0ac904252b16be1c4017604aed0c383f7 @dhh dhh committed Sep 30, 2005
Showing with 46 additions and 0 deletions.
  1. +23 −0 activesupport/lib/active_support/ordered_options.rb
  2. +23 −0 activesupport/test/ordered_options_test.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
@@ -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

0 comments on commit 9f146ea

Please sign in to comment.