class Rookout::Processor::Paths::ArithmeticPath

Public Class Methods

new(path) click to toggle source
# File lib/rookout/processor/paths/arithmetic_path.rb, line 15
def initialize path
  if path.is_a? Hash
    path = path["path"] || path[:path]
  end

  @raw_path = path

  if path.start_with?("NOT(") && path.end_with?(")")
    @path = path["NOT(".length..-2]
    @negation = true
  else
    @path = path
    @negation = false
  end

  raise RookInvalidArithmeticPath, @raw_path if @path.empty?
end

Public Instance Methods

normalize(result) click to toggle source
# File lib/rookout/processor/paths/arithmetic_path.rb, line 62
def normalize result
  if result.is_a? Canopy::List
    new_array = result.obj.map(&:obj)
    return Namespaces::RubyObjectNamespace.new new_array
  end

  if result.is_a? Canopy::NamespaceResult
    return result.obj
  end

  if result.is_a? Canopy::ToolExceptionMarker
    raise result.obj if result.obj.is_a? Exceptions::ToolException
    raise Exceptions::RookInvalidArithmeticPath.new(@raw_path, result.obj)
  end

  res = result.obj
  res = !res if [true, false].include?(res) && @negation
  Namespaces::RubyObjectNamespace.new res
end
read_from(namespace) click to toggle source
# File lib/rookout/processor/paths/arithmetic_path.rb, line 33
def read_from namespace
  begin
    result = Maps.parse @path, actions: Canopy::Actions.new(namespace)
  rescue Maps::ParseError => e
    raise RookInvalidArithmeticPath.new(@path, e)
  end

  normalize result
end
write_to(namespace, value) click to toggle source
# File lib/rookout/processor/paths/arithmetic_path.rb, line 43
def write_to namespace, value
  context = Canopy::Actions.new namespace

  begin
    # initialize operations chain - by parsing the expression - ignore return value
    Maps.parse @path, actions: context
  rescue Maps::ParseError => e
    raise RookInvalidArithmeticPath.new(@path, e)
  end

  raise RookInvalidArithmeticPath, @raw_path if context.operations.empty?

  (context.operations.length - 1).times do |i|
    namespace = context.operations[i].read namespace, true
  end

  context.operations[context.operations.length - 1].write namespace, value
end