Class: ChangeSensitiveArray
- Inherits:
-
Object
- Object
- ChangeSensitiveArray
show all
- Defined in:
- lib/yodel/models/core/fields/change_sensitive_array.rb
Overview
Notify the record owning this value whenever the underlying array changes. Records rely on assignment to determine when a value has changed, so mutable objects need to notify the record when they are updated. This is not an exhaustive list of ways to mutate an array, just some common methods used in Yodel already.
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of ChangeSensitiveArray.
8
9
10
11
12
|
# File 'lib/yodel/models/core/fields/change_sensitive_array.rb', line 8
def initialize(record, field, array)
@record = record
@field = field
@array = array
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
76
77
78
79
|
# File 'lib/yodel/models/core/fields/change_sensitive_array.rb', line 76
def method_missing(name, *args, &block)
notify! if name.to_s.end_with?('!')
@array.send(name, *args, &block)
end
|
Instance Attribute Details
#array ⇒ Object
Returns the value of attribute array.
7
8
9
|
# File 'lib/yodel/models/core/fields/change_sensitive_array.rb', line 7
def array
@array
end
|
Instance Method Details
#<<(value) ⇒ Object
41
42
43
44
|
# File 'lib/yodel/models/core/fields/change_sensitive_array.rb', line 41
def <<(value)
notify!
@array << value
end
|
#[]=(index, value) ⇒ Object
51
52
53
54
|
# File 'lib/yodel/models/core/fields/change_sensitive_array.rb', line 51
def []=(index, value)
notify!
@array[index] = value
end
|
#clear ⇒ Object
31
32
33
34
|
# File 'lib/yodel/models/core/fields/change_sensitive_array.rb', line 31
def clear
notify!
@array.clear
end
|
#collect(&block) ⇒ Object
60
61
62
|
# File 'lib/yodel/models/core/fields/change_sensitive_array.rb', line 60
def collect(&block)
@array.collect(&block)
end
|
#count(*item, &block) ⇒ Object
64
65
66
|
# File 'lib/yodel/models/core/fields/change_sensitive_array.rb', line 64
def count(*item, &block)
@array.count(*item, &block)
end
|
#delete(value) ⇒ Object
46
47
48
49
|
# File 'lib/yodel/models/core/fields/change_sensitive_array.rb', line 46
def delete(value)
notify!
@array.delete(value)
end
|
#dup ⇒ Object
Calling changed! on @record will call dup on this array before any mutating operation has been performed. We need to store the original unedited version in typecast (the ‘was’ value), then return this array since the mutating operation is being performed on it. Since dup returns self, the array being operated on will be stored in @record.changed, and be modified by the op.
86
87
88
89
90
|
# File 'lib/yodel/models/core/fields/change_sensitive_array.rb', line 86
def dup
copy = ChangeSensitiveArray.new(@record.dup, @field.dup, @array.dup)
@record.typecast[@field] = copy
self
end
|
#each(&block) ⇒ Object
56
57
58
|
# File 'lib/yodel/models/core/fields/change_sensitive_array.rb', line 56
def each(&block)
@array.each(&block)
end
|
#include?(item) ⇒ Boolean
72
73
74
|
# File 'lib/yodel/models/core/fields/change_sensitive_array.rb', line 72
def include?(item)
@array.include?(item)
end
|
#inspect ⇒ Object
14
15
16
|
# File 'lib/yodel/models/core/fields/change_sensitive_array.rb', line 14
def inspect
@array.inspect
end
|
#pop ⇒ Object
36
37
38
39
|
# File 'lib/yodel/models/core/fields/change_sensitive_array.rb', line 36
def pop
notify!
@array.pop
end
|
#push(value) ⇒ Object
26
27
28
29
|
# File 'lib/yodel/models/core/fields/change_sensitive_array.rb', line 26
def push(value)
notify!
@array.push(value)
end
|
#size ⇒ Object
68
69
70
|
# File 'lib/yodel/models/core/fields/change_sensitive_array.rb', line 68
def size
@array.size
end
|
#to_a ⇒ Object
18
19
20
|
# File 'lib/yodel/models/core/fields/change_sensitive_array.rb', line 18
def to_a
@array
end
|
#to_s ⇒ Object
22
23
24
|
# File 'lib/yodel/models/core/fields/change_sensitive_array.rb', line 22
def to_s
@array.collect(&:to_s).join(', ')
end
|