Class: Procedo::Engine::Intervention::Reading

Inherits:
Object
  • Object
show all
Includes:
Reassignable
Defined in:
lib/procedo/engine/intervention/reading.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Reassignable

#assign!, #reassign, #reassign!

Constructor Details

#initialize(parameter, id, attributes = {}) ⇒ Reading

delegate :depend_on?, to: :reference


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/procedo/engine/intervention/reading.rb', line 14

def initialize(parameter, id, attributes = {})
  unless parameter.is_a?(Procedo::Engine::Intervention::Parameter)
    raise "Invalid parameter: #{parameter.inspect}"
  end
  @parameter = parameter
  @id = id.to_s
  self.indicator_name = attributes[:indicator_name]
  @reference = parameter.reference.reading(name)
  unless reference
    raise 'Cannot find reference for: ' + attributes.inspect
  end
  if measure?
    if attributes[:measure_value_value] && attributes[:measure_value_unit]
      @value = attributes[:measure_value_value].to_d.in(attributes[:measure_value_unit])
    end
  else
    val = attributes["#{datatype}_value".to_sym]
    @value = if %i[point geometry multi_polygon].include?(datatype)
               if val.blank? || val == 'null'
                 Charta.empty_geometry
               else
                 val = Charta.from_geojson(val)
                 val.srid = 4326 if val.srid.zero?
                 val.convert_to(datatype)
               end
             elsif datatype == :integer
               val.to_i
             elsif datatype == :decimal
               val.to_d
             elsif datatype == :boolean
               %w[1 true t ok yes].include?(val.downcase)
             elsif datatype == :choice
               val.blank? ? nil : val.to_sym
             else
               val
             end
  end
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id


8
9
10
# File 'lib/procedo/engine/intervention/reading.rb', line 8

def id
  @id
end

#indicatorObject (readonly)

Returns the value of attribute indicator


8
9
10
# File 'lib/procedo/engine/intervention/reading.rb', line 8

def indicator
  @indicator
end

#parameterObject (readonly)

Returns the value of attribute parameter


8
9
10
# File 'lib/procedo/engine/intervention/reading.rb', line 8

def parameter
  @parameter
end

#referenceObject (readonly)

Returns the value of attribute reference


8
9
10
# File 'lib/procedo/engine/intervention/reading.rb', line 8

def reference
  @reference
end

#valueObject

Returns the value of attribute value


7
8
9
# File 'lib/procedo/engine/intervention/reading.rb', line 7

def value
  @value
end

Instance Method Details

#assign(attribute, value) ⇒ Object


63
64
65
66
# File 'lib/procedo/engine/intervention/reading.rb', line 63

def assign(attribute, value)
  super(attribute, value)
  impact_dependencies!
end

#envObject


86
87
88
# File 'lib/procedo/engine/intervention/reading.rb', line 86

def env
  parameter.env.merge(value: @value)
end

#impact_dependencies!Object


68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/procedo/engine/intervention/reading.rb', line 68

def impact_dependencies!
  reference.computations.each do |computation|
    next unless usable_computation?(computation)
    result = intervention.interpret(computation.expression_tree, env)
    computation.destinations.each do |destination|
      next unless destination == 'population'
      if parameter.quantity_population != result
        parameter.quantity_population = result
      end
    end
  end
end

#indicator_name=(name) ⇒ Object


53
54
55
# File 'lib/procedo/engine/intervention/reading.rb', line 53

def indicator_name=(name)
  @indicator = Nomen::Indicator.find!(name)
end

#measure?Boolean

Returns:

  • (Boolean)

107
108
109
# File 'lib/procedo/engine/intervention/reading.rb', line 107

def measure?
  datatype == :measure?
end

#to_hashObject Also known as: to_attributes


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/procedo/engine/intervention/reading.rb', line 90

def to_hash
  hash = {
    indicator_name: name,
    indicator_datatype: datatype
  }
  if measure?
    hash[:measure_value_value] = @value.to_d.to_s.to_f
    hash[:measure_value_unit] = @value.unit
  elsif %i[point geometry multi_polygon].include?(datatype)
    hash["#{datatype}_value".to_sym] = @value.to_json
  else
    hash["#{datatype}_value".to_sym] = @value
  end
  hash
end

#usable_computation?(computation) ⇒ Boolean

Returns:

  • (Boolean)

81
82
83
84
# File 'lib/procedo/engine/intervention/reading.rb', line 81

def usable_computation?(computation)
  return true unless computation.condition?
  intervention.interpret(computation.condition_tree, env)
end