Class: Balmora::Context

Inherits:
Command show all
Defined in:
lib/balmora/context.rb

Direct Known Subclasses

ConfigChanged, Exec, Value

Defined Under Namespace

Classes: ConfigChanged, Exec, ExecResult, Value

Instance Method Summary collapse

Methods inherited from Command

#_execute

Constructor Details

#initialize(state, context) ⇒ Context

Returns a new instance of Context.



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/balmora/context.rb', line 3

def initialize(state, context)
  @state = state

  @logger = state.logger
  @shell = state.shell
  @config = state.config
  @variables = state.variables
  @balmora = state.balmora

  @context = context
end

Instance Method Details

#_not(is_not, result) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/balmora/context.rb', line 81

def _not(is_not, result)
  if is_not
    return !result
  end

  return result
end

#executeObject



47
48
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
78
79
# File 'lib/balmora/context.rb', line 47

def execute()
  result = run()

  operator = @operator

  is_not = false
  if operator.start_with?('not-')
    is_not = true
    operator = operator[4..-1]
  end

  case operator
    when 'match'
      return _not(is_not, result.match(@operand) != nil)
    when 'equal'
      return _not(is_not, result == @operand)
    when 'greater'
      return _not(is_not, result > @operand)
    when 'greater-or-equal'
      return _not(is_not, result >= @operand)
    when 'lesser'
      return _not(is_not, result < @operand)
    when 'lesser-or-equal'
      return _not(is_not, result <= @operand)
  end

  raise Error.new("Unknown operator #{operator}")
rescue => error
  @logger.error("#{error.inspect()}; failed to run " +
    "context: #{@context.inspect()}")

  raise error
end

#initObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/balmora/context.rb', line 15

def init()
  if (@context.keys() - [:context] - options()).length != 0
    raise Error.new("Unknown options #{(@context.keys() - [:context] -
      options()).inspect()}")
  end

  options().each() { |key|
    if self.instance_variable_defined?(:"@#{key}")
      raise Error.new("Can not use #{key} as option")
    end

    option = @context.fetch(key, nil)
    self.instance_variable_set(:"@#{key}", option)
  }

  verify()
end

#option(option) ⇒ Object



93
94
95
# File 'lib/balmora/context.rb', line 93

def option(option)
  return @variables.inject(self.instance_variable_get(:"@#{option}"))
end

#optionsObject



33
34
35
# File 'lib/balmora/context.rb', line 33

def options()
  return [:operator, :operand]
end

#runObject

Raises:



89
90
91
# File 'lib/balmora/context.rb', line 89

def run()
  raise Error.new("run should be implemented in subclass")
end

#verifyObject



37
38
39
40
41
42
43
44
45
# File 'lib/balmora/context.rb', line 37

def verify()
  if @operator.nil?()
    raise Error.new('"operator" should be defined')
  end

  if @operand.nil?()
    raise Error.new('"operand" should be defined')
  end
end