Class: Schemurai::Internal::Dialect

Inherits:
Object
  • Object
show all
Defined in:
lib/schemurai/dialect.rb

Defined Under Namespace

Classes: Keyword

Constant Summary collapse

TYPE =
1
ENUM =
2
COMBINER =
4
NUMBER =
8
STRING =
16
ARRAY =
32
OBJECT =
64

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, uri:, keywords:, ref_siblings:, format_assertion: false) ⇒ Dialect

Returns a new instance of Dialect.



44
45
46
47
48
49
50
51
# File 'lib/schemurai/dialect.rb', line 44

def initialize(name:, uri:, keywords:, ref_siblings:, format_assertion: false)
  @name = name
  @uri = uri
  @keywords = keywords.freeze
  @ref_siblings = ref_siblings
  @format_assertion = format_assertion
  freeze
end

Instance Attribute Details

#keywordsObject (readonly)

Returns the value of attribute keywords.



42
43
44
# File 'lib/schemurai/dialect.rb', line 42

def keywords
  @keywords
end

#nameObject (readonly)

Returns the value of attribute name.



42
43
44
# File 'lib/schemurai/dialect.rb', line 42

def name
  @name
end

#uriObject (readonly)

Returns the value of attribute uri.



42
43
44
# File 'lib/schemurai/dialect.rb', line 42

def uri
  @uri
end

Class Method Details

.register(dialect, default: false) ⇒ Object



21
22
23
24
25
# File 'lib/schemurai/dialect.rb', line 21

def register(dialect, default: false)
  registry[normalize_uri(dialect.uri)] = dialect
  @default = dialect if default
  dialect
end

.resolve(uri = nil) ⇒ Object



27
28
29
30
31
# File 'lib/schemurai/dialect.rb', line 27

def resolve(uri = nil)
  return @default if uri.nil?

  registry[normalize_uri(uri)]
end

Instance Method Details

#draft2019?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/schemurai/dialect.rb', line 65

def draft2019?
  name == :draft2019_09
end

#draft2020?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/schemurai/dialect.rb', line 69

def draft2020?
  name == :draft2020_12
end

#draft7?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/schemurai/dialect.rb', line 61

def draft7?
  name == :draft7
end

#each_subschema(schema) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/schemurai/dialect.rb', line 79

def each_subschema(schema)
  return enum_for(__method__, schema) unless block_given?
  return unless schema.is_a?(Hash)
  return if schema.key?("$ref") && !ref_siblings?

  schema.each do |keyword, value|
    specification = keywords[keyword]
    next unless specification&.subschema_shape

    case specification.subschema_shape
    when :single
      yield value, [keyword] if schema?(value)
    when :single_or_list
      if value.is_a?(Array)
        value.each_with_index { |child, index| yield child, [keyword, index] if schema?(child) }
      elsif schema?(value)
        yield value, [keyword]
      end
    when :list
      Array(value).each_with_index { |child, index| yield child, [keyword, index] if schema?(child) }
    when :map
      value.each { |name, child| yield child, [keyword, name] if schema?(child) } if value.is_a?(Hash)
    when :dependencies
      if value.is_a?(Hash)
        value.each do |name, child|
          yield child, [keyword, name] if schema?(child)
        end
      end
    end
  end
end

#format_assertion?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/schemurai/dialect.rb', line 57

def format_assertion?
  @format_assertion
end

#keyword_mask(schema) ⇒ Object



73
74
75
76
77
# File 'lib/schemurai/dialect.rb', line 73

def keyword_mask(schema)
  schema.each_key.reduce(0) do |mask, keyword|
    mask | (keywords[keyword]&.mask || 0)
  end
end

#ref_siblings?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/schemurai/dialect.rb', line 53

def ref_siblings?
  @ref_siblings
end