Module: MetaProgramming::Object::ClassMethods

Defined in:
lib/meta_programming/object.rb

Instance Method Summary collapse

Instance Method Details

#define_chained_method(method_name, ext, &block) ⇒ Object



27
28
29
30
# File 'lib/meta_programming/object.rb', line 27

def define_chained_method(method_name, ext, &block)
  define_method("#{method_name}_with_#{ext}".to_sym, block)
  safe_alias_method_chain(method_name.to_sym, ext)
end

#metaclassObject



9
10
11
# File 'lib/meta_programming/object.rb', line 9

def metaclass
  class << self; self; end
end

#safe_alias_method_chain(method_name, ext) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/meta_programming/object.rb', line 12

def safe_alias_method_chain(method_name, ext)
  class_eval do
    method_name_with_ext = "#{method_name}_with_#{ext}".to_sym
    if (method_defined?(method_name_with_ext) && !(metaclass.instance_variable_defined?("@#{method_name_with_ext}")))
      if method_defined?(method_name.to_sym)
        alias_method_chain(method_name.to_sym, ext.to_sym)
      else
        alias_method method_name.to_sym, method_name_with_ext
        define_method("#{method_name}_without_#{ext}".to_sym) {|*args| }
      end
      metaclass.instance_variable_set("@#{method_name_with_ext}", true)
    end
  end
end