Class: Rookout::Processor::Paths::ArithmeticPath

Inherits:
Path
  • Object
show all
Defined in:
lib/rookout/processor/paths/arithmetic_path.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ ArithmeticPath

Returns a new instance of ArithmeticPath.

Raises:

  • (RookInvalidArithmeticPath)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rookout/processor/paths/arithmetic_path.rb', line 15

def initialize path
  super()

  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

Instance Method Details

#normalize(result) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rookout/processor/paths/arithmetic_path.rb', line 64

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) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/rookout/processor/paths/arithmetic_path.rb', line 35

def read_from namespace
  begin
    result = Maps.parse @path, actions: Canopy::Actions.new(namespace)
  rescue Maps::ParseError, NameError => e
    raise RookInvalidArithmeticPath.new(@path, e)
  end

  normalize result
end

#write_to(namespace, value) ⇒ Object

Raises:

  • (RookInvalidArithmeticPath)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rookout/processor/paths/arithmetic_path.rb', line 45

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