class DynaMo::Contexts

Attributes

default_context_name[RW]
module_name[R]

Public Class Methods

new(mod_name, default_context_name) click to toggle source
# File lib/dyna_mo/contexts.rb, line 8
def initialize(mod_name, default_context_name)
  # mod_name MUST be string here to assure target module_name consistency
  @module_name = (mod_name =~ /^::/ ? mod_name : '::' + mod_name)
  @default_context_name = default_context_name.to_sym

  @instance_method_mods = []
  @class_method_mods = []
end

Public Instance Methods

__apply__() click to toggle source
# File lib/dyna_mo/contexts.rb, line 17
def __apply__
  target = ::Kernel.eval(@module_name)

  # reverse: Last defined context's method priority is highest
  target.prepend( *(@instance_method_mods.reverse.map(&:applied_module)) )
  (class << target; self; end).prepend( *(@class_method_mods.reverse.map(&:applied_module)) )

  # prepending twice has no effects
end
def_class_method(method_name, context_name = @default_context_name, &block) click to toggle source
# File lib/dyna_mo/contexts.rb, line 35
def def_class_method(method_name, context_name = @default_context_name, &block)
  raise "block is not given for def_singleton_method" unless block #_given?

  @class_method_mods.push OverrideMethod.new(context_name.to_sym, method_name, &block)
  method_name
end
def_instance_method(method_name, context_name = @default_context_name, &block) click to toggle source
# File lib/dyna_mo/contexts.rb, line 27
def def_instance_method(method_name, context_name = @default_context_name, &block)
  raise "block is not given for def_instance_method" unless block # block_given?

  @instance_method_mods.push OverrideMethod.new(context_name.to_sym, method_name, &block)
  method_name
end
Also aliased as: def_method
def_method(method_name, context_name = @default_context_name, &block)
Alias for: def_instance_method