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) ⇒ Object
- #context(input) ⇒ Object
- #debug? ⇒ Boolean
-
#initialize(input_schema, debug: false, validate: true, helpers: nil) ⇒ Graph
constructor
A new instance of Graph.
- #validate!(input, schema = @json_schema) ⇒ Object
Constructor Details
#initialize(input_schema, debug: false, validate: true, helpers: nil) ⇒ Graph
Returns a new instance of Graph.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/lazy_graph/graph.rb', line 16 def initialize(input_schema, debug: false, validate: true, helpers: 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) end |
Instance Attribute Details
#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 |
#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 |
#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
#build_node(schema, path = :'$', name = :root, parent = nil) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/lazy_graph/graph.rb', line 31 def build_node(schema, path = :'$', name = :root, parent = 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) if node.type == :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 == :array node.children = build_node(schema.fetch(:items, {}), :"#{path}[]", :items, node) end node end |
#context(input) ⇒ Object
13 |
# File 'lib/lazy_graph/graph.rb', line 13 def context(input) = Context.new(self, input) |
#debug? ⇒ Boolean
14 |
# File 'lib/lazy_graph/graph.rb', line 14 def debug? = @debug |
#validate!(input, schema = @json_schema) ⇒ Object
55 56 57 |
# File 'lib/lazy_graph/graph.rb', line 55 def validate!(input, schema = @json_schema) JSON::Validator.validate!(schema, input) end |