Class: EasyJSONMatcher::SchemaGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_json_matcher/schema_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts: {}, global_opts: {}) {|_self| ... } ⇒ SchemaGenerator

Returns a new instance of SchemaGenerator.

Yields:

  • (_self)

Yield Parameters:



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

def initialize(opts: {}, global_opts: {})
  @name = opts.delete(:key)
  @options = opts
  @glob_opts = global_opts
  yield self if block_given?
end

Instance Attribute Details

#glob_optsObject (readonly)

Returns the value of attribute glob_opts.



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

def glob_opts
  @glob_opts
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#nodeObject (readonly)

Returns the value of attribute node.



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

def node
  @node
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#_create_validator(key, opts) ⇒ Object



92
93
94
# File 'lib/easy_json_matcher/schema_generator.rb', line 92

def _create_validator(key, opts)
  ValidatorFactory.get_instance type: opts[:type], opts: _validator_opts(key, opts)
end

#_node_generator(opts = {}) ⇒ Object



96
97
98
# File 'lib/easy_json_matcher/schema_generator.rb', line 96

def _node_generator(opts = {})
 self.class.new opts: opts, global_opts: glob_opts
end

#_prep_schema_opts(schema_name, opts) ⇒ Object

 Private methods #########################################



77
78
79
80
81
# File 'lib/easy_json_matcher/schema_generator.rb', line 77

def _prep_schema_opts(schema_name, opts)
  opts[:type] = :schema
  opts[:name] = schema_name
  opts
end

#_set_validator_key(validator, key) ⇒ Object



83
84
85
# File 'lib/easy_json_matcher/schema_generator.rb', line 83

def _set_validator_key(validator, key)
  validator.key = key
end

#_validator_opts(key, opts) ⇒ Object



87
88
89
90
# File 'lib/easy_json_matcher/schema_generator.rb', line 87

def _validator_opts(key, opts)
  opts[:key] = key
  glob_opts.merge(opts)
end

#contains_array(key:, opts: {}) {|array_validator| ... } ⇒ Object

Yields:

  • (array_validator)


53
54
55
56
57
58
# File 'lib/easy_json_matcher/schema_generator.rb', line 53

def contains_array(key:, opts: {})
  opts = opts.merge!({type: :array})
  array_validator = _create_validator(key, opts)
  yield array_validator if block_given?
  node.add_validator array_validator
end

#contains_node(key:, opts: {}) {|generator| ... } ⇒ Object

Methods for adding specific attribute types ##############

Yields:

  • (generator)


23
24
25
26
27
# File 'lib/easy_json_matcher/schema_generator.rb', line 23

def contains_node(key:, opts: {})
  generator = _node_generator _validator_opts(key, opts)
  yield generator if block_given?
  node.add_validator generator.generate_schema
end

#generate_schemaObject

Methods for generating the schema #########################



66
67
68
# File 'lib/easy_json_matcher/schema_generator.rb', line 66

def generate_schema
  node
end

#has_attribute(key:, opts: {}) ⇒ Object



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

def has_attribute(key:, opts: {})
  node.add_validator(_create_validator(key, opts))
end

#has_boolean(key:, opts: {}) ⇒ Object



29
30
31
# File 'lib/easy_json_matcher/schema_generator.rb', line 29

def has_boolean(key:, opts: {})
  has_attribute(key: key, opts: opts.merge({type: :boolean}))
end

#has_date(key:, opts: {}) ⇒ Object



37
38
39
# File 'lib/easy_json_matcher/schema_generator.rb', line 37

def has_date(key:, opts: {})
  has_attribute(key: key, opts: opts.merge({type: :date}))
end

#has_number(key:, opts: {}) ⇒ Object



33
34
35
# File 'lib/easy_json_matcher/schema_generator.rb', line 33

def has_number(key: , opts: {})
  has_attribute(key: key, opts: opts.merge({type: :number}))
end

#has_object(key:, opts: {}) ⇒ Object



41
42
43
# File 'lib/easy_json_matcher/schema_generator.rb', line 41

def has_object(key:, opts: {})
  has_attribute(key: key, opts: opts.merge({type: :object}))
end

#has_schema(key:, opts: {}) ⇒ Object



60
61
62
# File 'lib/easy_json_matcher/schema_generator.rb', line 60

def has_schema(key:, opts: {})
  has_attribute(key: key, opts: opts.merge({type: :schema}))
end

#has_string(key:, opts: {}) ⇒ Object



49
50
51
# File 'lib/easy_json_matcher/schema_generator.rb', line 49

def has_string(key:, opts: {})
  has_attribute(key: key, opts: opts.merge({type: :string}))
end

#has_value(key:, opts: {}) ⇒ Object



45
46
47
# File 'lib/easy_json_matcher/schema_generator.rb', line 45

def has_value(key:, opts: {})
  has_attribute(key: key, opts: opts.merge({type: :value}))
end

#register(as:) ⇒ Object



70
71
72
73
# File 'lib/easy_json_matcher/schema_generator.rb', line 70

def register(as:)
  SchemaLibrary.add_schema(name: as, schema: generate_schema)
  generate_schema
end