Class: Balmora::Contexts

Inherits:
Object
  • Object
show all
Defined in:
lib/balmora/contexts.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, extension, variables, state) ⇒ Contexts

Returns a new instance of Contexts.



7
8
9
10
11
12
# File 'lib/balmora/contexts.rb', line 7

def initialize(logger, extension, variables, state)
  @state = state
  @logger = logger
  @extension = extension
  @variables = variables
end

Class Method Details

.factory(state) ⇒ Object



3
4
5
# File 'lib/balmora/contexts.rb', line 3

def self.factory(state)
  return self.new(state.logger, state.extension, state.variables, state)
end

Instance Method Details

#_check_array(contexts) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/balmora/contexts.rb', line 45

def _check_array(contexts)
  operator = :and
  result = true
  contexts.each() { |context|
    if context == 'or'
      operator = :or
      next
    end

    if operator == :or
      result = result || check(context)
      operator = :and
    else
      if !result
        next
      end

      result = result && check(context)
    end
  }

  return result
end

#check(context) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/balmora/contexts.rb', line 14

def check(context)
  if context.nil?()
    return true
  end

  if context.instance_of?(::Array)
    return _check_array(context)
  end

  if context.instance_of?(::String)
    context = {
      context: 'exec-result',
      exec: context,
      operator: 'equal',
      operand: 0,
    }
  end

  context_class = @extension.get(Balmora::Context, context[:context])
  context_instance = context_class.new(@state, context)
  context_instance.init()

  if !(context_class < Balmora::Context)
    raise Error.new("Context #{context_class} should be subclass of context")
  end

  context_instance.verify()
  result = context_instance.execute()
  return result
end