Class: Indicatus

Inherits:
Object
  • Object
show all
Defined in:
app/models/indicatus.rb

Instance Method Summary collapse

Constructor Details

#initialize(variable_indicator, operation) ⇒ Indicatus

Returns a new instance of Indicatus.


2
3
4
5
6
# File 'app/models/indicatus.rb', line 2

def initialize(variable_indicator, operation)
  @varicator = variable_indicator
  @operation = operation
  @intervention = @operation.intervention
end

Instance Method Details

#actorObject


24
25
26
27
28
29
30
# File 'app/models/indicatus.rb', line 24

def actor
  if cast = @intervention.product_parameters.find_by(reference_name: @varicator.stakeholder.name)
    return cast.actor
  else
    return nil
  end
end

#computed_value(_options = {}) ⇒ Object


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/indicatus.rb', line 32

def computed_value(_options = {})
  if @varicator.value?
    expr = @varicator.value.strip
    # Computed value
    if expr =~ /\A(\w+)?[[:space:]]*\:[[:space:]]*\w+\z/
      computation, var = expr.split(/[[:space:]]*\:[[:space:]]*/)[0..1]
      computation = (computation.blank? ? :same_as : computation.underscore.to_sym)
      source_var = @varicator.procedure.variables[var]
      source_cast = @intervention.product_parameters.find_by(reference_name: source_var.name.to_s)
      source_actor = source_cast.actor
      cast  = @intervention.product_parameters.find_by(reference_name: @varicator.stakeholder.name)
      actor = cast.actor
      if computation == :superficial_count
        if source_actor.indicators_list.include?(:shape)
          if actor.indicators_list.include?(:net_surface_area)
            if source_cast.shape
              whole = Charta.new_geometry(source_cast.shape).area
            elsif whole = source_actor.shape_area(at: @operation.started_at)
            #
            else
              raise StandardError, "Cannot compute superficial count if with a source product without shape reading (#{source_cast.shape.inspect})"
            end
            return 0 if whole.zero?
            individual = actor.net_surface_area(@operation.stopped_at, gathering: false, default: false)
            if individual.nil?
              raise StandardError, 'Cannot compute superficial count with a product with null net_surface_area indicator. Maybe indicator is variable and not already read.'
            end
            return (whole.to_f(:square_meter) / individual.to_f(:square_meter))
          else
            raise StandardError, 'Cannot compute superficial count with a product without net_surface_area indicator'
          end
        else
          raise StandardError, "Cannot compute superficial count with a source product without shape indicator #{source_actor.nature.inspect}"
        end
      else # if computation == :same_as
        if source_actor.indicators.include?(@varicator.indicator)
          return source_actor.get!(@varicator.indicator, @operation.started_at)
        else
          raise StandardError, "Cannot get #{@varicator.indicator_name} from a source product without this indicator #{source_actor.nature.inspect}."
        end
      end
    else
      # Direct value
      return expr
    end
  else
    raise NotImplementedError
  end
end

#indicatorObject


12
13
14
# File 'app/models/indicatus.rb', line 12

def indicator
  @varicator.indicator
end

#nameObject


16
17
18
# File 'app/models/indicatus.rb', line 16

def name
  @varicator.indicator_name
end

#readingObject


20
21
22
# File 'app/models/indicatus.rb', line 20

def reading
  Reading.new(@varicator.indicator_name, computed_value)
end

#value?Boolean

Returns:

  • (Boolean)

8
9
10
# File 'app/models/indicatus.rb', line 8

def value?
  @varicator.value?
end