lib/hashme/casted_array.rb in hashme-0.2.6 vs lib/hashme/casted_array.rb in hashme-0.3.0
- old
+ new
@@ -1,61 +1,84 @@
require 'forwardable'
module Hashme
- # The Hashme CastedArray is a special object wrapper that allows other Model's
- # or Objects to be stored in an array, but maintain casted ownership.
- #
- # Adding objects will automatically assign the Array's owner, as opposed
- # to the array itself.
- #
- class CastedArray
- extend Forwardable
-
+ # The Hashme CastedArray is a special Array that typecasts each item according to a given property
+ class CastedArray < Array
attr_reader :property
- def_delegators :@_array,
- :to_a, :==, :eql?, :size,
- :first, :last, :at, :length,
- :each, :reject, :empty?, :map, :collect,
- :clear, :pop, :shift, :delete, :delete_at,
- :encode_json, :as_json, :to_json,
- :inspect, :any?
-
- def initialize(property, owner, values = [])
- @_array = []
+ def initialize(property, vals = [])
@property = property
- if values.respond_to?(:each)
- values.each do |value|
- self.push(value)
- end
- end
+ super build_all(vals)
end
- def <<(obj)
- @_array << instantiate_and_build(obj)
+ def <<(val)
+ super build(val)
end
- def push(obj)
- @_array.push(instantiate_and_build(obj))
+ def push(*vals)
+ super *build_all(vals)
end
- def unshift(obj)
- @_array.unshift(instantiate_and_build(obj))
+ alias append push
+
+ def concat(*arrays)
+ super *arrays.map { |array| build_all(array) }
end
-
- def [] index
- @_array[index]
+
+ def insert(index, *vals)
+ super index, *build_all(vals)
end
- def []= index, obj
- @_array[index] = instantiate_and_build(obj)
+ def unshift(*vals)
+ super *build_all(vals)
end
-
+
+ alias prepend unshift
+
+ def replace(array)
+ super build_all(array)
+ end
+
+ def []=(*args)
+ args = args.dup
+ args[-1] = build(args[-1])
+ super *args
+ end
+
+ def fill(*args, &block)
+ if block
+ super *args do |index|
+ val = block.call(index)
+ build(val)
+ end
+ else
+ args = args.dup
+ args[0] = build(args[0])
+ super *args
+ end
+ end
+
+ def collect!(&block)
+ if block
+ super do |element|
+ val = block.call(element)
+ build(val)
+ end
+ else
+ super
+ end
+ end
+
+ alias map! collect!
+
protected
- def instantiate_and_build(obj)
- property.build(self, obj)
+ def build_all(vals)
+ vals.map { |val| build(val) }
end
+ def build(val)
+ property.build(val)
+ end
end
end