Skip to content

Commit

Permalink
Merge remote-tracking branches 'origin/schema_modules', 'origin/msn_b…
Browse files Browse the repository at this point in the history
…ase', 'origin/ref_registry', 'origin/doc' and 'origin/misc' into HEAD

#128
#130
#137
#131
#127
  • Loading branch information
notEthan committed Feb 12, 2021
5 parents 340e2ef + a7e5f3a + 1ce1ecb + c211904 + f000300 commit f3564ae
Show file tree
Hide file tree
Showing 21 changed files with 101 additions and 211 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -31,7 +31,7 @@ properties:
Using that schema, we instantiate a JSI::Schema to represent it:

```ruby
# this would usually load YAML or JSON; it's inlined for copypastability.
# this would usually load YAML or JSON; the schema object is inlined for copypastability.
contact_schema = JSI.new_schema({"description" => "A Contact", "type" => "object", "properties" => {"name" => {"type" => "string"}, "phone" => {"type" => "array", "items" => {"type" => "object", "properties" => {"location" => {"type" => "string"}, "number" => {"type" => "string"}}}}}})
```

Expand All @@ -54,7 +54,7 @@ nickname: big b
So, if we construct an instance like:

```ruby
# this would usually load YAML or JSON; it's inlined for copypastability.
# this would usually load YAML or JSON; the schema instance is inlined for copypastability.
bill = Contact.new_jsi({"name" => "bill", "phone" => [{"location" => "home", "number" => "555"}], "nickname" => "big b"})
# => #{<JSI (Contact)>
# "name" => "bill",
Expand Down
22 changes: 13 additions & 9 deletions lib/jsi/base.rb
Expand Up @@ -10,10 +10,13 @@ module JSI
#
# the JSI::Base class itself is not intended to be instantiated.
class Base
include Util::Memoize
include Enumerable
include PathedNode
include Schema::SchemaAncestorNode
include Util::Memoize

# not every JSI::Base is necessarily an Enumerable, but it's better to include Enumerable on
# the class than to conditionally extend the instance.
include Enumerable

# an exception raised when #[] is invoked on an instance which is not an array or hash
class CannotSubscriptError < StandardError
Expand Down Expand Up @@ -174,7 +177,8 @@ def initialize(instance,

if self.jsi_instance.respond_to?(:to_hash)
extend PathedHashNode
elsif self.jsi_instance.respond_to?(:to_ary)
end
if self.jsi_instance.respond_to?(:to_ary)
extend PathedArrayNode
end
end
Expand All @@ -191,8 +195,8 @@ def initialize(instance,
# the instance of the json-schema - the underlying JSON data used to instantiate this JSI
alias_method :jsi_instance, :jsi_node_content

# each is overridden by PathedHashNode or PathedArrayNode when appropriate. the base
# #each is not actually implemented, along with all the methods of Enumerable.
# each is overridden by PathedHashNode or PathedArrayNode when appropriate. the base #each
# is not actually implemented, along with all the methods of Enumerable.
def each
raise NoMethodError, "Enumerable methods and #each not implemented for instance that is not like a hash or array: #{jsi_instance.pretty_inspect.chomp}"
end
Expand Down Expand Up @@ -286,7 +290,7 @@ def []=(token, value)

# @return [Set<Module>] the set of JSI schema modules corresponding to the schemas that describe this JSI
def jsi_schema_modules
jsi_schemas.map(&:jsi_schema_module).to_set
jsi_schemas.map(&:jsi_schema_module).to_set.freeze
end

# yields the content of the underlying instance. the block must result in
Expand All @@ -297,7 +301,7 @@ def jsi_schema_modules
# in a (nondestructively) modified copy of this.
# @return [JSI::Base subclass the same as self] the modified copy of self
def jsi_modified_copy(&block)
if jsi_ptr.root?
if @jsi_ptr.root?
modified_document = @jsi_ptr.modified_document_copy(@jsi_document, &block)
self.class.new(Base::NOINSTANCE,
jsi_document: modified_document,
Expand Down Expand Up @@ -410,7 +414,7 @@ def jsi_fingerprint
jsi_document: jsi_document,
jsi_ptr: jsi_ptr,
# for instances in documents with schemas:
jsi_schema_base_uri: jsi_schema_base_uri,
jsi_schema_base_uri: is_a?(Schema) ? jsi_subschema_base_uri : jsi_schema_base_uri,
# only defined for JSI::Schema instances:
jsi_schema_instance_modules: is_a?(Schema) ? jsi_schema_instance_modules : nil,
}
Expand All @@ -430,7 +434,7 @@ def jsi_subinstance_schemas_memos
raise(Bug, 'jsi_subinstance_schemas_memos: not array or hash')
end
subschemas.map { |subschema| subschema.match_to_instance(value) }.inject(Set.new, &:|)
end.inject(Set.new, &:|)
end.inject(Set.new, &:|).freeze
end
end

Expand Down
128 changes: 0 additions & 128 deletions lib/jsi/base/to_rb.rb

This file was deleted.

6 changes: 3 additions & 3 deletions lib/jsi/jsi_coder.rb
Expand Up @@ -25,13 +25,13 @@ module JSI
# (represented as one json object) or an array of them (represented as a json
# array of json objects), indicated by the keyword argument `array`.
class JSICoder
# @param schema [JSI::Schema, JSI::SchemaModule, Class < JSI::Base] a schema, a JSI schema class, or
# a JSI schema module. #load will instantiate column data using the JSI schema represented.
# @param schema [#new_jsi] a Schema, JSI schema class, or JSI schema module. #load
# will instantiate column data using the JSI schemas represented.
# @param array [Boolean] whether the dumped data represent one instance of the schema,
# or an array of them. note that it may be preferable to simply use an array schema.
def initialize(schema, array: false)
unless schema.respond_to?(:new_jsi)
raise(ArgumentError, "not a JSI schema, class, or module: #{schema.inspect}")
raise(ArgumentError, "schema param does not respond to #new_jsi: #{schema.inspect}")
end
@schema = schema
@array = array
Expand Down
36 changes: 2 additions & 34 deletions lib/jsi/metaschema_node.rb
Expand Up @@ -22,17 +22,9 @@ module JSI
#
# a MetaschemaNode is extended with JSI::Schema when it represents a schema - this is the case when
# the metaschema is one of its schemas.
class MetaschemaNode
class MetaschemaNode < Base
autoload :BootstrapSchema, 'jsi/metaschema_node/bootstrap_schema'

include PathedNode
include Schema::SchemaAncestorNode
include Util::Memoize

# not every MetaschemaNode is necessarily an Enumerable, but it's better to include Enumerable on
# the class than to conditionally extend the instance.
include Enumerable

# @param jsi_document the document containing the metaschema
# @param jsi_ptr [JSI::JSON::Pointer] ptr to this MetaschemaNode in jsi_document
# @param metaschema_instance_modules [Set<Module>] modules which implement the functionality of the
Expand Down Expand Up @@ -112,7 +104,7 @@ def initialize(
jsi_schema_base_uri: bootstrap_schema.jsi_schema_base_uri,
)
end
end.to_set
end.to_set.freeze

@jsi_schemas.each do |schema|
extend schema.jsi_schema_module
Expand All @@ -132,11 +124,6 @@ def initialize(
end
end

# document containing the metaschema. see PathedNode#jsi_document.
attr_reader :jsi_document
# ptr to this metaschema node. see PathedNode#jsi_ptr.
attr_reader :jsi_ptr

# Set of modules to apply to schemas which are instances of (described by) the metaschema
attr_reader :metaschema_instance_modules

Expand Down Expand Up @@ -202,24 +189,6 @@ def jsi_modified_copy(&block)
MetaschemaNode.new(jsi_ptr.modified_document_copy(jsi_document, &block), our_initialize_params)
end

# @return [String]
def inspect
"\#<#{jsi_object_group_text.join(' ')} #{jsi_node_content.inspect}>"
end

def pretty_print(q)
q.text '#<'
q.text jsi_object_group_text.join(' ')
q.group_sub {
q.nest(2) {
q.breakable ' '
q.pp jsi_node_content
}
}
q.breakable ''
q.text '>'
end

# @private
# @return [Array<String>]
def jsi_object_group_text
Expand All @@ -239,7 +208,6 @@ def jsi_object_group_text
def jsi_fingerprint
{class: self.class, jsi_document: jsi_document}.merge(our_initialize_params)
end
include Util::FingerprintHash

private

Expand Down
6 changes: 3 additions & 3 deletions lib/jsi/metaschema_node/bootstrap_schema.rb
Expand Up @@ -59,14 +59,14 @@ def jsi_node_content

# @return [String]
def inspect
"\#<#{object_group_text.join(' ')} #{schema_content.inspect}>"
"\#<#{jsi_object_group_text.join(' ')} #{schema_content.inspect}>"
end

# pretty-prints a representation of self to the given printer
# @return [void]
def pretty_print(q)
q.text '#<'
q.text object_group_text.join(' ')
q.text jsi_object_group_text.join(' ')
q.group_sub {
q.nest(2) {
q.breakable ' '
Expand All @@ -79,7 +79,7 @@ def pretty_print(q)

# @private
# @return [Array<String>]
def object_group_text
def jsi_object_group_text
[
self.class.name || MetaschemaNode::BootstrapSchema.name,
"(#{metaschema_instance_modules.map(&:inspect).join(', ')})",
Expand Down
4 changes: 2 additions & 2 deletions lib/jsi/pathed_node.rb
Expand Up @@ -28,7 +28,7 @@ module PathedHashNode
# returns an Enumerator if no block is given.
#
# @yield [Object, Object] each key and value of this hash node
# @return [self, Enumerator]
# @return [self, Enumerator] returns an Enumerator if invoked without a block; otherwise self
def each(&block)
return to_enum(__method__) { jsi_node_content_hash_pubsend(:size) } unless block
if block.arity > 1
Expand Down Expand Up @@ -74,7 +74,7 @@ module PathedArrayNode
# returns an Enumerator if no block is given.
#
# @yield [Object] each element of this array node
# @return [self, Enumerator]
# @return [self, Enumerator] returns an Enumerator if invoked without a block; otherwise self
def each(&block)
return to_enum(__method__) { jsi_node_content_ary_pubsend(:size) } unless block
jsi_node_content_ary_pubsend(:each_index) { |i| yield(self[i]) }
Expand Down
4 changes: 2 additions & 2 deletions lib/jsi/schema.rb
Expand Up @@ -275,7 +275,7 @@ def describes_schema?
# on this schema's {#jsi_schema_module}
def jsi_schema_instance_modules
return @jsi_schema_instance_modules if instance_variable_defined?(:@jsi_schema_instance_modules)
return Set[]
return Set[].freeze
end

# @return [void]
Expand Down Expand Up @@ -381,7 +381,7 @@ def described_object_property_names
if schema_content.respond_to?(:to_hash) && schema_content['required'].respond_to?(:to_ary)
property_names.merge(schema_content['required'].to_ary)
end
end
end.freeze
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/jsi/schema/application/child_application.rb
Expand Up @@ -28,7 +28,7 @@ def subschemas_for_property_name(property_name)
subschemas << subschema(['additionalProperties'])
end
end
end
end.freeze
end
end

Expand All @@ -51,7 +51,7 @@ def subschemas_for_index(index)
subschemas << subschema(['items'])
end
end
end
end.freeze
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/jsi/schema/application/inplace_application.rb
Expand Up @@ -43,7 +43,7 @@ def match_to_instance(instance)
else
schemas << self
end
end
end.freeze
end
end
end

0 comments on commit f3564ae

Please sign in to comment.