Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
127 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# frozen_string_literal: true | ||
|
||
module JSI | ||
# a Set of JSI Schemas. always frozen. | ||
# | ||
# any schema instance is described by a set of schemas. | ||
class SchemaSet < ::Set | ||
class << self | ||
# builds a SchemaSet from a mutable Set which is added to by the given block | ||
# | ||
# @yield [Set] a Set to which the block may add schemas | ||
# @return [SchemaSet] | ||
def build | ||
mutable_set = Set.new | ||
yield mutable_set | ||
new(mutable_set) | ||
end | ||
end | ||
|
||
# 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. | ||
# | ||
# @param enum [#each] the schemas to be included in the SchemaSet, or items to be passed to the block | ||
# @yieldparam yields each element of enum for preprocessing into a Schema | ||
# @yieldreturn [JSI::Schema] | ||
# @raise [JSI::Schema::NotASchemaError] | ||
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 | ||
|
||
def inspect | ||
"#{self.class}[#{map(&:inspect).join(", ")}]" | ||
end | ||
|
||
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 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'test_helper' | ||
|
||
describe 'JSI::SchemaSet' do | ||
let(:schema_a) { JSI::JSONSchemaOrgDraft06.new_schema({'title' => 'A'}) } | ||
describe 'initialization' do | ||
describe '.new' do | ||
it 'initializes' do | ||
schema_set = JSI::SchemaSet.new([schema_a]) | ||
assert_equal(1, schema_set.size) | ||
end | ||
it 'errors given non-schemas' do | ||
err = assert_raises(JSI::Schema::NotASchemaError) { JSI::SchemaSet.new([3]) } | ||
assert_equal("JSI::SchemaSet initialized with non-schema objects:\n3", err.message) | ||
end | ||
end | ||
describe '.build' do | ||
it 'initializes' do | ||
schema_set = JSI::SchemaSet.build do |schemas| | ||
schemas << schema_a | ||
end | ||
assert_equal(1, schema_set.size) | ||
end | ||
it 'errors given non-schemas' do | ||
err = assert_raises(JSI::Schema::NotASchemaError) do | ||
JSI::SchemaSet.build do |schemas| | ||
schemas << 3 | ||
end | ||
end | ||
assert_equal("JSI::SchemaSet initialized with non-schema objects:\n3", err.message) | ||
end | ||
end | ||
end | ||
describe '#inspect' do | ||
it 'inspects' do | ||
inspect = JSI::SchemaSet[schema_a].inspect | ||
assert_equal(%q(JSI::SchemaSet[#{<JSI (JSI::JSONSchemaOrgDraft06) Schema> "title" => "A"}]), inspect) | ||
end | ||
end | ||
describe '#pretty_print' do | ||
it 'pretty prints' do | ||
pp = JSI::SchemaSet[schema_a].pretty_inspect | ||
assert_equal(%q(JSI::SchemaSet[#{<JSI (JSI::JSONSchemaOrgDraft06) Schema> "title" => "A"}]), pp.chomp) | ||
end | ||
end | ||
end |