Class: Glimmer::DataBinding::ModelBinding
- Inherits:
-
Object
- Object
- Glimmer::DataBinding::ModelBinding
show all
- Includes:
- Observable, Observer
- Defined in:
- lib/glimmer/data_binding/model_binding.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Observer
#add_dependent, #compact_args, #dependents, #dependents_for, proc, #register, #registration_for, #registrations, #remove_dependent, #unregister, #unregister_all_observables, #unregister_dependents_with_observable
Methods included from Observable
#inspect
Constructor Details
#initialize(base_model, property_name_expression, binding_options = nil) ⇒ ModelBinding
Returns a new instance of ModelBinding.
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/glimmer/data_binding/model_binding.rb', line 33
def initialize(base_model, property_name_expression, binding_options = nil)
@base_model = base_model
@property_name_expression = property_name_expression
@binding_options = binding_options || Concurrent::Hash.new
if computed?
@computed_model_bindings = Concurrent::Array.new(computed_by.map do |computed_by_property_expression|
self.class.new(base_model, computed_by_property_expression)
end)
end
end
|
Instance Attribute Details
#binding_options ⇒ Object
Returns the value of attribute binding_options.
31
32
33
|
# File 'lib/glimmer/data_binding/model_binding.rb', line 31
def binding_options
@binding_options
end
|
#property_name_expression ⇒ Object
Returns the value of attribute property_name_expression.
31
32
33
|
# File 'lib/glimmer/data_binding/model_binding.rb', line 31
def property_name_expression
@property_name_expression
end
|
Instance Method Details
#add_computed_observers(observer) ⇒ Object
173
174
175
176
177
178
179
|
# File 'lib/glimmer/data_binding/model_binding.rb', line 173
def add_computed_observers(observer)
@computed_model_bindings.each do |computed_model_binding|
observer_registration = computed_observer_for(observer).observe(computed_model_binding, observation_options)
my_registration = observer.registration_for(self)
observer.add_dependent(my_registration => observer_registration)
end
end
|
#add_nested_observers(observer) ⇒ Object
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
# File 'lib/glimmer/data_binding/model_binding.rb', line 181
def add_nested_observers(observer)
nested_property_observers = nested_property_observers_for(observer)
Concurrent::Array.new(nested_models.zip(nested_property_names)).each_with_index do |zip, i|
model, property_name = zip
nested_property_observer = nested_property_observers[property_name]
previous_index = i - 1
if previous_index.negative?
parent_model = self
parent_property_name = nil
parent_observer = observer
else
parent_model = nested_models[previous_index]
parent_property_name = nested_property_names[previous_index]
parent_observer = nested_property_observers[parent_property_name]
end
parent_property_name = nil if parent_property_name.to_s.start_with?('[')
unless model.nil?
observer_registration = property_indexed?(property_name) ? nested_property_observer.observe(model, observation_options) : nested_property_observer.observe(model, property_name, observation_options)
parent_registration = parent_observer.registration_for(parent_model, *[parent_property_name].compact)
parent_observer.add_dependent(parent_registration => observer_registration)
end
end
end
|
#add_observer(observer, extra_options = {}) ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/glimmer/data_binding/model_binding.rb', line 128
def add_observer(observer, = {})
if computed?
add_computed_observers(observer)
elsif nested_property?
add_nested_observers(observer)
else
model_binding_observer = Observer.proc do |new_value|
converted_value = evaluate_property
observer.call(converted_value).tap do
apply_processor(@binding_options[:after_read], converted_value)
end
end
observer_registration = model_binding_observer.observe(model, property_name, observation_options)
my_registration = observer.registration_for(self)
observer.add_dependent(my_registration => observer_registration)
end
end
|
#base_model ⇒ Object
65
66
67
|
# File 'lib/glimmer/data_binding/model_binding.rb', line 65
def base_model
@base_model
end
|
#call(value, *extra_args) ⇒ Object
206
207
208
209
210
|
# File 'lib/glimmer/data_binding/model_binding.rb', line 206
def call(value, *)
return if model.nil?
converted_value = value
invoke_property_writer(model, "#{property_name}=", converted_value) unless converted_value == evaluate_property
end
|
#computed? ⇒ Boolean
97
98
99
|
# File 'lib/glimmer/data_binding/model_binding.rb', line 97
def computed?
!computed_by.empty?
end
|
#computed_by ⇒ Object
101
102
103
|
# File 'lib/glimmer/data_binding/model_binding.rb', line 101
def computed_by
Concurrent::Array.new([@binding_options[:computed_by]].flatten.compact)
end
|
#computed_observer_for(observer) ⇒ Object
160
161
162
163
164
165
166
167
168
169
170
171
|
# File 'lib/glimmer/data_binding/model_binding.rb', line 160
def computed_observer_for(observer)
@computed_observer_collection ||= Concurrent::Hash.new
unless @computed_observer_collection.has_key?(observer)
@computed_observer_collection[observer] = Observer.proc do |new_value|
converted_value = evaluate_property
observer.call(converted_value).tap do
apply_processor(@binding_options[:after_read], converted_value)
end
end
end
@computed_observer_collection[observer]
end
|
#evaluate_options_property ⇒ Object
219
220
221
|
# File 'lib/glimmer/data_binding/model_binding.rb', line 219
def evaluate_options_property
model.send(options_property_name) unless model.nil?
end
|
#evaluate_property ⇒ Object
212
213
214
215
216
217
|
# File 'lib/glimmer/data_binding/model_binding.rb', line 212
def evaluate_property
value = nil
value = invoke_property_reader(model, property_name) unless model.nil?
apply_processor(@binding_options[:before_read], value)
convert_on_read(value)
end
|
#model ⇒ Object
44
45
46
|
# File 'lib/glimmer/data_binding/model_binding.rb', line 44
def model
nested_property? ? nested_model : base_model
end
|
#model_property_names ⇒ Object
Model representing nested property names e.g. property name expression “address.state” gives [:address]
89
90
91
|
# File 'lib/glimmer/data_binding/model_binding.rb', line 89
def model_property_names
Concurrent::Array.new(nested_property_names[0...-1])
end
|
#nested_model ⇒ Object
61
62
63
|
# File 'lib/glimmer/data_binding/model_binding.rb', line 61
def nested_model
nested_models.last
end
|
#nested_models ⇒ Object
e.g. person.address.state returns [person, person.address]
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/glimmer/data_binding/model_binding.rb', line 49
def nested_models
@nested_models = Concurrent::Array.new([base_model])
model_property_names.reduce(base_model) do |reduced_model, nested_model_property_name|
if !reduced_model.nil?
invoke_property_reader(reduced_model, nested_model_property_name).tap do |new_reduced_model|
@nested_models << new_reduced_model
end
end
end
@nested_models
end
|
#nested_property? ⇒ Boolean
93
94
95
|
# File 'lib/glimmer/data_binding/model_binding.rb', line 93
def nested_property?
property_name_expression.match(/[.\[]/)
end
|
#nested_property_name ⇒ Object
Final nested property name e.g. property name expression “address.state” gives :state
83
84
85
|
# File 'lib/glimmer/data_binding/model_binding.rb', line 83
def nested_property_name
nested_property_names.last
end
|
#nested_property_names ⇒ Object
All nested property names e.g. property name expression “address.state” gives [‘address’, ‘state’] If there are any indexed property names, this returns indexes as properties. e.g. property name expression “addresses.state” gives [‘addresses’, ‘[1]’, ‘state’]
77
78
79
|
# File 'lib/glimmer/data_binding/model_binding.rb', line 77
def nested_property_names
@nested_property_names ||= Concurrent::Array.new(property_name_expression.split(/\[|\./).map {|pne| pne.end_with?(']') ? "[#{pne}" : pne })
end
|
#nested_property_observers_for(observer) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/glimmer/data_binding/model_binding.rb', line 109
def nested_property_observers_for(observer)
@nested_property_observers_collection ||= Concurrent::Hash.new
unless @nested_property_observers_collection.has_key?(observer)
@nested_property_observers_collection[observer] = nested_property_names.reduce(Concurrent::Hash.new) do |output, property_name|
output.merge(
property_name => Observer.proc do |new_value|
add_observer(observer)
converted_value = evaluate_property
observer.call(converted_value).tap do
apply_processor(@binding_options[:after_read], converted_value)
end
end
)
end
end
@nested_property_observers_collection[observer]
end
|
#observation_options ⇒ Object
105
106
107
|
# File 'lib/glimmer/data_binding/model_binding.rb', line 105
def observation_options
@binding_options.slice(:recursive)
end
|
#options_property_name ⇒ Object
223
224
225
|
# File 'lib/glimmer/data_binding/model_binding.rb', line 223
def options_property_name
self.property_name + "_options"
end
|
#property_indexed?(property_expression) ⇒ Boolean
227
228
229
|
# File 'lib/glimmer/data_binding/model_binding.rb', line 227
def property_indexed?(property_expression)
property_expression.to_s.start_with?('[')
end
|
#property_name ⇒ Object
69
70
71
|
# File 'lib/glimmer/data_binding/model_binding.rb', line 69
def property_name
nested_property? ? nested_property_name : property_name_expression
end
|
#remove_observer(observer, extra_options = {}) ⇒ Object
146
147
148
149
150
151
152
153
154
155
156
157
158
|
# File 'lib/glimmer/data_binding/model_binding.rb', line 146
def remove_observer(observer, = {})
if computed?
@computed_model_bindings.each do |computed_model_binding|
computed_observer_for(observer).unobserve(computed_model_binding)
end
@computed_observer_collection.delete(observer)
elsif nested_property?
nested_property_observers_for(observer).clear
else
observer.unobserve(model, property_name)
end
end
|