Class: Compute::Computation

Inherits:
Object
  • Object
show all
Defined in:
lib/compute/computation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#propertyObject

Returns the value of attribute property.



5
6
7
# File 'lib/compute/computation.rb', line 5

def property
  @property
end

Instance Method Details

#dependenciesObject



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

Returns:

  • (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