Class: Caixanegra::Unit

Inherits:
Object
  • Object
show all
Defined in:
lib/caixanegra/unit.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(oid, inputs = {}, mappings = {}, carry_over = {}, storage = {}) ⇒ Unit

Returns a new instance of Unit.



7
8
9
10
11
12
13
14
# File 'lib/caixanegra/unit.rb', line 7

def initialize(oid, inputs = {}, mappings = {}, carry_over = {}, storage = {})
  @oid = oid
  @mappings = mappings
  @inputs = inputs
  @carry_over = carry_over.with_indifferent_access
  @storage = storage.with_indifferent_access
  set_mapping_defaults
end

Class Attribute Details

.assignmentsObject (readonly)

Returns the value of attribute assignments.



119
120
121
# File 'lib/caixanegra/unit.rb', line 119

def assignments
  @assignments
end

.colorObject (readonly)

Returns the value of attribute color.



119
120
121
# File 'lib/caixanegra/unit.rb', line 119

def color
  @color
end

.descriptionObject (readonly)

Returns the value of attribute description.



119
120
121
# File 'lib/caixanegra/unit.rb', line 119

def description
  @description
end

.exitsObject (readonly)

Returns the value of attribute exits.



119
120
121
# File 'lib/caixanegra/unit.rb', line 119

def exits
  @exits
end

.inputsObject (readonly)

Returns the value of attribute inputs.



119
120
121
# File 'lib/caixanegra/unit.rb', line 119

def inputs
  @inputs
end

.scopeObject (readonly)

Returns the value of attribute scope.



119
120
121
# File 'lib/caixanegra/unit.rb', line 119

def scope
  @scope
end

.typeObject (readonly)

Returns the value of attribute type.



119
120
121
# File 'lib/caixanegra/unit.rb', line 119

def type
  @type
end

.unit_nameObject (readonly)

Returns the value of attribute unit_name.



119
120
121
# File 'lib/caixanegra/unit.rb', line 119

def unit_name
  @unit_name
end

Instance Attribute Details

#oidObject (readonly)

Returns the value of attribute oid.



5
6
7
# File 'lib/caixanegra/unit.rb', line 5

def oid
  @oid
end

Class Method Details

.configure_assignments(assignments) ⇒ Object



156
157
158
# File 'lib/caixanegra/unit.rb', line 156

def configure_assignments(assignments)
  @assignments = assignments
end

.configure_color(color) ⇒ Object



160
161
162
# File 'lib/caixanegra/unit.rb', line 160

def configure_color(color)
  @color = color
end

.configure_description(value) ⇒ Object



140
141
142
# File 'lib/caixanegra/unit.rb', line 140

def configure_description(value)
  @description = value
end

.configure_exits(exits) ⇒ Object



148
149
150
# File 'lib/caixanegra/unit.rb', line 148

def configure_exits(exits)
  @exits = exits
end

.configure_inputs(inputs) ⇒ Object



152
153
154
# File 'lib/caixanegra/unit.rb', line 152

def configure_inputs(inputs)
  @inputs = inputs
end

.configure_name(value) ⇒ Object



136
137
138
# File 'lib/caixanegra/unit.rb', line 136

def configure_name(value)
  @unit_name = value
end

.configure_scope(value) ⇒ Object



132
133
134
# File 'lib/caixanegra/unit.rb', line 132

def configure_scope(value)
  @scope = value
end

.configure_type(value) ⇒ Object



144
145
146
# File 'lib/caixanegra/unit.rb', line 144

def configure_type(value)
  @type = value
end

Instance Method Details

#carry_over(value) ⇒ Object



24
25
26
# File 'lib/caixanegra/unit.rb', line 24

def carry_over(value)
  @carry_over.merge!(value) if value.is_a? Hash
end

#colorObject



103
104
105
# File 'lib/caixanegra/unit.rb', line 103

def color
  self.class.color
end

#current_carry_overObject



16
17
18
# File 'lib/caixanegra/unit.rb', line 16

def current_carry_over
  @carry_over.dup
end

#current_storageObject



20
21
22
# File 'lib/caixanegra/unit.rb', line 20

def current_storage
  @storage.dup
end

#descriptionObject



87
88
89
# File 'lib/caixanegra/unit.rb', line 87

def description
  self.class.description
end

#exit_and_returnObject



39
40
41
42
43
# File 'lib/caixanegra/unit.rb', line 39

def exit_and_return
  {
    carry_over: @carry_over
  }
end

#exit_through(exit_id) ⇒ Object



32
33
34
35
36
37
# File 'lib/caixanegra/unit.rb', line 32

def exit_through(exit_id)
  {
    exit_through: exit_id,
    carry_over: @carry_over
  }
end

#exitsObject



95
96
97
# File 'lib/caixanegra/unit.rb', line 95

def exits
  self.class.exits || []
end

#flowObject



45
46
47
# File 'lib/caixanegra/unit.rb', line 45

def flow
  exit_through exits.first
end

#input(id) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/caixanegra/unit.rb', line 49

def input(id)
   = @mappings[id]
  input_value = case &.[](:type)
                when 'storage'
                  @storage[id]
                when 'carryover'
                  @carry_over[id]
                when 'user'
                  [:value]
                end

  if input_value.present?
    result = input_value.dup
    input_value.to_s.scan(Regexp.new(/%(.*?)%/)) do |match|
      match = match[0]
      result.gsub!("%#{match}%", @carry_over[match] || '')
    end
    input_value.to_s.scan(Regexp.new(/@(.*?)@/)) do |match|
      match = match[0]
      result.gsub!("@#{match}@", @storage[match] || '')
    end

    input_value = result
  end

  input_value.presence || @inputs[id][:default]
rescue StandardError
  raise(UnitIOException.new(self), "Unable to fetch input '#{id}'")
end

#inputsObject



91
92
93
# File 'lib/caixanegra/unit.rb', line 91

def inputs
  self.class.inputs || []
end

#persist(value) ⇒ Object



28
29
30
# File 'lib/caixanegra/unit.rb', line 28

def persist(value)
  @storage.merge!(value) if value.is_a? Hash
end

#scopeObject



79
80
81
# File 'lib/caixanegra/unit.rb', line 79

def scope
  self.class.scope
end

#setObject



99
100
101
# File 'lib/caixanegra/unit.rb', line 99

def set
  self.class.set || []
end

#unit_nameObject



83
84
85
# File 'lib/caixanegra/unit.rb', line 83

def unit_name
  self.class.unit_name
end