Class: JSI::SchemaSet

Inherits:
Set
  • Object
show all
Defined in:
lib/jsi/schema_set.rb

Overview

a Set of JSI Schemas. always frozen.

any schema instance is described by a set of schemas.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enum) {|yields| ... } ⇒ SchemaSet

initializes a SchemaSet from the given enum and freezes it.

if a block is given, each element of the enum is passed to it, and the result must be a Schema. if no block is given, the enum must contain only Schemas.

Parameters:

  • enum (#each)

    the schemas to be included in the SchemaSet, or items to be passed to the block

Yield Parameters:

  • yields

    each element of enum for preprocessing into a Schema

Yield Returns:

Raises:


44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/jsi/schema_set.rb', line 44

def initialize(enum, &block)
  super

  not_schemas = reject { |s| s.is_a?(Schema) }
  if !not_schemas.empty?
    raise(Schema::NotASchemaError, [
      "JSI::SchemaSet initialized with non-schema objects:",
      *not_schemas.map { |ns| ns.pretty_inspect.chomp },
    ].join("\n"))
  end

  freeze
end

Class Method Details

.build {|Set| ... } ⇒ SchemaSet

builds a SchemaSet from a mutable Set which is added to by the given block

Yields:

  • (Set)

    a Set to which the block may add schemas

Returns:


13
14
15
16
17
# File 'lib/jsi/schema_set.rb', line 13

def build
  mutable_set = Set.new
  yield mutable_set
  new(mutable_set)
end

.ensure_schema_set(schemas) ⇒ SchemaSet

ensures the given param becomes a SchemaSet. returns the param if it is already SchemaSet, otherwise initializes a SchemaSet from it.

Parameters:

  • schemas (SchemaSet, Enumerable)

    the object to ensure becomes a SchemaSet

Returns:

  • (SchemaSet)

    the given SchemaSet, or a SchemaSet initialized from the given Enumerable

Raises:

  • (ArgumentError)

    when the schemas param is not an Enumerable

  • (Schema::NotASchemaError)

    when the schemas param contains objects which are not Schemas


26
27
28
29
30
31
32
# File 'lib/jsi/schema_set.rb', line 26

def ensure_schema_set(schemas)
  if schemas.is_a?(SchemaSet)
    schemas
  else
    new(schemas)
  end
end

Instance Method Details

#each_inplace_applicator_schema(instance) {|JSI::Schema| ... } ⇒ nil, Enumerator

yields each inplace applicator schema which applies to the given instance.

Parameters:

  • instance (Object)

    the instance to check any applicators against

Yields:

Returns:

  • (nil, Enumerator)

    an Enumerator if invoked without a block; otherwise nil


95
96
97
98
99
100
101
102
103
# File 'lib/jsi/schema_set.rb', line 95

def each_inplace_applicator_schema(instance, &block)
  return to_enum(__method__, instance) unless block

  each do |schema|
    schema.each_inplace_applicator_schema(instance, &block)
  end

  nil
end

#inplace_applicator_schemas(instance) ⇒ JSI::SchemaSet

a set of inplace applicator schemas of each schema in this set which apply to the given instance. (see JSI::Schema::Application::InplaceApplication#inplace_applicator_schemas)

Parameters:

  • instance (Object)

    the instance to check any applicators against

Returns:


86
87
88
# File 'lib/jsi/schema_set.rb', line 86

def inplace_applicator_schemas(instance)
  SchemaSet.new(each_inplace_applicator_schema(instance))
end

#inspectString

Returns:

  • (String)

122
123
124
# File 'lib/jsi/schema_set.rb', line 122

def inspect
  "#{self.class}[#{map(&:inspect).join(", ")}]"
end

#instance_valid?(instance) ⇒ Boolean

whether the given instance is valid against our schemas

Parameters:

  • instance (Object)

    the instance to validate against our schemas

Returns:

  • (Boolean)

117
118
119
# File 'lib/jsi/schema_set.rb', line 117

def instance_valid?(instance)
  all? { |schema| schema.instance_valid?(instance) }
end

#instance_validate(instance) ⇒ JSI::Validation::Result

validates the given instance against our schemas

Parameters:

  • instance (Object)

    the instance to validate against our schemas

Returns:


109
110
111
112
# File 'lib/jsi/schema_set.rb', line 109

def instance_validate(instance)
  results = map { |schema| schema.instance_validate(instance) }
  results.inject(Validation::FullResult.new, &:merge).freeze
end

#new_jsi(instance, uri: nil) ⇒ JSI::Base subclass

instantiates the given instance as a JSI. its schemas are inplace applicators matched from the schemas in this SchemaSet which apply to the given instance.

Parameters:

  • instance (Object)

    the JSON Schema instance to be represented as a JSI

  • uri (nil, #to_str, Addressable::URI) (defaults to: nil)

    for an instance document containing schemas, this is the URI of the document, whether or not the document is itself a schema. in the normal case where the document does not contain any schemas, uri has no effect. schemas within the document use this uri as the base URI to resolve relative URIs. the resulting JSI may be registered with a JSI::SchemaRegistry (see JSI.schema_registry).

Returns:

  • (JSI::Base subclass)

    a JSI whose instance is the given instance and whose schemas are inplace applicators matched to the instance from the schemas in this set.


69
70
71
72
73
74
75
76
77
78
79
# File 'lib/jsi/schema_set.rb', line 69

def new_jsi(instance,
    uri: nil
)
  applied_schemas = inplace_applicator_schemas(instance)

  jsi = JSI::SchemaClasses.class_for_schemas(applied_schemas).new(instance,
    jsi_schema_base_uri: uri,
  )

  jsi
end

#pretty_print(q) ⇒ Object


126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/jsi/schema_set.rb', line 126

def pretty_print(q)
  q.text self.class.to_s
  q.text '['
  q.group_sub {
    q.nest(2) {
      q.breakable('')
      q.seplist(self, nil, :each) { |e|
        q.pp e
      }
    }
  }
  q.breakable ''
  q.text ']'
end