Class: LazyGraph::Context
- Inherits:
-
Object
- Object
- LazyGraph::Context
- 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
-
#input ⇒ Object
Returns the value of attribute input.
-
#ruleset ⇒ Object
Returns the value of attribute ruleset.
Instance Method Summary collapse
- #[](*parts) ⇒ Object
- #debug(path) ⇒ Object
- #get(path) ⇒ Object
- #get_json(path) ⇒ Object
-
#initialize(graph, input) ⇒ Context
constructor
A new instance of Context.
- #pretty_print(q) ⇒ Object
- #resolve(path) ⇒ Object
Constructor Details
#initialize(graph, input) ⇒ Context
Returns a new instance of Context.
9 10 11 12 13 14 15 16 17 |
# 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 @graph.root_node.properties.each_key do |key| define_singleton_method(key) { get(key) } end end |
Instance Attribute Details
#input ⇒ Object
Returns the value of attribute input.
7 8 9 |
# File 'lib/lazy_graph/context.rb', line 7 def input @input end |
#ruleset ⇒ Object
Returns the value of attribute ruleset.
7 8 9 |
# File 'lib/lazy_graph/context.rb', line 7 def ruleset @ruleset end |
Instance Method Details
#[](*parts) ⇒ Object
83 84 85 |
# File 'lib/lazy_graph/context.rb', line 83 def [](*parts) get(parts.map { |p| p.is_a?(Integer) ? "[#{p}]" : p.to_s }.join('.').gsub('.[', '[')) end |
#debug(path) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/lazy_graph/context.rb', line 30 def debug(path) result = resolve(path) raise result[:error] if result[:err] result[:debug_trace] end |
#get(path) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/lazy_graph/context.rb', line 23 def get(path) result = resolve(path) raise result[:error] if result[:err] result[:output] end |
#get_json(path) ⇒ Object
19 20 21 |
# File 'lib/lazy_graph/context.rb', line 19 def get_json(path) HashUtils.strip_missing(get(path)) end |
#pretty_print(q) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/lazy_graph/context.rb', line 69 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 q.group do q.text 'input=' q.pp(@input) end end end |
#resolve(path) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/lazy_graph/context.rb', line 37 def resolve(path) @input = @graph.root_node.fetch_item({ input: @input }, :input, nil) query = PathParser.parse(path, strip_root: true) stack = StackPointer.new(nil, @input, 0, 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: result, debug_trace: debug_trace } rescue AbortError, ValidationError => e { output: nil, err: e., status: :abort, error: e } rescue StandardError => e if @graph.debug? LazyGraph.logger.error(e.) LazyGraph.logger.error(e.backtrace.join("\n")) end { output: nil, err: e., backtrace: e.backtrace, error: e } end |