Class: LazyGraph::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/lazy_graph/context.rb

Overview

Context class is responsible for managing ruleset and input data, allowing querying and dynamic method calls to access input fields.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graph, input) ⇒ Context

Returns a new instance of Context.



9
10
11
12
13
14
# File 'lib/lazy_graph/context.rb', line 9

def initialize(graph, input)
  input = HashUtils.deep_dup(input, symbolize: true)
  graph.validate!(input) if [true, 'input'].include?(graph.validate)
  @graph = graph
  @input = input
end

Instance Attribute Details

#inputObject

Returns the value of attribute input.



7
8
9
# File 'lib/lazy_graph/context.rb', line 7

def input
  @input
end

#rulesetObject

Returns the value of attribute ruleset.



7
8
9
# File 'lib/lazy_graph/context.rb', line 7

def ruleset
  @ruleset
end

Instance Method Details

#pretty_print(q) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/lazy_graph/context.rb', line 50

def pretty_print(q)
  # Start the custom pretty print
  q.group(1, '<LazyGraph::Context ', '>') do
    q.group do
      q.text 'graph='
      q.pp(@graph)
    end
  end
end

#query(paths) ⇒ Object Also known as: []



16
17
18
# File 'lib/lazy_graph/context.rb', line 16

def query(paths)
  paths.is_a?(Array) ? paths.map { |path| resolve(path) } : resolve(paths)
end

#resolve(path) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/lazy_graph/context.rb', line 20

def resolve(path)
  @input = @graph.root_node.fetch_item({ input: @input }, :input, nil)

  query = PathParser.parse(path, true)
  stack = StackPointer.new(nil, @input, 0, :'$', nil)
  stack.root = stack

  result = @graph.root_node.resolve(query, stack)

  @graph.root_node.clear_visits!
  if @graph.debug?
    debug_trace = stack.frame[:DEBUG]
    stack.frame[:DEBUG] = nil
  end
  {
    output: HashUtils.strip_invalid(result),
    debug_trace: HashUtils.strip_invalid(debug_trace)
  }
rescue LazyGraph::AbortError => e
  {
    output: { err: e.message, status: :abort }
  }
rescue StandardError => e
  LazyGraph.logger.error(e.message)
  LazyGraph.logger.error(e.backtrace)
  {
    output: { err: e.message, backtrace: e.backtrace }
  }
end