Skip to content

Commit

Permalink
remove Schema#describes_array? and Schema#describes_hash? - these met…
Browse files Browse the repository at this point in the history
…hods are not too good and not too needed
  • Loading branch information
notEthan committed Sep 5, 2018
1 parent f6b810b commit 3e2cb59
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 55 deletions.
36 changes: 17 additions & 19 deletions lib/jsi/base.rb
Expand Up @@ -219,26 +219,24 @@ def self.module_for_schema(schema_object)
%Q(#<Module for Schema: #{schema_id}>)
end

if schema.describes_hash?
instance_method_modules = [m, Base, BaseArray, BaseHash]
instance_methods = instance_method_modules.map do |mod|
mod.instance_methods + mod.private_instance_methods
end.inject(Set.new, &:|)
accessors_to_define = schema.described_hash_property_names.map(&:to_s) - instance_methods.map(&:to_s)
accessors_to_define.each do |property_name|
define_method(property_name) do
if respond_to?(:[])
self[property_name]
else
raise(NoMethodError, "instance does not respond to []; cannot call reader `#{property_name}' for: #{pretty_inspect.chomp}")
end
instance_method_modules = [m, Base, BaseArray, BaseHash]
instance_methods = instance_method_modules.map do |mod|
mod.instance_methods + mod.private_instance_methods
end.inject(Set.new, &:|)
accessors_to_define = schema.described_hash_property_names.map(&:to_s) - instance_methods.map(&:to_s)
accessors_to_define.each do |property_name|
define_method(property_name) do
if respond_to?(:[])
self[property_name]
else
raise(NoMethodError, "instance does not respond to []; cannot call reader `#{property_name}' for: #{pretty_inspect.chomp}")
end
define_method("#{property_name}=") do |value|
if respond_to?(:[]=)
self[property_name] = value
else
raise(NoMethodError, "instance does not respond to []=; cannot call writer `#{property_name}=' for: #{pretty_inspect.chomp}")
end
end
define_method("#{property_name}=") do |value|
if respond_to?(:[]=)
self[property_name] = value
else
raise(NoMethodError, "instance does not respond to []=; cannot call writer `#{property_name}=' for: #{pretty_inspect.chomp}")
end
end
end
Expand Down
36 changes: 0 additions & 36 deletions lib/jsi/schema.rb
Expand Up @@ -148,42 +148,6 @@ def subschema_for_index(index_)
end
end

def describes_array?
memoize(:describes_array?) do
schema_node['type'] == 'array' ||
schema_node['items'] ||
schema_node['additionalItems'] ||
schema_node['default'].respond_to?(:to_ary) || # TODO make sure this is right
(schema_node['enum'].respond_to?(:to_ary) && schema_node['enum'].all? { |enum| enum.respond_to?(:to_ary) }) ||
schema_node['maxItems'] ||
schema_node['minItems'] ||
schema_node.key?('uniqueItems') ||
schema_node['oneOf'].respond_to?(:to_ary) &&
schema_node['oneOf'].all? { |someof_node| self.class.new(someof_node).describes_array? } ||
schema_node['allOf'].respond_to?(:to_ary) &&
schema_node['allOf'].all? { |someof_node| self.class.new(someof_node).describes_array? } ||
schema_node['anyOf'].respond_to?(:to_ary) &&
schema_node['anyOf'].all? { |someof_node| self.class.new(someof_node).describes_array? }
end
end
def describes_hash?
memoize(:describes_hash?) do
schema_node['type'] == 'object' ||
schema_node['required'].respond_to?(:to_ary) ||
schema_node['properties'].respond_to?(:to_hash) ||
schema_node['additionalProperties'] ||
schema_node['patternProperties'] ||
schema_node['default'].respond_to?(:to_hash) ||
(schema_node['enum'].respond_to?(:to_ary) && schema_node['enum'].all? { |enum| enum.respond_to?(:to_hash) }) ||
schema_node['oneOf'].respond_to?(:to_ary) &&
schema_node['oneOf'].all? { |someof_node| self.class.new(someof_node).describes_hash? } ||
schema_node['allOf'].respond_to?(:to_ary) &&
schema_node['allOf'].all? { |someof_node| self.class.new(someof_node).describes_hash? } ||
schema_node['anyOf'].respond_to?(:to_ary) &&
schema_node['anyOf'].all? { |someof_node| self.class.new(someof_node).describes_hash? }
end
end

def described_hash_property_names
memoize(:described_hash_property_names) do
Set.new.tap do |property_names|
Expand Down

0 comments on commit 3e2cb59

Please sign in to comment.