Skip to content

Commit

Permalink
Remove dynamically built and included Module and fallback on method_m…
Browse files Browse the repository at this point in the history
…issing (#93)
  • Loading branch information
nesaulov committed Apr 18, 2018
1 parent 671ab04 commit 9f89b2d
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 36 deletions.
1 change: 1 addition & 0 deletions lib/surrealist.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'oj'
require 'set'
require_relative 'surrealist/any'
require_relative 'surrealist/bool'
require_relative 'surrealist/builder'
Expand Down
36 changes: 0 additions & 36 deletions lib/surrealist/schema_definer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,6 @@ def call(klass, hash)
raise Surrealist::InvalidSchemaError, SCHEMA_TYPE_ERROR unless hash.is_a?(Hash)

Surrealist::VarsHelper.set_schema(klass, hash)
define_missing_methods(klass, hash) if klass < Surrealist::Serializer
end

private

# Defines all methods from the json_schema on Serializer instance in order to increase
# performance (comparing to using method_missing)
#
# @param [Object] klass class of the object where methods will be defined
#
# @param [Hash] hash the schema hash
def define_missing_methods(klass, hash)
methods = find_methods(hash)
klass.include(Module.new do
instance_exec do
methods.each do |method|
define_method method do
if (object = instance_variable_get('@object'))
object.public_send(method)
end
end
end
end
end)
end

# Takes out all keys from a hash
#
# @param [Hash] hash a hash to take keys from
#
# @return [Array] an array of keys
def find_methods(hash)
hash.each_with_object([]) do |(k, v), keys|
keys.push(k)
keys.concat(find_methods(v)) if v.is_a? Hash
end
end
end
end
Expand Down

0 comments on commit 9f89b2d

Please sign in to comment.