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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Reading.


11
12
13
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
# File 'lib/procedo/engine/intervention/reading.rb', line 11

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 [: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 == 0
                 val
               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


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

def id
  @id
end

#indicatorObject (readonly)

Returns the value of attribute indicator


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

def indicator
  @indicator
end

#parameterObject (readonly)

Returns the value of attribute parameter


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

def parameter
  @parameter
end

#referenceObject (readonly)

Returns the value of attribute reference


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

def reference
  @reference
end

#valueObject

Returns the value of attribute value


5
6
7
# File 'lib/procedo/engine/intervention/reading.rb', line 5

def value
  @value
end

Instance Method Details

#envObject


77
78
79
# File 'lib/procedo/engine/intervention/reading.rb', line 77

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

#impact_dependencies!Object


59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/procedo/engine/intervention/reading.rb', line 59

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


50
51
52
# File 'lib/procedo/engine/intervention/reading.rb', line 50

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

#measure?Boolean

Returns:

  • (Boolean)

97
98
99
# File 'lib/procedo/engine/intervention/reading.rb', line 97

def measure?
  datatype == :measure?
end

#to_hashObject


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/procedo/engine/intervention/reading.rb', line 81

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 [: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)

72
73
74
75
# File 'lib/procedo/engine/intervention/reading.rb', line 72

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