Skip to content

Commit

Permalink
Merge c5f07b5 into 9c2bf1d
Browse files Browse the repository at this point in the history
  • Loading branch information
notEthan committed Mar 17, 2021
2 parents 9c2bf1d + c5f07b5 commit 5d0e05a
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/jsi/metaschema_node.rb
Expand Up @@ -43,7 +43,7 @@ def initialize(

self.jsi_document = jsi_document
self.jsi_ptr = jsi_ptr
@metaschema_instance_modules = metaschema_instance_modules
@metaschema_instance_modules = Util.ensure_module_set(metaschema_instance_modules)
@metaschema_root_ptr = metaschema_root_ptr
@root_schema_ptr = root_schema_ptr

Expand Down
4 changes: 1 addition & 3 deletions lib/jsi/schema.rb
Expand Up @@ -317,9 +317,7 @@ def jsi_schema_instance_modules

# @return [void]
def jsi_schema_instance_modules=(jsi_schema_instance_modules)
raise(TypeError) unless jsi_schema_instance_modules.is_a?(Set)
raise(TypeError) unless jsi_schema_instance_modules.all? { |m| m.is_a?(Module) }
@jsi_schema_instance_modules = jsi_schema_instance_modules
@jsi_schema_instance_modules = Util.ensure_module_set(jsi_schema_instance_modules)
end

# a resource containing this schema.
Expand Down
1 change: 1 addition & 0 deletions lib/jsi/schema_classes.rb
Expand Up @@ -64,6 +64,7 @@ def class_for_schemas(schema_objects)
# @param modules [Set<Module>] metaschema instance modules
# @return [Class] a subclass of MetaschemaNode::BootstrapSchema with the given modules included
def bootstrap_schema_class(modules)
modules = Util.ensure_module_set(modules)
jsi_memoize(__method__, modules) do |modules|
Class.new(MetaschemaNode::BootstrapSchema).instance_exec(modules) do |modules|
define_singleton_method(:metaschema_instance_modules) { modules }
Expand Down
15 changes: 15 additions & 0 deletions lib/jsi/schema_set.rb
Expand Up @@ -15,6 +15,21 @@ def build
yield mutable_set
new(mutable_set)
end

# ensures the given param becomes a SchemaSet. returns the param if it is already SchemaSet, otherwise
# initializes a SchemaSet from it.
#
# @param schemas [SchemaSet, Enumerable] the object to ensure becomes a SchemaSet
# @return [SchemaSet] the given SchemaSet, or a SchemaSet initialized from the given Enumerable
# @raise [ArgumentError] when the schemas param is not an Enumerable
# @raise [Schema::NotASchemaError] when the schemas param contains objects which are not Schemas
def ensure_schema_set(schemas)
if schemas.is_a?(SchemaSet)
schemas
else
new(schemas)
end
end
end

# initializes a SchemaSet from the given enum and freezes it.
Expand Down
25 changes: 25 additions & 0 deletions lib/jsi/util.rb
Expand Up @@ -53,6 +53,31 @@ def deep_stringify_symbol_keys(object)
end
end

# ensures the given param becomes a frozen Set of Modules.
# returns the param if it is already that, otherwise initializes and freezes such a Set.
#
# @param modules [Set, Enumerable] the object to ensure becomes a frozen Set of Modules
# @return [SchemaSet] the given SchemaSet, or a SchemaSet initialized from the given Enumerable
# @raise [ArgumentError] when the modules param is not an Enumerable
# @raise [Schema::NotASchemaError] when the modules param contains objects which are not Schemas
def ensure_module_set(modules)
if modules.is_a?(Set) && modules.frozen?
modules
else
set = Set.new(modules).freeze

not_modules = set.reject { |s| s.is_a?(Module) }
if !not_modules.empty?
raise(TypeError, [
"ensure_module_set give non-Module objects:",
*not_modules.map { |ns| ns.pretty_inspect.chomp },
].join("\n"))
end

set
end
end

# this is the Y-combinator, which allows anonymous recursive functions. for a simple example,
# to define a recursive function to return the length of an array:
#
Expand Down
2 changes: 1 addition & 1 deletion lib/schemas/json-schema.org/draft-04/schema.rb
Expand Up @@ -4,7 +4,7 @@ module JSI
schema_id = 'http://json-schema.org/draft-04/schema'
schema_content = ::JSON.parse(File.read(::JSON::Validator.validators[schema_id].metaschema))
JSONSchemaOrgDraft04 = MetaschemaNode.new(schema_content,
metaschema_instance_modules: Set[JSI::Schema::Draft04].freeze,
metaschema_instance_modules: [JSI::Schema::Draft04],
).tap(&:register_schema).jsi_schema_module

# the JSI schema module for http://json-schema.org/draft-04/schema
Expand Down
2 changes: 1 addition & 1 deletion lib/schemas/json-schema.org/draft-06/schema.rb
Expand Up @@ -4,7 +4,7 @@ module JSI
schema_id = 'http://json-schema.org/draft/schema' # I don't know why this is not http://json-schema.org/draft-06/schema
schema_content = ::JSON.parse(File.read(::JSON::Validator.validators[schema_id].metaschema))
JSONSchemaOrgDraft06 = MetaschemaNode.new(schema_content,
metaschema_instance_modules: Set[JSI::Schema::Draft06].freeze,
metaschema_instance_modules: [JSI::Schema::Draft06],
).tap(&:register_schema).jsi_schema_module

# the JSI schema module for http://json-schema.org/draft-06/schema
Expand Down
2 changes: 1 addition & 1 deletion test/metaschema_node_test.rb
Expand Up @@ -2,7 +2,7 @@

describe JSI::MetaschemaNode do
let(:metaschema_instance_modules) do
Set[
[
JSI::Schema,
JSI::Schema::Application::InplaceApplication,
JSI::Schema::Application::ChildApplication,
Expand Down
12 changes: 12 additions & 0 deletions test/schema_set_test.rb
Expand Up @@ -32,6 +32,18 @@
end
end
end
describe '.ensure_schema_set' do
it 'ensures each thing is a schema set' do
schemas = [schema_a]
schema_set = JSI::SchemaSet.new(schemas)
assert_equal(schema_set.object_id, JSI::SchemaSet.ensure_schema_set(schema_set).object_id)
assert_equal(schema_set, JSI::SchemaSet.ensure_schema_set(schemas))
assert_is_a(JSI::SchemaSet, JSI::SchemaSet.ensure_schema_set(schemas))
assert_raises(JSI::Schema::NotASchemaError) { JSI::SchemaSet.ensure_schema_set([3]) }
# TypeError seems better but ruby's Set raises ArgumentError
assert_raises(ArgumentError) { JSI::SchemaSet.ensure_schema_set(3) }
end
end
describe '#inspect' do
it 'inspects' do
inspect = JSI::SchemaSet[schema_a].inspect
Expand Down

0 comments on commit 5d0e05a

Please sign in to comment.