Module: Mongoid::Listable::Extensions::Object

Defined in:
lib/mongoid/listable/extensions/object.rb

Instance Method Summary collapse

Instance Method Details

#_redefine_method(name, &block) ⇒ Method

Redefine the method. Will undef the method if it exists or simply just define it.

# @since 0.0.7

Examples:

Redefine the method.

Object.re_define_method("exists?") do
  self
end

Parameters:

  • name (String, Symbol)

    The name of the method.

  • block (Proc)

    The method body.

Returns:

  • (Method)

    The new method.



21
22
23
24
# File 'lib/mongoid/listable/extensions/object.rb', line 21

def _redefine_method name, &block
  undef_method(name) if method_defined?(name)
  define_method(name, &block)
end

#around_method(name, &block) ⇒ Object

Redefines a method on owner to execute &block, passing to it as arguments the original method and its arguments. It’s the callers’ responsibility at that point to run whatever code and return the results of the original method by calling it.

Use as an alternative to before_method if your override depends on the results of the original method. around_method will avoid the inevitable stack overflow that occurs in before_method if you call itself.

Parameters:

  • owner (Object)

    The owner of the method

  • method (String)

    The name of the method to override

  • &block (Proc)

    The block to execute before original method

Since:

  • 0.0.6



57
58
59
60
61
62
# File 'lib/mongoid/listable/extensions/object.rb', line 57

def around_method name, &block
  original_method = instance_method name
  _redefine_method name do |*args|
    instance_exec original_method, *args, &block
  end        
end

#before_method(name, &block) ⇒ Object

Redefines a method on owner to first execute &block, then continue executing original method.

Parameters:

  • owner (Object)

    The owner of the method

  • method (String)

    The name of the method to override

  • &block (Proc)

    The block to execute before original method

Since:

  • 0.0.3



34
35
36
37
38
39
40
# File 'lib/mongoid/listable/extensions/object.rb', line 34

def before_method name, &block
  original_method = instance_method name
  _redefine_method name do |*args|
    instance_exec *args, &block
    original_method.bind(self).call *args
  end
end