Class: ReactiveArray
- Inherits:
-
Object
show all
- Defined in:
- lib/reactive_array.rb
Overview
A wrapper around an array that calls #react! on any ‘send’. This enables you to react to certain events or changes to the array.
Constant Summary
collapse
- OPERATORS =
["&", "+", "-", "|", "<=>", "=="]
Instance Method Summary
collapse
Constructor Details
#initialize(*args) {|_self| ... } ⇒ ReactiveArray
Returns a new instance of ReactiveArray.
4
5
6
7
|
# File 'lib/reactive_array.rb', line 4
def initialize(*args)
@array = Array.new(*args)
yield self if block_given?
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
29
30
31
32
33
34
|
# File 'lib/reactive_array.rb', line 29
def method_missing(m, *args, &block)
x = @array.send(m, *args, &block)
react!(m)
x.equal?(@array) ? self : x
end
|
Instance Method Details
#react!(m) ⇒ Object
36
37
38
39
|
# File 'lib/reactive_array.rb', line 36
def react!(m)
raise NotImplemented, "responsibility of subclass"
end
|
#to_a ⇒ Object
Also known as:
to_ary
14
15
16
17
|
# File 'lib/reactive_array.rb', line 14
def to_a
react!(:to_a)
@array
end
|
#to_s ⇒ Object
9
10
11
12
|
# File 'lib/reactive_array.rb', line 9
def to_s
react!(:to_s)
@array.to_s
end
|