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/codeable.rb,
lib/procedo/procedure/attribute.rb,
lib/procedo/procedure/parameter.rb,
lib/procedo/procedure/computation.rb,
lib/procedo/procedure/group_parameter.rb,
lib/procedo/procedure/product_parameter.rb

Overview

This class represents a procedure. It's the definition

Defined Under Namespace

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

Constant Summary collapse

ROOT_NAME =
'root_'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Procedure.


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/procedo/procedure.rb', line 68

def initialize(name, options = {})
  @name = name.to_sym
  @categories = []
  @mandatory_actions = []
  @optional_actions = []
  @varieties = []
  @root_group = Procedo::Procedure::GroupParameter.new(self, ROOT_NAME, cardinality: 1)
  @deprecated = !!options[:deprecated]
  @hidden = !!options[:hidden]
  # 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]
  options[:varieties].each { |v| add_variety(v) } if options[:varieties]
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

#hiddenObject (readonly)

Returns the value of attribute hidden


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

def hidden
  @hidden
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

#varietiesObject (readonly)

Returns the value of attribute varieties


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

def varieties
  @varieties
end

Class Method Details

.find(name) ⇒ Object


16
17
18
# File 'lib/procedo/procedure.rb', line 16

def find(name)
  Procedo.find(name)
end

.find_each(&block) ⇒ Object


20
21
22
# File 'lib/procedo/procedure.rb', line 20

def find_each(&block)
  Procedo.procedures.each(&block)
end

.of_activity_family(*families) ⇒ Object

Returns procedures of given activity families


25
26
27
28
29
30
# File 'lib/procedo/procedure.rb', line 25

def of_activity_family(*families)
  options = families.extract_options!
  select(options) do |p|
    p.of_activity_family?(*families)
  end
end

.of_category(*categories) ⇒ Object

Returns procedures of given categories


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

def of_category(*categories)
  options = categories.extract_options!
  select(options) do |p|
    p.of_category?(*categories)
  end
end

.of_main_category(*categories) ⇒ Object

Returns procedures which main category match given ones


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

def of_main_category(*categories)
  options = categories.extract_options!
  select(options) do |p|
    categories.detect { |c| p.categories.first <= c }
  end
end

.of_varieties(*varieties) ⇒ Object

Return procedures of given varieties


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

def of_varieties(*varieties)
  options = varieties.extract_options!
  select(options) do |p|
    p.has_varieties?(*varieties)
  end
end

.select(options = {}) ⇒ Object

Select procedures with given block


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

def select(options = {})
  include_deprecated = options[:include_deprecated]
  include_hidden = options[:include_hidden]
  Procedo.procedures.select do |p|
    (include_deprecated || !p.deprecated?) &&
      (include_hidden || !p.hidden?) &&
      yield(p)
  end
end

Instance Method Details

#actionsObject

All actions (mandatory and optional)


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

def actions
  @mandatory_actions + @optional_actions
end

#actions_selectionObject


206
207
208
# File 'lib/procedo/procedure.rb', line 206

def actions_selection
  action_selection(actions)
end

#activity_familiesObject

Returns activity families of the procedure


215
216
217
218
219
220
221
222
# File 'lib/procedo/procedure.rb', line 215

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


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

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


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

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

#add_variety(name) ⇒ Object

Adds variety scope of procedure


174
175
176
177
178
# File 'lib/procedo/procedure.rb', line 174

def add_variety(name)
  variety = Nomen::Variety.find(name)
  raise "Invalid variety: #{name.inspect}".red unless variety
  @varieties << variety unless @varieties.include?(variety)
end

#can_compute_duration?Boolean

Returns:

  • (Boolean)

210
211
212
# File 'lib/procedo/procedure.rb', line 210

def can_compute_duration?
  @duration_tree.present?
end

#category_namesObject

Returns names of categories of procedure


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

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

#deprecated?Boolean

Returns:

  • (Boolean)

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

def deprecated?
  @deprecated
end

#handled_parametersObject


245
246
247
# File 'lib/procedo/procedure.rb', line 245

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

#has_action?(action) ⇒ Boolean

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

Returns:

  • (Boolean)

123
124
125
# File 'lib/procedo/procedure.rb', line 123

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

#has_varieties?(*varieties) ⇒ Boolean

Returns true if varieties are included in one of the procedure, false otherwise.

Returns:

  • (Boolean)

181
182
183
184
185
186
187
188
# File 'lib/procedo/procedure.rb', line 181

def has_varieties?(*varieties)
  @varieties.any? do |obj|
    varieties.all? do |v|
      variety = Nomen::Variety.find(v)
      variety && obj >= variety
    end
  end
end

#hidden?Boolean

Returns:

  • (Boolean)

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

def hidden?
  @hidden
end

#human_name(options = {}) ⇒ Object

Returns human_name of the procedure


232
233
234
235
236
237
238
# File 'lib/procedo/procedure.rb', line 232

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

#lintObject


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/procedo/procedure.rb', line 143

def lint
  messages = []
  product_parameters.each do |p|
    if p.filter
      begin
        WorkingSet.to_sql(p.filter)
      rescue SyntaxError => e
        messages << "Cannot parse filter of #{p.name}: #{e.message}"
      rescue WorkingSet::InvalidExpression => e
        messages << "Invalid expression in filter of #{p.name}: #{e.message}"
      end
    end
    if p.component_of?

    end
    p.handlers.each do |handler|
      %w[condition forward backward].each do |tree|
        next unless handler.send("#{tree}?")
        parameters = handler.send("#{tree}_parameters")
        parameters.each do |parameter|
          unless find(parameter)
            messages << "Cannot find #{parameter} from #{handler.name}/#{tree} in #{p.name}"
          end
        end
      end
    end
  end
  messages
end

#mandatory_actions_selectionObject


198
199
200
# File 'lib/procedo/procedure.rb', line 198

def mandatory_actions_selection
  action_selection(@mandatory_actions)
end

#new_parametersObject

Returns only parameters which must be built during runnning process


241
242
243
# File 'lib/procedo/procedure.rb', line 241

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

#of_activity_family?(*families) ⇒ Boolean

Returns:

  • (Boolean)

190
191
192
# File 'lib/procedo/procedure.rb', line 190

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

#of_category?(*categories) ⇒ Boolean

Returns:

  • (Boolean)

194
195
196
# File 'lib/procedo/procedure.rb', line 194

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

#optional_actions_selectionObject


202
203
204
# File 'lib/procedo/procedure.rb', line 202

def optional_actions_selection
  action_selection(@optional_actions)
end

#parameter_namesObject

Lists names of all parameters


139
140
141
# File 'lib/procedo/procedure.rb', line 139

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

#parametersObject

Retrieve all parameters recursively in group or subgroups


134
135
136
# File 'lib/procedo/procedure.rb', line 134

def parameters
  @root_group.parameters(true)
end

#remove_action(name) ⇒ Object

Removes action of procedure


128
129
130
131
# File 'lib/procedo/procedure.rb', line 128

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


105
106
107
# File 'lib/procedo/procedure.rb', line 105

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

#required?Boolean

Returns if the procedure is required

Returns:

  • (Boolean)

227
228
229
# File 'lib/procedo/procedure.rb', line 227

def required?
  @required
end