Skip to content

Commit

Permalink
Merge 26f8344 into f6b810b
Browse files Browse the repository at this point in the history
  • Loading branch information
notEthan committed Sep 3, 2018
2 parents f6b810b + 26f8344 commit 72a4c05
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions lib/jsi/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,31 @@ def initialize(schema_object)
if schema_object.is_a?(JSI::Schema)
raise(TypeError, "will not instantiate Schema from another Schema: #{schema_object.pretty_inspect.chomp}")
elsif schema_object.is_a?(JSI::Base)
@schema_object = JSI.deep_stringify_symbol_keys(schema_object.deref)
@schema_node = @schema_object.instance
@schema_jsi = JSI.deep_stringify_symbol_keys(schema_object.deref)
@schema_node = @schema_jsi.instance
elsif schema_object.is_a?(JSI::JSON::HashNode)
@schema_object = nil
@schema_jsi = nil
@schema_node = JSI.deep_stringify_symbol_keys(schema_object.deref)
elsif schema_object.respond_to?(:to_hash)
@schema_object = nil
@schema_jsi = nil
@schema_node = JSI::JSON::Node.new_by_type(JSI.deep_stringify_symbol_keys(schema_object), [])
else
raise(TypeError, "cannot instantiate Schema from: #{schema_object.pretty_inspect.chomp}")
end
if @schema_object
define_singleton_method(:instance) { schema_node } # aka schema_object.instance
define_singleton_method(:schema) { schema_object.schema }
if @schema_jsi
define_singleton_method(:instance) { schema_node } # aka schema_jsi.instance
define_singleton_method(:schema) { schema_jsi.schema }
extend BaseHash
else
define_singleton_method(:[]) { |*a, &b| schema_node.public_send(:[], *a, &b) }
end
end
attr_reader :schema_node

attr_reader :schema_node, :schema_jsi

# refers to the JSI of the schema (#schema_jsi) if known; otherwise the schema node (#schema_node)
def schema_object
@schema_object || @schema_node
@schema_jsi || @schema_node
end

def schema_id
Expand Down

0 comments on commit 72a4c05

Please sign in to comment.