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 =
[:indicator, :population].freeze

Instance Attribute Summary collapse

Attributes inherited from Field

#name

Instance Method Summary collapse

Methods inherited from Field

code_trees, count_variables, dependent_parameters, detect, detect_environment_variable, detect_parameter, each, parse!

Constructor Details

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

Returns a new instance of Handler.


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

def initialize(parameter, name, options = {})
  super(parameter, name, options)
  @trees = {}.with_indifferent_access
  options[:type] ||= :population if @name == :population
  @type = options[:type] || :indicator
  raise 'Invalid type: ' + @type.inspect unless TYPES.include?(@type)
  if indicator?
    self.indicator_name = options[:indicator] || @name
    self.unit_name = options[:unit] if measure?
  elsif population?
    options[:forward] = 'VALUE'
    options[:backward] = 'POPULATION'
    # options[:if] ||= 'PRODUCT?'
  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


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

def indicator
  @indicator
end

#parameterObject (readonly)

Returns the value of attribute parameter


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

def parameter
  @parameter
end

#unitObject (readonly)

Returns the value of attribute unit


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

def unit
  @unit
end

#widgetObject (readonly)

Returns the value of attribute widget


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

def widget
  @widget
end

Instance Method Details

#datatypeObject


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

def datatype
  population? ? :decimal : indicator.datatype
end

#depend_on?(parameter_name) ⇒ Boolean

Returns keys

Returns:

  • (Boolean)

130
131
132
133
134
# File 'lib/procedo/procedure/handler.rb', line 130

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

#dependent_parametersObject


136
137
138
139
140
141
# File 'lib/procedo/procedure/handler.rb', line 136

def dependent_parameters
  parameters = handler.condition_dependent_parameters
  parameters += handler.backward_dependent_parameters
  parameters += handler.forward_dependent_parameters
  parameters.uniq
end

#dimensionObject


92
93
94
# File 'lib/procedo/procedure/handler.rb', line 92

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

#dimension_nameObject


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

def dimension_name
  @unit.dimension.to_sym
end

#human_nameObject

Returns the human name of the handler


102
103
104
105
106
107
108
109
110
111
# File 'lib/procedo/procedure/handler.rb', line 102

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

#indicator?Boolean

Returns:

  • (Boolean)

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

def indicator?
  @type == :indicator
end

#indicator_name=(value) ⇒ Object

Sets the indicator name


45
46
47
# File 'lib/procedo/procedure/handler.rb', line 45

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

#measure?Boolean

Returns:

  • (Boolean)

80
81
82
# File 'lib/procedo/procedure/handler.rb', line 80

def measure?
  datatype == :measure
end

#othersObject

Returns other handlers in the current parameter scope


97
98
99
# File 'lib/procedo/procedure/handler.rb', line 97

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

#population?Boolean

Returns:

  • (Boolean)

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

def population?
  @type == :population
end

#tree(name) ⇒ Object


113
114
115
116
117
118
119
120
121
122
123
# File 'lib/procedo/procedure/handler.rb', line 113

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)

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

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

#unit?Boolean

Returns:

  • (Boolean)

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

def unit?
  !@unit.blank?
end

#unit_name=(value) ⇒ Object

Sets the indicator name


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

def unit_name=(value)
  raise 'Cant assign unit without indicator' unless indicator
  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
  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
  @unit = unit
end