mirror of
https://github.com/github/rails.git
synced 2026-01-27 23:38:11 -05:00
Extract ActiveSupport::TypedArray class to ensure an array is all of the same type [#673 state:resolved]
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
module ActionView #:nodoc:
|
module ActionView #:nodoc:
|
||||||
class PathSet < Array #:nodoc:
|
class PathSet < ActiveSupport::TypedArray #:nodoc:
|
||||||
def self.type_cast(obj)
|
def self.type_cast(obj)
|
||||||
if obj.is_a?(String)
|
if obj.is_a?(String)
|
||||||
if Base.warn_cache_misses && defined?(Rails) && Rails.initialized?
|
if Base.warn_cache_misses && defined?(Rails) && Rails.initialized?
|
||||||
@@ -25,7 +25,7 @@ module ActionView #:nodoc:
|
|||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :path, :paths
|
attr_reader :path, :paths
|
||||||
delegate :to_s, :to_str, :inspect, :to => :path
|
delegate :to_s, :to_str, :hash, :inspect, :to => :path
|
||||||
|
|
||||||
def initialize(path)
|
def initialize(path)
|
||||||
raise ArgumentError, "path already is a Path class" if path.is_a?(Path)
|
raise ArgumentError, "path already is a Path class" if path.is_a?(Path)
|
||||||
@@ -38,6 +38,10 @@ module ActionView #:nodoc:
|
|||||||
to_str == path.to_str
|
to_str == path.to_str
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def eql?(path)
|
||||||
|
to_str == path.to_str
|
||||||
|
end
|
||||||
|
|
||||||
def [](path)
|
def [](path)
|
||||||
@paths[path]
|
@paths[path]
|
||||||
end
|
end
|
||||||
@@ -67,28 +71,10 @@ module ActionView #:nodoc:
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(*args)
|
|
||||||
super(*args).map! { |obj| self.class.type_cast(obj) }
|
|
||||||
end
|
|
||||||
|
|
||||||
def reload!
|
def reload!
|
||||||
each { |path| path.reload! }
|
each { |path| path.reload! }
|
||||||
end
|
end
|
||||||
|
|
||||||
def <<(obj)
|
|
||||||
super(self.class.type_cast(obj))
|
|
||||||
end
|
|
||||||
|
|
||||||
def push(*objs)
|
|
||||||
delete_paths!(objs)
|
|
||||||
super(*objs.map { |obj| self.class.type_cast(obj) })
|
|
||||||
end
|
|
||||||
|
|
||||||
def unshift(*objs)
|
|
||||||
delete_paths!(objs)
|
|
||||||
super(*objs.map { |obj| self.class.type_cast(obj) })
|
|
||||||
end
|
|
||||||
|
|
||||||
def [](template_path)
|
def [](template_path)
|
||||||
each do |path|
|
each do |path|
|
||||||
if template = path[template_path]
|
if template = path[template_path]
|
||||||
@@ -97,10 +83,5 @@ module ActionView #:nodoc:
|
|||||||
end
|
end
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
def delete_paths!(paths)
|
|
||||||
paths.each { |p1| delete_if { |p2| p1.to_s == p2.to_s } }
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -54,10 +54,7 @@ class ViewLoadPathsTest < Test::Unit::TestCase
|
|||||||
assert_equal [FIXTURE_LOAD_PATH, 'foo', 'bar', 'baz'], @controller.view_paths
|
assert_equal [FIXTURE_LOAD_PATH, 'foo', 'bar', 'baz'], @controller.view_paths
|
||||||
|
|
||||||
@controller.append_view_path(FIXTURE_LOAD_PATH)
|
@controller.append_view_path(FIXTURE_LOAD_PATH)
|
||||||
assert_equal ['foo', 'bar', 'baz', FIXTURE_LOAD_PATH], @controller.view_paths
|
assert_equal [FIXTURE_LOAD_PATH, 'foo', 'bar', 'baz', FIXTURE_LOAD_PATH], @controller.view_paths
|
||||||
|
|
||||||
@controller.append_view_path([FIXTURE_LOAD_PATH])
|
|
||||||
assert_equal ['foo', 'bar', 'baz', FIXTURE_LOAD_PATH], @controller.view_paths
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_controller_prepends_view_path_correctly
|
def test_controller_prepends_view_path_correctly
|
||||||
@@ -68,10 +65,7 @@ class ViewLoadPathsTest < Test::Unit::TestCase
|
|||||||
assert_equal ['foo', 'bar', 'baz', FIXTURE_LOAD_PATH], @controller.view_paths
|
assert_equal ['foo', 'bar', 'baz', FIXTURE_LOAD_PATH], @controller.view_paths
|
||||||
|
|
||||||
@controller.prepend_view_path(FIXTURE_LOAD_PATH)
|
@controller.prepend_view_path(FIXTURE_LOAD_PATH)
|
||||||
assert_equal [FIXTURE_LOAD_PATH, 'foo', 'bar', 'baz'], @controller.view_paths
|
assert_equal [FIXTURE_LOAD_PATH, 'foo', 'bar', 'baz', FIXTURE_LOAD_PATH], @controller.view_paths
|
||||||
|
|
||||||
@controller.prepend_view_path([FIXTURE_LOAD_PATH])
|
|
||||||
assert_equal [FIXTURE_LOAD_PATH, 'foo', 'bar', 'baz'], @controller.view_paths
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_template_appends_view_path_correctly
|
def test_template_appends_view_path_correctly
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ require 'active_support/cache'
|
|||||||
require 'active_support/dependencies'
|
require 'active_support/dependencies'
|
||||||
require 'active_support/deprecation'
|
require 'active_support/deprecation'
|
||||||
|
|
||||||
|
require 'active_support/typed_array'
|
||||||
require 'active_support/ordered_hash'
|
require 'active_support/ordered_hash'
|
||||||
require 'active_support/ordered_options'
|
require 'active_support/ordered_options'
|
||||||
require 'active_support/option_merger'
|
require 'active_support/option_merger'
|
||||||
|
|||||||
31
activesupport/lib/active_support/typed_array.rb
Normal file
31
activesupport/lib/active_support/typed_array.rb
Normal file
@@ -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
|
||||||
51
activesupport/test/typed_array_test.rb
Normal file
51
activesupport/test/typed_array_test.rb
Normal file
@@ -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
|
||||||
Reference in New Issue
Block a user