Sha256: e2409c57a80db4baa8343a8439cf5a07a27e0950d5c1e0a7ba5aa9be75b19b8b
Contents?: true
Size: 1.43 KB
Versions: 1
Compression:
Stored size: 1.43 KB
Contents
require "dyna_mo/override_method" module DynaMo class Contexts < BasicObject attr_reader :module_name attr_accessor :default_context_name 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 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 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 alias :def_method :def_instance_method 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 end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
dyna_mo-0.0.1 | lib/dyna_mo/contexts.rb |