Class: Procedo::Engine::Intervention::GroupParameter

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

Constant Summary collapse

CHILDREN_PARAM_NAMES =
[:doers_attributes, :inputs_attributes,
:outputs_attributes, :targets_attributes,
:tools_attributes, :group_parameters_attributes].freeze

Instance Attribute Summary

Attributes inherited from Parameter

#group, #id, #intervention, #name, #reference, #type

Instance Method Summary collapse

Methods inherited from Parameter

#env, #impact, #impact_dependencies!, #param_name, #root?

Constructor Details

#initialize(intervention, id, attributes = {}) ⇒ GroupParameter

Returns a new instance of GroupParameter.


16
17
18
19
# File 'lib/procedo/engine/intervention/group_parameter.rb', line 16

def initialize(intervention, id, attributes = {})
  super(intervention, id, attributes)
  @members = {}.with_indifferent_access
end

Instance Method Details

#add(param_name, id, attributes = {}) ⇒ Object

Builds and adds a parameter of any type in members


82
83
84
85
86
87
88
89
90
91
# File 'lib/procedo/engine/intervention/group_parameter.rb', line 82

def add(param_name, id, attributes = {})
  model_name = param_name.to_s.gsub(/_attributes$/, '').singularize
  class_name = 'Procedo::Engine::Intervention::' + model_name.camelize
  parameter = class_name.constantize.new(self, id, attributes)
  add_parameter(parameter)
  if parameter.is_a?(Procedo::Engine::Intervention::GroupParameter)
    parameter.parse_params(attributes)
  end
  parameter
end

#children(name) ⇒ Object


62
63
64
65
66
67
68
# File 'lib/procedo/engine/intervention/group_parameter.rb', line 62

def children(name)
  parameters = []
  each_member do |member|
    parameters << member if member.name == name
  end
  parameters
end

#each_member(&_block) ⇒ Object


54
55
56
57
58
59
60
# File 'lib/procedo/engine/intervention/group_parameter.rb', line 54

def each_member(&_block)
  @members.each do |_reflection, children|
    children.each do |_id, member|
      yield member
    end
  end
end

#handlers_statesObject


41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/procedo/engine/intervention/group_parameter.rb', line 41

def handlers_states
  hash = {}
  each_member do |parameter|
    param_name = parameter.param_name
    next unless parameter.respond_to? :handlers_states
    states = parameter.handlers_states
    next if states.empty?
    hash[param_name] ||= {}
    hash[param_name][parameter.id.to_s] = states
  end
  hash
end

#impact_with(steps) ⇒ Object


93
94
95
96
97
98
99
100
101
# File 'lib/procedo/engine/intervention/group_parameter.rb', line 93

def impact_with(steps)
  if steps.size == 1
    impact(step)
  elsif steps.size >= 2
    @members[steps[0]][steps[1]].impact_with(steps[2..-1])
  else
    raise 'Invalid steps: ' + steps.inspect
  end
end

#parameters_of_name(name) ⇒ Object


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

def parameters_of_name(name)
  parameters = []
  each_member do |member|
    parameters << member if member.name == name
    if member.respond_to?(:parameters_of_name)
      parameters += member.parameters_of_name(name)
    end
  end
  parameters
end

#parse_params(params) ⇒ Object


21
22
23
24
25
26
27
28
29
# File 'lib/procedo/engine/intervention/group_parameter.rb', line 21

def parse_params(params)
  CHILDREN_PARAM_NAMES.each do |param_name|
    next unless params[param_name]
    params[param_name].each do |id, attributes|
      # puts "Add #{param_name}: #{id} (#{attributes.inspect})".magenta
      add(param_name, id, attributes)
    end
  end
end

#to_hashObject


31
32
33
34
35
36
37
38
39
# File 'lib/procedo/engine/intervention/group_parameter.rb', line 31

def to_hash
  hash = super
  each_member do |parameter|
    param_name = parameter.param_name
    hash[param_name] ||= {}
    hash[param_name][parameter.id.to_s] = parameter.to_hash
  end
  hash
end