Class: RuleBox::Facade

Inherits:
Object
  • Object
show all
Includes:
ExecutionHook, MethodHelper
Defined in:
lib/rule_box/facade.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ExecutionHook

included

Methods included from MethodHelper

included

Constructor Details

#initialize(**dependencies) ⇒ Facade

Returns a new instance of Facade.



13
14
15
16
# File 'lib/rule_box/facade.rb', line 13

def initialize(**dependencies)
  set_dependencies! dependencies
  @executed = false
end

Instance Attribute Details

#bucketObject (readonly)

Returns the value of attribute bucket.



8
9
10
# File 'lib/rule_box/facade.rb', line 8

def bucket
  @bucket
end

#current_methodObject (readonly)

Returns the value of attribute current_method.



8
9
10
# File 'lib/rule_box/facade.rb', line 8

def current_method
  @current_method
end

#current_strategyObject (readonly)

Returns the value of attribute current_strategy.



8
9
10
# File 'lib/rule_box/facade.rb', line 8

def current_strategy
  @current_strategy
end

#entityObject (readonly)

Returns the value of attribute entity.



8
9
10
# File 'lib/rule_box/facade.rb', line 8

def entity
  @entity
end

#exceptionObject (readonly)

Returns the value of attribute exception.



8
9
10
# File 'lib/rule_box/facade.rb', line 8

def exception
  @exception
end

#executedObject (readonly)

Returns the value of attribute executed.



8
9
10
# File 'lib/rule_box/facade.rb', line 8

def executed
  @executed
end

#last_resultObject (readonly)

Returns the value of attribute last_result.



8
9
10
# File 'lib/rule_box/facade.rb', line 8

def last_result
  @last_result
end

#statusObject (readonly)

Returns the value of attribute status.



8
9
10
# File 'lib/rule_box/facade.rb', line 8

def status
  @status
end

#strategiesObject (readonly)

Returns the value of attribute strategies.



8
9
10
# File 'lib/rule_box/facade.rb', line 8

def strategies
  @strategies
end

Class Method Details

.add_dependency(key, &block) ⇒ Object



216
217
218
# File 'lib/rule_box/facade.rb', line 216

def add_dependency(key, &block)
  dependencies[key.to_sym] = block
end

.clear_configuration!Object



224
225
226
227
# File 'lib/rule_box/facade.rb', line 224

def clear_configuration!
  @dependencies = {}
  clear_hooks!
end

.configure(&block) ⇒ Object



220
221
222
# File 'lib/rule_box/facade.rb', line 220

def configure(&block)
  block.call(self)
end

.set_dependencies(facade, dependencies) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/rule_box/facade.rb', line 229

def set_dependencies(facade, dependencies)
  errors = []

  self.dependencies.each do |key, block|
    if dependencies.key? key
      value = dependencies[key]
      block&.call(value, errors)
      facade.send(:set, key, value)
    else
      errors << "missing keyword: #{key}"
    end
  rescue StandardError => e
    errors << e.message
  end

  raise errors.join("\n") unless errors.empty?
end

.use_case!Object



247
248
249
# File 'lib/rule_box/facade.rb', line 247

def use_case!
  define_method(:use_case) { entity }
end

Instance Method Details

#add_error(msg) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/rule_box/facade.rb', line 18

def add_error(msg)
  if msg.is_a? Array
    msg.each { |message| add_error(message) }
  else
    @errors << msg
  end
end

#exec(method = :perform, entity, **args) ⇒ Object



26
27
28
# File 'lib/rule_box/facade.rb', line 26

def exec(method = :perform, entity, **args)
  perform method, entity, **args
end

#get(key) ⇒ Object



30
31
32
# File 'lib/rule_box/facade.rb', line 30

def get(key)
  keys[key.to_s].clone
end

#instance_valuesObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rule_box/facade.rb', line 45

def instance_values
  hash = {
    current_strategy: @current_strategy&.instance_values,
    strategies: (@strategies || []).map(&:instance_values)
  }

  keys.each { |k, v| hash[k.to_sym] = v.clone }

  i[
    entity exception current_class current_method bucket
    executed last_result status errors steps
  ].each { |key| hash[key] = send(key).clone }

  hash
end

#runObject



34
35
36
37
38
39
# File 'lib/rule_box/facade.rb', line 34

def run
  return unless @next_run

  send @next_run
  true
end

#set_status(status) ⇒ Object



41
42
43
# File 'lib/rule_box/facade.rb', line 41

def set_status(status)
  @status = status
end