Class: EasyJSONMatcher::NodeGenerator
- Inherits:
-
Object
- Object
- EasyJSONMatcher::NodeGenerator
- Includes:
- AttributeTypeMethods
- Defined in:
- lib/easy_json_matcher/node_generator.rb
Instance Attribute Summary collapse
-
#array_opts ⇒ Object
readonly
Returns the value of attribute array_opts.
-
#attribute_opts ⇒ Object
readonly
Returns the value of attribute attribute_opts.
-
#global_opts ⇒ Object
readonly
Returns the value of attribute global_opts.
-
#node_opts ⇒ Object
readonly
Returns the value of attribute node_opts.
-
#validators ⇒ Object
readonly
Returns the value of attribute validators.
Instance Method Summary collapse
- #contains_array(key:, opts: []) {|validator| ... } ⇒ Object
- #contains_node(key:, opts: []) {|generator| ... } ⇒ Object
- #extract_opts(local:, global:) ⇒ Object
- #generate_node ⇒ Object
- #has_attribute(key:, opts:) ⇒ Object
- #has_schema(key:, name:) ⇒ Object
-
#initialize(opts: [], global_opts: []) ⇒ NodeGenerator
constructor
A new instance of NodeGenerator.
Methods included from AttributeTypeMethods
#has_boolean, #has_date, #has_number, #has_object, #has_string, #has_value
Constructor Details
#initialize(opts: [], global_opts: []) ⇒ NodeGenerator
Returns a new instance of NodeGenerator.
14 15 16 17 18 |
# File 'lib/easy_json_matcher/node_generator.rb', line 14 def initialize(opts: [], global_opts: []) @validators = {} @node_opts = extract_opts(local: opts, global: global_opts) @global_opts = global_opts end |
Instance Attribute Details
#array_opts ⇒ Object (readonly)
Returns the value of attribute array_opts.
12 13 14 |
# File 'lib/easy_json_matcher/node_generator.rb', line 12 def array_opts @array_opts end |
#attribute_opts ⇒ Object (readonly)
Returns the value of attribute attribute_opts.
12 13 14 |
# File 'lib/easy_json_matcher/node_generator.rb', line 12 def attribute_opts @attribute_opts end |
#global_opts ⇒ Object (readonly)
Returns the value of attribute global_opts.
12 13 14 |
# File 'lib/easy_json_matcher/node_generator.rb', line 12 def global_opts @global_opts end |
#node_opts ⇒ Object (readonly)
Returns the value of attribute node_opts.
12 13 14 |
# File 'lib/easy_json_matcher/node_generator.rb', line 12 def node_opts @node_opts end |
#validators ⇒ Object (readonly)
Returns the value of attribute validators.
12 13 14 |
# File 'lib/easy_json_matcher/node_generator.rb', line 12 def validators @validators end |
Instance Method Details
#contains_array(key:, opts: []) {|validator| ... } ⇒ Object
36 37 38 39 40 |
# File 'lib/easy_json_matcher/node_generator.rb', line 36 def contains_array(key:, opts: []) validator = ArrayGenerator.new(local_opts: opts, global_opts: global_opts) yield validator if block_given? validators[key] = validator.generate_array end |
#contains_node(key:, opts: []) {|generator| ... } ⇒ Object
30 31 32 33 34 |
# File 'lib/easy_json_matcher/node_generator.rb', line 30 def contains_node(key:, opts: []) generator = self.class.new(opts: opts, global_opts: global_opts) yield generator if block_given? validators[key] = generator.generate_node end |
#extract_opts(local:, global:) ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/easy_json_matcher/node_generator.rb', line 47 def extract_opts(local:, global:) conflicts = { required: :not_required } opts = global.map do |opt| if conflicts.keys.include? opt local.include?(conflicts[opt]) ? conflicts[opt] : opt else opt end end end |
#generate_node ⇒ Object
20 21 22 23 |
# File 'lib/easy_json_matcher/node_generator.rb', line 20 def generate_node strict = node_opts.delete(:strict) Node.new(opts: node_opts, strict: strict, validators: validators) end |
#has_attribute(key:, opts:) ⇒ Object
25 26 27 28 |
# File 'lib/easy_json_matcher/node_generator.rb', line 25 def has_attribute(key:, opts:) validator = AttributeGenerator.new(local_opts: opts, global_opts: global_opts) validators[key] = validator.generate_attribute end |
#has_schema(key:, name:) ⇒ Object
42 43 44 45 |
# File 'lib/easy_json_matcher/node_generator.rb', line 42 def has_schema(key:, name:) schema = SchemaLibrary.get_schema(name: name) validators[key] = schema end |