Class: JSON::SchemaBuilder::Schema

Inherits:
Object
  • Object
show all
Includes:
Validation
Defined in:
lib/json/schema_builder/schema.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Validation

#fully_validate, #validate, #validate!

Constructor Details

#initialize(hash = {}, entities = nil) ⇒ Schema

Returns a new instance of Schema.



11
12
13
14
# File 'lib/json/schema_builder/schema.rb', line 11

def initialize(hash = {}, entities = nil)
  @data = hash.with_indifferent_access
  @entities = Array(entities)
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



8
9
10
# File 'lib/json/schema_builder/schema.rb', line 8

def data
  @data
end

#entitiesObject

Returns the value of attribute entities.



8
9
10
# File 'lib/json/schema_builder/schema.rb', line 8

def entities
  @entities
end

Instance Method Details

#_deep_merge(this, other) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/json/schema_builder/schema.rb', line 40

def _deep_merge(this, other)
  this.deep_merge(other) do |current_key, this_value, other_value|
    if current_key == "anyOf"
      _deep_merge_any_of(Array(this_value), Array(other_value))
    elsif this_value.is_a?(::Array) && other_value.is_a?(::Array)
      this_value + other_value
    else
      other_value
    end
  end
end

#_deep_merge_any_of(this, other) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/json/schema_builder/schema.rb', line 52

def _deep_merge_any_of(this, other)
  object = self.class.new
  combined = []

  (this + other).each do |item|
    if item[:type].to_sym == :object
      object.data = object._deep_merge(object.data, item)
    else
      combined << item
    end
  end

  combined << object.data unless object.data.empty?
  combined.uniq
end

#fragmentsObject



20
21
22
23
24
25
26
27
28
# File 'lib/json/schema_builder/schema.rb', line 20

def fragments
  fragment_map = Hash.new { |hash, key| hash[key] = [] }
  entities.map(&:fragments).each do |entity_fragments|
    entity_fragments.each do |fragment, entity|
      fragment_map[fragment] += entity
    end
  end
  fragment_map
end

#merge(schema) ⇒ Object



30
31
32
# File 'lib/json/schema_builder/schema.rb', line 30

def merge(schema)
  self.class.new _deep_merge(data, schema.data), entities + schema.entities
end

#merge!(schema) ⇒ Object



34
35
36
37
38
# File 'lib/json/schema_builder/schema.rb', line 34

def merge!(schema)
  @data = _deep_merge(data, schema.data)
  @entities += schema.entities
  self
end

#optionsObject



16
17
18
# File 'lib/json/schema_builder/schema.rb', line 16

def options
  JSON::SchemaBuilder.options.to_h
end