Class: Procedo::Procedure

Inherits:
Object
  • Object
show all
Defined in:
lib/procedo/procedure.rb,
lib/procedo/procedure/field.rb,
lib/procedo/procedure/setter.rb,
lib/procedo/procedure/handler.rb,
lib/procedo/procedure/reading.rb,
lib/procedo/procedure/parameter.rb,
lib/procedo/procedure/attribute.rb,
lib/procedo/procedure/computation.rb,
lib/procedo/procedure/group_parameter.rb,
lib/procedo/procedure/product_parameter.rb

Overview

This class represents a procedure

Defined Under Namespace

Classes: Attribute, Computation, Field, GroupParameter, Handler, Parameter, ProductParameter, Reading, Setter

Constant Summary collapse

ROOT_NAME =
'root_'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Procedure

Returns a new instance of Procedure.


15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/procedo/procedure.rb', line 15

def initialize(name, options = {})
  @name = name.to_sym
  @categories = []
  @mandatory_actions = []
  @optional_actions = []
  @root_group = Procedo::Procedure::GroupParameter.new(self, ROOT_NAME, cardinality: 1)
  # Adds categories & action
  options[:categories].each { |c| add_category(c) } if options[:categories]
  options[:mandatory_actions].each { |c| add_action(c) } if options[:mandatory_actions]
  options[:optional_actions].each { |c| add_action(c, true) } if options[:optional_actions]
  # Compile it
  # self.compile!
end

Instance Attribute Details

#categoriesObject (readonly)

Returns the value of attribute categories


9
10
11
# File 'lib/procedo/procedure.rb', line 9

def categories
  @categories
end

#idObject (readonly)

Returns the value of attribute id


9
10
11
# File 'lib/procedo/procedure.rb', line 9

def id
  @id
end

#mandatory_actionsObject (readonly)

Returns the value of attribute mandatory_actions


9
10
11
# File 'lib/procedo/procedure.rb', line 9

def mandatory_actions
  @mandatory_actions
end

#nameObject (readonly) Also known as: uid

Returns the value of attribute name


9
10
11
# File 'lib/procedo/procedure.rb', line 9

def name
  @name
end

#optional_actionsObject (readonly)

Returns the value of attribute optional_actions


9
10
11
# File 'lib/procedo/procedure.rb', line 9

def optional_actions
  @optional_actions
end

Instance Method Details

#actionsObject

All actions (mandatory and optional)


30
31
32
# File 'lib/procedo/procedure.rb', line 30

def actions
  @mandatory_actions + @optional_actions
end

#actions_selectionObject


111
112
113
# File 'lib/procedo/procedure.rb', line 111

def actions_selection
  action_selection(actions)
end

#activity_familiesObject

Returns activity families of the procedure


120
121
122
123
124
125
126
127
# File 'lib/procedo/procedure.rb', line 120

def activity_families
  @activity_families ||= categories.map do |c|
    families = c.activity_family || []
    families.map do |f|
      Nomen::ActivityFamily.all(f)
    end
  end.flatten.uniq.map(&:to_sym)
end

#add_action(name, optional = false) ⇒ Object

Adds action to procedure


52
53
54
55
56
57
# File 'lib/procedo/procedure.rb', line 52

def add_action(name, optional = false)
  action = Nomen::ProcedureAction.find(name)
  raise "Invalid action: #{name.inspect}".red unless action
  actions = optional ? @optional_actions : @mandatory_actions
  actions << action unless actions.include?(action)
end

#add_category(name) ⇒ Object

Adds category to procedure


35
36
37
38
39
# File 'lib/procedo/procedure.rb', line 35

def add_category(name)
  category = Nomen::ProcedureCategory.find(name)
  raise "Invalid category: #{name.inspect}".red unless category
  @categories << category unless @categories.include?(category)
end

#can_compute_duration?Boolean

Returns:

  • (Boolean)

115
116
117
# File 'lib/procedo/procedure.rb', line 115

def can_compute_duration?
  @duration_tree.present?
end

#category_namesObject

Returns names of categories of procedure


47
48
49
# File 'lib/procedo/procedure.rb', line 47

def category_names
  @categories.map(&:name).map(&:to_sym)
end

#check!Object


80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/procedo/procedure.rb', line 80

def check!
  # Check ungiven roles
  remaining_roles = roles - given_roles.uniq
  if remaining_roles.any?
    raise Procedo::Errors::MissingRole, "Remaining roles of procedure #{name} are not given: #{remaining_roles.join(', ')}"
  end

  # Check producers
  new_parameters.each do |parameter|
    unless parameter.producer.is_a?(Parameter)
      raise Procedo::Errors::UnknownAspect, "Unknown parameter producer for #{parameter.name}"
    end
  end
end

#handled_parametersObject


150
151
152
# File 'lib/procedo/procedure.rb', line 150

def handled_parameters
  parameters.select(&:handled?)
end

#has_action?(action) ⇒ Boolean

Returns true if action is one of the procedure, false otherwise.

Returns:

  • (Boolean)

60
61
62
# File 'lib/procedo/procedure.rb', line 60

def has_action?(action)
  actions.detect { |a| a.name.to_s == action.to_s }
end

#human_name(options = {}) ⇒ Object

Returns human_name of the procedure


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

def human_name(options = {})
  default = []
  default << "labels.procedures.#{name}".to_sym
  default << "labels.#{name}".to_sym
  default << name.to_s.humanize
  "procedures.#{name}".t(options.merge(default: default))
end

#mandatory_actions_selectionObject


103
104
105
# File 'lib/procedo/procedure.rb', line 103

def mandatory_actions_selection
  action_selection(@mandatory_actions)
end

#new_parametersObject

Returns only parameters which must be built during runnning process


146
147
148
# File 'lib/procedo/procedure.rb', line 146

def new_parameters
  parameters.select(&:new?)
end

#of_activity_family?(*families) ⇒ Boolean

Returns:

  • (Boolean)

95
96
97
# File 'lib/procedo/procedure.rb', line 95

def of_activity_family?(*families)
  (activity_families & families).any?
end

#of_category?(*categories) ⇒ Boolean

Returns:

  • (Boolean)

99
100
101
# File 'lib/procedo/procedure.rb', line 99

def of_category?(*categories)
  (category_names & categories).any?
end

#optional_actions_selectionObject


107
108
109
# File 'lib/procedo/procedure.rb', line 107

def optional_actions_selection
  action_selection(@optional_actions)
end

#parameter_namesObject

Lists names of all parameters


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

def parameter_names
  @parameter_names ||= parameters.map(&:name)
end

#parametersObject

Retrieve all parameters recursively in group or subgroups


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

def parameters
  @root_group.parameters(true)
end

#remove_action(name) ⇒ Object

Removes action of procedure


65
66
67
68
# File 'lib/procedo/procedure.rb', line 65

def remove_action(name)
  @mandatory_actions.delete_if { |c| c.name == name.to_sym }
  @optional_actions.delete_if { |c| c.name == name.to_sym }
end

#remove_category(name) ⇒ Object

Removes category of procedure


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

def remove_category(name)
  @categories.delete_if { |c| c.name == name.to_sym }
end

#required?Boolean

Returns if the procedure is required

Returns:

  • (Boolean)

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

def required?
  @required
end