Class: Compute::Computation
- Inherits:
-
Object
- Object
- Compute::Computation
- Defined in:
- lib/compute/computation.rb
Instance Attribute Summary collapse
-
#property ⇒ Object
Returns the value of attribute property.
Instance Method Summary collapse
- #dependencies ⇒ Object
- #execute(record) ⇒ Object
-
#initialize(model, property, &block) ⇒ Computation
constructor
A new instance of Computation.
- #needs_update?(changed_properties) ⇒ Boolean
Constructor Details
#initialize(model, property, &block) ⇒ Computation
Returns a new instance of Computation.
7 8 9 10 11 |
# File 'lib/compute/computation.rb', line 7 def initialize(model, property, &block) @model = model @property = property @proc = Proc.new(&block) end |
Instance Attribute Details
#property ⇒ Object
Returns the value of attribute property.
5 6 7 |
# File 'lib/compute/computation.rb', line 5 def property @property end |
Instance Method Details
#dependencies ⇒ Object
13 14 15 |
# File 'lib/compute/computation.rb', line 13 def dependencies @dependencies ||= calculate_dependencies(@proc) end |
#execute(record) ⇒ Object
22 23 24 25 26 |
# File 'lib/compute/computation.rb', line 22 def execute(record) source_values = source_values(record) destination_result = record.instance_exec(*source_values, &@proc) record.send(@property.to_s + '=', destination_result) end |
#needs_update?(changed_properties) ⇒ Boolean
17 18 19 20 |
# File 'lib/compute/computation.rb', line 17 def needs_update?(changed_properties) common_changes = changed_properties.map(&:to_sym) & @dependencies common_changes.count > 0 end |