Class: JSI::SchemaSet
- Inherits:
-
Set
- Object
- Set
- JSI::SchemaSet
- 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
-
.build {|Set| ... } ⇒ SchemaSet
builds a SchemaSet from a mutable Set which is added to by the given block.
-
.ensure_schema_set(schemas) ⇒ SchemaSet
ensures the given param becomes a SchemaSet.
Instance Method Summary collapse
-
#child_applicator_schemas(token, instance) ⇒ JSI::SchemaSet
a set of child applicator subschemas of each schema in this set which apply to the child of the given instance on the given token.
-
#each_child_applicator_schema(token, instance) {|JSI::Schema| ... } ⇒ nil, Enumerator
yields each child applicator schema which applies to the child of the given instance on the given token.
-
#each_inplace_applicator_schema(instance) {|JSI::Schema| ... } ⇒ nil, Enumerator
yields each inplace applicator schema which applies to the given instance.
-
#initialize(enum) {|yields| ... } ⇒ SchemaSet
constructor
initializes a SchemaSet from the given enum and freezes it.
-
#inplace_applicator_schemas(instance) ⇒ JSI::SchemaSet
a set of inplace applicator schemas of each schema in this set which apply to the given instance.
- #inspect ⇒ String (also: #to_s)
-
#instance_valid?(instance) ⇒ Boolean
whether the given instance is valid against our schemas.
-
#instance_validate(instance) ⇒ JSI::Validation::Result
validates the given instance against our schemas.
-
#new_jsi(instance, uri: nil) ⇒ JSI::Base subclass
instantiates the given instance as a JSI.
- #pretty_print(q) ⇒ Object
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.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/jsi/schema_set.rb', line 44 def initialize(enum, &block) if enum.is_a?(Schema) raise(ArgumentError, [ "#{SchemaSet} initialized with a #{Schema}", "you probably meant to pass that to #{SchemaSet}[]", "or to wrap that schema in a Set or Array for #{SchemaSet}.new", "given: #{enum.pretty_inspect.chomp}", ].join("\n")) end super not_schemas = reject { |s| s.is_a?(Schema) } if !not_schemas.empty? raise(Schema::NotASchemaError, [ "#{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
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.
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
#child_applicator_schemas(token, instance) ⇒ JSI::SchemaSet
a set of child applicator subschemas of each schema in this set which apply to the child of the given instance on the given token. (see JSI::Schema::Application::ChildApplication#child_applicator_schemas)
123 124 125 |
# File 'lib/jsi/schema_set.rb', line 123 def child_applicator_schemas(token, instance) SchemaSet.new(each_child_applicator_schema(token, instance)) end |
#each_child_applicator_schema(token, instance) {|JSI::Schema| ... } ⇒ nil, Enumerator
yields each child applicator schema which applies to the child of the given instance on the given token.
133 134 135 136 137 138 139 140 141 |
# File 'lib/jsi/schema_set.rb', line 133 def each_child_applicator_schema(token, instance, &block) return to_enum(__method__, token, instance) unless block each do |schema| schema.each_child_applicator_schema(token, instance, &block) end nil end |
#each_inplace_applicator_schema(instance) {|JSI::Schema| ... } ⇒ nil, Enumerator
yields each inplace applicator schema which applies to the given instance.
107 108 109 110 111 112 113 114 115 |
# File 'lib/jsi/schema_set.rb', line 107 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)
98 99 100 |
# File 'lib/jsi/schema_set.rb', line 98 def inplace_applicator_schemas(instance) SchemaSet.new(each_inplace_applicator_schema(instance)) end |
#inspect ⇒ String Also known as: to_s
160 161 162 |
# File 'lib/jsi/schema_set.rb', line 160 def inspect "#{self.class}[#{map(&:inspect).join(", ")}]" end |
#instance_valid?(instance) ⇒ Boolean
whether the given instance is valid against our schemas
155 156 157 |
# File 'lib/jsi/schema_set.rb', line 155 def instance_valid?(instance) all? { |schema| schema.instance_valid?(instance) } end |
#instance_validate(instance) ⇒ JSI::Validation::Result
validates the given instance against our schemas
147 148 149 150 |
# File 'lib/jsi/schema_set.rb', line 147 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.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/jsi/schema_set.rb', line 78 def new_jsi(instance, uri: nil ) applied_schemas = inplace_applicator_schemas(instance) jsi_class = JSI::SchemaClasses.class_for_schemas(applied_schemas, includes: SchemaClasses.includes_for(instance), ) jsi = jsi_class.new(instance, jsi_schema_base_uri: uri, ) jsi end |
#pretty_print(q) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/jsi/schema_set.rb', line 166 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 |