Module: Virtuatable::Enhancers::Helpers::Declarations

Included in:
Base
Defined in:
lib/virtuatable/enhancers/helpers/declarations.rb

Overview

This module holds the static methods to declare an enhancer in another class, linking both and allowing to automatically create enhanced instances by calling the same methods in the enhancer that one would call on the enhanced class.

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#enhancedObject (readonly)

Returns the value of attribute enhanced.



15
16
17
# File 'lib/virtuatable/enhancers/helpers/declarations.rb', line 15

def enhanced
  @enhanced
end

Instance Method Details

#enhances(class_object) ⇒ Object

Enhances the given class by declaring itself as its enhancer.

Parameters:

  • class_object (Class)

    the class to enhance.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/virtuatable/enhancers/helpers/declarations.rb', line 19

def enhances(class_object)
  @enhanced = class_object
  self_class = self
  class_object.singleton_class.class_eval do
    define_method :enhancer do
      self_class
    end
  end
  class_object.define_method :enhance do
    self_class.new(self)
  end
end