class Balmora::Context

Public Class Methods

new(state, context) click to toggle source
# 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

Public Instance Methods

_not(is_not, result) click to toggle source
# File lib/balmora/context.rb, line 81
def _not(is_not, result)
  if is_not
    return !result
  end

  return result
end
execute() click to toggle source
# 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
init() click to toggle source
# 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) click to toggle source
# File lib/balmora/context.rb, line 93
def option(option)
  return @variables.inject(self.instance_variable_get(:"@#{option}"))
end
options() click to toggle source
# File lib/balmora/context.rb, line 33
def options()
  return [:operator, :operand]
end
run() click to toggle source
# File lib/balmora/context.rb, line 89
def run()
  raise Error.new("run should be implemented in subclass")
end
verify() click to toggle source
# 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