Class: LazyGraph::Graph
- Inherits:
-
Object
- Object
- LazyGraph::Graph
- Defined in:
- lib/lazy_graph/graph.rb
Instance Attribute Summary collapse
-
#json_schema ⇒ Object
readonly
Returns the value of attribute json_schema.
-
#root_node ⇒ Object
readonly
Returns the value of attribute root_node.
-
#validate ⇒ Object
readonly
Returns the value of attribute validate.
Instance Method Summary collapse
- #build_node(schema, path = :'$', name = :root, parent = nil, namespace: nil) ⇒ Object
- #context(input) ⇒ Object (also: #input, #feed)
- #debug? ⇒ Boolean
-
#initialize(input_schema, debug: false, validate: true, helpers: nil, namespace: nil) ⇒ Graph
constructor
A new instance of Graph.
- #pretty_print(q) ⇒ Object
- #validate!(input, schema = @json_schema) ⇒ Object
Constructor Details
permalink #initialize(input_schema, debug: false, validate: true, helpers: nil, namespace: nil) ⇒ Graph
Returns a new instance of Graph.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/lazy_graph/graph.rb', line 18 def initialize(input_schema, debug: false, validate: true, helpers: nil, namespace: nil) @json_schema = HashUtils.deep_dup(input_schema, symbolize: true, signature: signature = [0]).merge(type: :object) @debug = debug @validate = validate @helpers = helpers VALIDATION_CACHE[signature[0]] ||= validate!(@json_schema, METASCHEMA) if [true, 'schema'].include?(validate) if @json_schema[:type].to_sym != :object || @json_schema[:properties].nil? raise ArgumentError, 'Root schema must be a non-empty object' end @root_node = build_node(@json_schema, namespace: namespace) @root_node.build_derived_inputs! end |
Instance Attribute Details
permalink #json_schema ⇒ Object (readonly)
Returns the value of attribute json_schema.
11 12 13 |
# File 'lib/lazy_graph/graph.rb', line 11 def json_schema @json_schema end |
permalink #root_node ⇒ Object (readonly)
Returns the value of attribute root_node.
11 12 13 |
# File 'lib/lazy_graph/graph.rb', line 11 def root_node @root_node end |
permalink #validate ⇒ Object (readonly)
Returns the value of attribute validate.
11 12 13 |
# File 'lib/lazy_graph/graph.rb', line 11 def validate @validate end |
Instance Method Details
permalink #build_node(schema, path = :'$', name = :root, parent = nil, namespace: nil) ⇒ Object
[View source]
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/lazy_graph/graph.rb', line 34 def build_node(schema, path = :'$', name = :root, parent = nil, namespace: nil) schema[:type] = schema[:type].to_sym node = \ case schema[:type] when :object then ObjectNode when :array then ArrayNode else Node end.new(name, path, schema, parent, debug: @debug, helpers: @helpers, namespace: namespace) if node.type.equal?(:object) node.children = \ { properties: schema.fetch(:properties, {}).map do |key, value| [key, build_node(value, :"#{path}.#{key}", key, node)] end.to_h.compare_by_identity, pattern_properties: schema.fetch(:patternProperties, {}).map do |key, value| [Regexp.new(key.to_s), build_node(value, :"#{path}.#{key}", :'<property>', node)] end } elsif node.type.equal?(:array) node.children = build_node(schema.fetch(:items, {}), :"#{path}[]", :items, node) end node end |
permalink #context(input) ⇒ Object Also known as: input, feed
[View source]
13 |
# File 'lib/lazy_graph/graph.rb', line 13 def context(input) = Context.new(self, input) |
permalink #debug? ⇒ Boolean
14 |
# File 'lib/lazy_graph/graph.rb', line 14 def debug? = !!@debug |
permalink #pretty_print(q) ⇒ Object
[View source]
65 66 67 68 69 70 71 72 73 |
# File 'lib/lazy_graph/graph.rb', line 65 def pretty_print(q) # Start the custom pretty print q.group(1, '<LazyGraph::Graph ', '>') do q.group do q.text 'props=' q.text root_node.children[:properties].keys end end end |
permalink #validate!(input, schema = @json_schema) ⇒ Object
[View source]
59 60 61 62 63 |
# File 'lib/lazy_graph/graph.rb', line 59 def validate!(input, schema = @json_schema) JSON::Validator.validate!(schema, input) rescue JSON::Schema::ValidationError => e raise ValidationError, "Input validation failed: #{e.}", cause: e end |