Class: Kagemusha
- Inherits:
-
Object
- Object
- Kagemusha
- Defined in:
- lib/kagemusha/core.rb,
lib/kagemusha/date.rb,
lib/kagemusha/rand.rb,
lib/kagemusha/time.rb,
lib/kagemusha/version.rb,
lib/kagemusha/datetime.rb,
lib/kagemusha/composite.rb
Overview
#
$Id: composite.rb 120 2009-02-09 08:22:25Z yuyakato $
#
Defined Under Namespace
Modules: Date, DateTime, Rand, Time, VERSION Classes: Composite
Instance Method Summary collapse
- #concat(mock) ⇒ Object (also: #+)
-
#define_class_method(name, &block) ⇒ Object
(also: #defs)
:nodoc:.
-
#define_instance_method(name, &block) ⇒ Object
(also: #def)
:nodoc:.
-
#initialize(klass) ⇒ Kagemusha
constructor
:nodoc:.
-
#swap ⇒ Object
:nodoc:.
-
#undefine_class_method(name) ⇒ Object
(also: #undefs)
:nodoc:.
-
#undefine_instance_method(name) ⇒ Object
(also: #undef)
:nodoc:.
Constructor Details
#initialize(klass) ⇒ Kagemusha
:nodoc:
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/kagemusha/core.rb', line 12 def initialize(klass) #:nodoc: @klass = klass @meta = class << @klass; self; end @class_methods = {} @instance_methods = {} if block_given? yield(self) end end |
Instance Method Details
#concat(mock) ⇒ Object Also known as: +
140 141 142 |
# File 'lib/kagemusha/core.rb', line 140 def concat(mock) return Kagemusha::Composite.new(self, mock) end |
#define_class_method(name, &block) ⇒ Object Also known as: defs
:nodoc:
23 24 25 26 |
# File 'lib/kagemusha/core.rb', line 23 def define_class_method(name, &block) #:nodoc: @class_methods[name.to_s.to_sym] = block return self end |
#define_instance_method(name, &block) ⇒ Object Also known as: def
:nodoc:
35 36 37 38 |
# File 'lib/kagemusha/core.rb', line 35 def define_instance_method(name, &block) #:nodoc: @instance_methods[name.to_s.to_sym] = block return self end |
#swap ⇒ Object
:nodoc:
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/kagemusha/core.rb', line 47 def swap #:nodoc: recover_class_methods = @class_methods.map { |name, proc| if proc begin # replace method attr = get_accessibility(@meta, name) methods = to_symbols(@klass.singleton_methods(false)) method = @meta.instance_method(name) @meta.instance_eval { define_method(name, proc) } @meta.instance_eval { private(name) } if attr == :private (methods.include?(name) ? [name, :define, nil, method] : [name, :remove]) rescue NameError # insert method @meta.instance_eval { define_method(name, proc) } [name, :undef] end else begin # remove method method = @meta.instance_method(name) @meta.instance_eval { undef_method(name) } [name, :define, nil, method] # FIXME: attr rescue NameError [name, :nop] end end } recover_instance_methods = @instance_methods.map { |name, proc| if proc begin # replace method attr = get_accessibility(@klass, name) methods = to_symbols(@klass.public_instance_methods(false)) methods += to_symbols(@klass.protected_instance_methods(false)) methods += to_symbols(@klass.private_instance_methods(false)) method = @klass.instance_method(name) @klass.instance_eval { define_method(name, proc) } @klass.instance_eval { protected(name) } if attr == :protected @klass.instance_eval { private(name) } if attr == :private (methods.include?(name) ? [name, :define, attr, method] : [name, :remove]) rescue NameError # insert method @klass.instance_eval { define_method(name, proc) } [name, :undef] end else begin # remove method method = @klass.instance_method(name) @klass.instance_eval { undef_method(name) } [name, :define, nil, method] # FIXME: attr rescue NameError [name, :nop] end end } return yield ensure recover_class_methods.each { |name, type, attr, method| case type when :nop then # nop when :remove then @meta.instance_eval { remove_method(name) } when :undef then @meta.instance_eval { undef_method(name) } when :define @meta.instance_eval { define_method(name, method) } @meta.instance_eval { private(name) } if attr == :private else raise("BUG") end } recover_instance_methods.each { |name, type, attr, method| case type when :nop then # nop when :remove then @klass.instance_eval { remove_method(name) } when :undef then @klass.instance_eval { undef_method(name) } when :define @klass.instance_eval { define_method(name, method) } @klass.instance_eval { protected(name) } if attr == :protected @klass.instance_eval { private(name) } if attr == :private else raise("BUG") end } end |
#undefine_class_method(name) ⇒ Object Also known as: undefs
:nodoc:
29 30 31 32 |
# File 'lib/kagemusha/core.rb', line 29 def undefine_class_method(name) #:nodoc: @class_methods[name.to_s.to_sym] = false return self end |
#undefine_instance_method(name) ⇒ Object Also known as: undef
:nodoc:
41 42 43 44 |
# File 'lib/kagemusha/core.rb', line 41 def undefine_instance_method(name) #:nodoc: @instance_methods[name.to_s.to_sym] = false return self end |