Class: CouchRest::Model::CastedArray

Inherits:
Array
  • Object
show all
Includes:
CastedBy, Dirty
Defined in:
lib/couchrest/model/casted_array.rb

Direct Known Subclasses

CollectionOfProxy

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Dirty

#couchrest_attribute_will_change!, #couchrest_parent_will_change!, #use_dirty?

Methods included from CastedBy

#base_doc, #base_doc?

Constructor Details

#initialize(array, property, parent = nil) ⇒ CastedArray

Returns a new instance of CastedArray.



12
13
14
15
16
# File 'lib/couchrest/model/casted_array.rb', line 12

def initialize(array, property, parent = nil)
  self.casted_by_property = property
  self.casted_by = parent unless parent.nil?
  super(array)
end

Instance Attribute Details

#casted_by_propertyObject

Returns the value of attribute casted_by_property.



10
11
12
# File 'lib/couchrest/model/casted_array.rb', line 10

def casted_by_property
  @casted_by_property
end

Instance Method Details

#<<(obj) ⇒ Object

Adding new entries



20
21
22
# File 'lib/couchrest/model/casted_array.rb', line 20

def << obj
  super(instantiate_and_cast(obj))
end

#[]=(index, obj) ⇒ Object



32
33
34
35
36
# File 'lib/couchrest/model/casted_array.rb', line 32

def []= index, obj
  value = instantiate_and_cast(obj, false)
  couchrest_parent_will_change! if use_dirty? && value != self[index]
  super(index, value)
end

#as_couch_jsonObject



75
76
77
# File 'lib/couchrest/model/casted_array.rb', line 75

def as_couch_json
  map{ |v| (v.respond_to?(:as_couch_json) ? v.as_couch_json : v)}
end

#build(*args) ⇒ Object



69
70
71
72
73
# File 'lib/couchrest/model/casted_array.rb', line 69

def build(*args)
  obj = casted_by_property.build(*args)
  self.push(obj)
  obj
end

#clearObject



54
55
56
57
# File 'lib/couchrest/model/casted_array.rb', line 54

def clear
  couchrest_parent_will_change! if use_dirty? && self.length > 0
  super
end

#delete(obj) ⇒ Object



59
60
61
62
# File 'lib/couchrest/model/casted_array.rb', line 59

def delete(obj)
  couchrest_parent_will_change! if use_dirty? && self.length > 0
  super(obj)
end

#delete_at(index) ⇒ Object



64
65
66
67
# File 'lib/couchrest/model/casted_array.rb', line 64

def delete_at(index)
  couchrest_parent_will_change! if use_dirty? && self.length > 0
  super(index)
end

#insert(index, *args) ⇒ Object



38
39
40
41
42
# File 'lib/couchrest/model/casted_array.rb', line 38

def insert index, *args
  values = *args.map{|obj| instantiate_and_cast(obj, false)}
  couchrest_parent_will_change! if use_dirty?
  super(index, *values)
end

#popObject



44
45
46
47
# File 'lib/couchrest/model/casted_array.rb', line 44

def pop
  couchrest_parent_will_change! if use_dirty? && self.length > 0
  super
end

#push(obj) ⇒ Object



24
25
26
# File 'lib/couchrest/model/casted_array.rb', line 24

def push(obj)
  super(instantiate_and_cast(obj))
end

#shiftObject



49
50
51
52
# File 'lib/couchrest/model/casted_array.rb', line 49

def shift
  couchrest_parent_will_change! if use_dirty? && self.length > 0
  super
end

#unshift(obj) ⇒ Object



28
29
30
# File 'lib/couchrest/model/casted_array.rb', line 28

def unshift(obj)
  super(instantiate_and_cast(obj))
end