Class: Procedo::Procedure::Handler

Inherits:
Field
  • Object
show all
Defined in:
lib/procedo/procedure/handler.rb

Overview

An Handler define a way to quantify a population

Constant Summary collapse

TYPES =
%i[indicator population].freeze

Instance Attribute Summary collapse

Attributes inherited from Field

#name

Instance Method Summary collapse

Constructor Details

#initialize(parameter, name, options = {}) ⇒ Handler

Returns a new instance of Handler.


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/procedo/procedure/handler.rb', line 13

def initialize(parameter, name, options = {})
  super(parameter, name, options)
  @trees = {}.with_indifferent_access
  if population?
    options[:forward] = 'VALUE'
    options[:backward] = 'POPULATION'
  else
    @datatype = options[:datatype]
    @datatype ||= Maybe(Nomen::Indicator.find(options[:indicator])).datatype.or_else(nil)
    raise 'Cant have handler without datatype or indicator' if @datatype.blank?
    self.unit_name = options[:unit] if measure?
    self.indicator_name = options[:indicator] if options[:indicator]
  end
  self.condition = options[:if]
  self.forward = options[:forward]
  self.backward = options[:backward]
  # Define widget of handler (or parameter...)
  @widget = (options[:widget] || (@datatype == :geometry ? :map : :number)).to_sym
end

Instance Attribute Details

#indicatorObject

Returns the value of attribute indicator


11
12
13
# File 'lib/procedo/procedure/handler.rb', line 11

def indicator
  @indicator
end

#parameterObject (readonly)

Returns the value of attribute parameter


11
12
13
# File 'lib/procedo/procedure/handler.rb', line 11

def parameter
  @parameter
end

#unitObject (readonly)

Returns the value of attribute unit


11
12
13
# File 'lib/procedo/procedure/handler.rb', line 11

def unit
  @unit
end

#widgetObject (readonly)

Returns the value of attribute widget


11
12
13
# File 'lib/procedo/procedure/handler.rb', line 11

def widget
  @widget
end

Instance Method Details

#depend_on?(parameter_name) ⇒ Boolean

Returns keys

Returns:

  • (Boolean)

125
126
127
128
129
# File 'lib/procedo/procedure/handler.rb', line 125

def depend_on?(parameter_name)
  condition_with_parameter?(parameter_name) ||
    backward_with_parameter?(parameter_name) ||
    forward_with_parameter?(parameter_name)
end

#dimensionObject


84
85
86
# File 'lib/procedo/procedure/handler.rb', line 84

def dimension
  Nomen::Dimension.find(@unit.dimension)
end

#dimension_nameObject


63
64
65
# File 'lib/procedo/procedure/handler.rb', line 63

def dimension_name
  @unit.dimension.to_sym
end

#human_nameObject

Returns the human name of the handler


94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/procedo/procedure/handler.rb', line 94

def human_name
  default = []
  params = {}
  if @indicator
    params.merge(indicator: indicator.human_name)
    if unit?
      default << :indicator_with_unit
      params[:unit] = unit.symbol
    end
    default << @indicator.human_name
  end
  name.t(params.merge(default: default, scope: 'procedure_handlers'))
end

#indicator_name=(value) ⇒ Object

Sets the indicator name


43
44
45
# File 'lib/procedo/procedure/handler.rb', line 43

def indicator_name=(value)
  self.indicator = Nomen::Indicator.find!(value)
end

#measure?Boolean

Returns:

  • (Boolean)

71
72
73
74
# File 'lib/procedo/procedure/handler.rb', line 71

def measure?
  return false unless @datatype
  @datatype.to_sym == :measure
end

#othersObject

Returns other handlers in the current parameter scope


89
90
91
# File 'lib/procedo/procedure/handler.rb', line 89

def others
  @parameter.handlers.reject { |h| h == self }
end

#population?Boolean

Returns:

  • (Boolean)

67
68
69
# File 'lib/procedo/procedure/handler.rb', line 67

def population?
  @name == :population
end

#tree(name) ⇒ Object


108
109
110
111
112
113
114
115
116
117
118
# File 'lib/procedo/procedure/handler.rb', line 108

def tree(name)
  if name == :backward
    @backward_tree
  elsif name == :forward
    @forward_tree
  elsif name == :condition
    @condition_tree
  else
    raise 'Unknown tree: ' + name.inspect
  end
end

#tree?(name) ⇒ Boolean

Returns:

  • (Boolean)

120
121
122
# File 'lib/procedo/procedure/handler.rb', line 120

def tree?(name)
  tree(name).present?
end

#unit?Boolean

Returns:

  • (Boolean)

76
77
78
# File 'lib/procedo/procedure/handler.rb', line 76

def unit?
  @unit.present?
end

#unit_name=(value) ⇒ Object

Sets the indicator name


48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/procedo/procedure/handler.rb', line 48

def unit_name=(value)
  raise 'Cant assign unit with indicator which is not a measure' unless measure?
  unit = Nomen::Unit.find(value)
  unless unit
    raise Procedo::Errors::InvalidHandler, "Cannot find unit. Got: #{value.inspect}"
  end
  if @indicator
    indicator_dimension = Nomen::Unit.find(indicator.unit).dimension
    unless unit.dimension == indicator_dimension
      raise "Dimension of unit (#{unit.dimension.inspect}) must be identical to indicator's (#{indicator_dimension.inspect}) in #{parameter_name}##{@name} of #{procedure_name}"
    end
  end
  @unit = unit
end