Class: Hashme::CastedArray
- Inherits:
-
Object
- Object
- Hashme::CastedArray
- Extended by:
- Forwardable
- Includes:
- Castable
- Defined in:
- lib/hashme/casted_array.rb
Overview
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.
Instance Method Summary collapse
- #<<(obj) ⇒ Object
- #[](index) ⇒ Object
- #[]=(index, obj) ⇒ Object
-
#initialize(owner, property, values = []) ⇒ CastedArray
constructor
A new instance of CastedArray.
- #push(obj) ⇒ Object
- #unshift(obj) ⇒ Object
Constructor Details
#initialize(owner, property, values = []) ⇒ CastedArray
Returns a new instance of CastedArray.
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/hashme/casted_array.rb', line 23 def initialize(owner, property, values = []) @_array = [] self.casted_by = owner self.casted_by_property = property if values.respond_to?(:each) values.each do |value| self.push(value) end end end |
Instance Method Details
#<<(obj) ⇒ Object
34 35 36 |
# File 'lib/hashme/casted_array.rb', line 34 def <<(obj) @_array << instantiate_and_build(obj) end |
#[](index) ⇒ Object
46 47 48 |
# File 'lib/hashme/casted_array.rb', line 46 def [] index @_array[index] end |
#[]=(index, obj) ⇒ Object
50 51 52 |
# File 'lib/hashme/casted_array.rb', line 50 def []= index, obj @_array[index] = instantiate_and_build(obj) end |
#push(obj) ⇒ Object
38 39 40 |
# File 'lib/hashme/casted_array.rb', line 38 def push(obj) @_array.push(instantiate_and_build(obj)) end |
#unshift(obj) ⇒ Object
42 43 44 |
# File 'lib/hashme/casted_array.rb', line 42 def unshift(obj) @_array.unshift(instantiate_and_build(obj)) end |