Class: Hashme::CastedArray

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(property, owner, values = []) ⇒ CastedArray

Returns a new instance of CastedArray.



24
25
26
27
28
29
30
31
32
# File 'lib/hashme/casted_array.rb', line 24

def initialize(property, owner, values = [])
  @_array = []
  @property = property
  if values.respond_to?(:each)
    values.each do |value|
      self.push(value)
    end
  end
end

Instance Attribute Details

#propertyObject (readonly)

Returns the value of attribute property.



14
15
16
# File 'lib/hashme/casted_array.rb', line 14

def property
  @property
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