class Unparser::Adamantium::MethodBuilder

Build the memoized method

def assert_arity(arity)

def assert_arity(arity)
  if arity.nonzero?
    fail InvalidArityError.new(@descendant, @method_name, arity)
  end
end

def call

Other tags:
    Api: - public

Returns:
  • (UnboundMethod) -
def call
  remove_original_method
  create_memoized_method
  set_method_visibility
  @original_method
end

def create_memoized_method

def create_memoized_method
  name =   @method_name
  method = @original_method
  @descendant.module_eval do
    define_method(name) do |&block|
      fail BlockNotAllowedError.new(self.class, name) if block
      memoized_method_cache.fetch(name) do
        method.bind(self).call.freeze
      end
    end
  end
end

def initialize(descendant, method_name)

Other tags:
    Api: - private

Returns:
  • (undefined) -

Parameters:
  • method_name (Symbol) --
  • descendant (Module) --
def initialize(descendant, method_name)
  @descendant          = descendant
  @method_name         = method_name
  @original_visibility = visibility
  @original_method     = @descendant.instance_method(@method_name)
  assert_arity(@original_method.arity)
end

def remove_original_method

def remove_original_method
  name = @method_name
  @descendant.module_eval { undef_method(name) }
end

def set_method_visibility

def set_method_visibility
  @descendant.__send__(@original_visibility, @method_name)
end

def visibility

def visibility
  if    @descendant.private_method_defined?(@method_name)   then :private
  elsif @descendant.protected_method_defined?(@method_name) then :protected
  else
    :public
  end
end