Class: EasyJSONMatcher::NodeGenerator

Inherits:
Object
  • Object
show all
Includes:
AttributeTypeMethods
Defined in:
lib/easy_json_matcher/node_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AttributeTypeMethods

#has_boolean, #has_booleans, #has_date, #has_dates, #has_number, #has_numbers, #has_object, #has_objects, #has_string, #has_strings, #has_value, #has_values, #mass_assign

Constructor Details

#initialize(opts: [], global_opts: [], **args) ⇒ NodeGenerator

Returns a new instance of NodeGenerator.



10
11
12
13
14
15
# File 'lib/easy_json_matcher/node_generator.rb', line 10

def initialize(opts: [], global_opts: [], **args)
  super(**args)
  @validators = {} 
  @node_opts = extract_opts(local: opts, global: global_opts)
  @global_opts = global_opts
end

Instance Attribute Details

#array_optsObject (readonly)

Returns the value of attribute array_opts.



8
9
10
# File 'lib/easy_json_matcher/node_generator.rb', line 8

def array_opts
  @array_opts
end

#attribute_optsObject (readonly)

Returns the value of attribute attribute_opts.



8
9
10
# File 'lib/easy_json_matcher/node_generator.rb', line 8

def attribute_opts
  @attribute_opts
end

#global_optsObject (readonly)

Returns the value of attribute global_opts.



8
9
10
# File 'lib/easy_json_matcher/node_generator.rb', line 8

def global_opts
  @global_opts
end

#node_optsObject (readonly)

Returns the value of attribute node_opts.



8
9
10
# File 'lib/easy_json_matcher/node_generator.rb', line 8

def node_opts
  @node_opts
end

#validatorsObject (readonly)

Returns the value of attribute validators.



8
9
10
# File 'lib/easy_json_matcher/node_generator.rb', line 8

def validators
  @validators
end

Instance Method Details

#contains_array(key:, opts: [], &block) ⇒ Object



33
34
35
36
37
# File 'lib/easy_json_matcher/node_generator.rb', line 33

def contains_array(key:, opts: [], &block)
  validator = array_generator.new(local_opts: opts, global_opts: global_opts)
  validator.instance_eval &block if block_given?
  validators[key] = validator.generate_array
end

#contains_node(key:, opts: [], &block) ⇒ Object



27
28
29
30
31
# File 'lib/easy_json_matcher/node_generator.rb', line 27

def contains_node(key:, opts: [], &block)
  generator = self.class.new(opts: opts, global_opts: global_opts)
  generator.instance_eval &block if block_given?
  validators[key] = generator.generate_node
end

#extract_opts(local:, global:) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/easy_json_matcher/node_generator.rb', line 44

def extract_opts(local:, global:)
  conflicts = { required: :not_required }
  global.map do |opt| 
    if conflicts.keys.include? opt
      local.include?(conflicts[opt]) ? conflicts[opt] : opt
    else
      opt
    end
  end
end

#generate_nodeObject



17
18
19
20
# File 'lib/easy_json_matcher/node_generator.rb', line 17

def generate_node
  strict = node_opts.delete(:strict)
  node.new(opts: node_opts, strict: strict, validators: validators)
end

#has_attribute(key:, opts: []) ⇒ Object



22
23
24
25
# File 'lib/easy_json_matcher/node_generator.rb', line 22

def has_attribute(key:, opts: [])
  validator = attribute_generator.new(local_opts: opts, global_opts: global_opts)
  validators[key] = validator.generate_attribute
end

#has_schema(key:, name:) ⇒ Object



39
40
41
42
# File 'lib/easy_json_matcher/node_generator.rb', line 39

def has_schema(key:, name:)
  schema = schema_library.get_schema(name: name)
  validators[key] = schema
end