diff options
author | Joshua Peek <josh@joshpeek.com> | 2008-07-22 11:12:16 -0500 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2008-07-22 11:12:16 -0500 |
commit | 2681685450631238511cfc3c2f0fa044c1f8033a (patch) | |
tree | 2414319c504d25ece84a8cfdce5f2f6c10b13fc8 /activesupport | |
parent | bc5896e708bf8070835bebe61de03b701fa5e6f7 (diff) | |
download | rails-2681685450631238511cfc3c2f0fa044c1f8033a.tar.gz rails-2681685450631238511cfc3c2f0fa044c1f8033a.tar.bz2 rails-2681685450631238511cfc3c2f0fa044c1f8033a.zip |
Extract ActiveSupport::TypedArray class to ensure an array is all of the same type [#673 state:resolved]
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support.rb | 1 | ||||
-rw-r--r-- | activesupport/lib/active_support/typed_array.rb | 31 | ||||
-rw-r--r-- | activesupport/test/typed_array_test.rb | 51 |
3 files changed, 83 insertions, 0 deletions
diff --git a/activesupport/lib/active_support.rb b/activesupport/lib/active_support.rb index 1df911a3f2..51067e910e 100644 --- a/activesupport/lib/active_support.rb +++ b/activesupport/lib/active_support.rb @@ -39,6 +39,7 @@ require 'active_support/cache' require 'active_support/dependencies' require 'active_support/deprecation' +require 'active_support/typed_array' require 'active_support/ordered_hash' require 'active_support/ordered_options' require 'active_support/option_merger' diff --git a/activesupport/lib/active_support/typed_array.rb b/activesupport/lib/active_support/typed_array.rb new file mode 100644 index 0000000000..1a4d8a8faf --- /dev/null +++ b/activesupport/lib/active_support/typed_array.rb @@ -0,0 +1,31 @@ +module ActiveSupport + class TypedArray < Array + def self.type_cast(obj) + obj + end + + def initialize(*args) + super(*args).map! { |obj| self.class.type_cast(obj) } + end + + def <<(obj) + super(self.class.type_cast(obj)) + end + + def concat(array) + super(array.map! { |obj| self.class.type_cast(obj) }) + end + + def insert(index, obj) + super(index, self.class.type_cast(obj)) + end + + def push(*objs) + super(*objs.map { |obj| self.class.type_cast(obj) }) + end + + def unshift(*objs) + super(*objs.map { |obj| self.class.type_cast(obj) }) + end + end +end diff --git a/activesupport/test/typed_array_test.rb b/activesupport/test/typed_array_test.rb new file mode 100644 index 0000000000..023f3a1b84 --- /dev/null +++ b/activesupport/test/typed_array_test.rb @@ -0,0 +1,51 @@ +require 'abstract_unit' + +class TypedArrayTest < Test::Unit::TestCase + class StringArray < ActiveSupport::TypedArray + def self.type_cast(obj) + obj.to_s + end + end + + def setup + @array = StringArray.new + end + + def test_string_array_initialize + assert_equal ["1", "2", "3"], StringArray.new([1, "2", :"3"]) + end + + def test_string_array_append + @array << 1 + @array << "2" + @array << :"3" + assert_equal ["1", "2", "3"], @array + end + + def test_string_array_concat + @array.concat([1, "2"]) + @array.concat([:"3"]) + assert_equal ["1", "2", "3"], @array + end + + def test_string_array_insert + @array.insert(0, 1) + @array.insert(1, "2") + @array.insert(2, :"3") + assert_equal ["1", "2", "3"], @array + end + + def test_string_array_push + @array.push(1) + @array.push("2") + @array.push(:"3") + assert_equal ["1", "2", "3"], @array + end + + def test_string_array_unshift + @array.unshift(:"3") + @array.unshift("2") + @array.unshift(1) + assert_equal ["1", "2", "3"], @array + end +end |