Module: Prototype::ClassMethods
- Included in:
- Prototype
- Defined in:
- lib/prototype.rb,
lib/prototype-2.0.0.rb
Instance Method Summary collapse
- #clone(src, *a, &b) ⇒ Object
- #context(&b) ⇒ Object
- #extend(obj, mod) ⇒ Object
- #inherit(c = Object, *a, &b) ⇒ Object (also: #exnihilo, #ex_nihilo)
- #new(c = Object, *a, &b) ⇒ Object
- #prototyped!(obj) ⇒ Object
- #prototyped?(obj) ⇒ Boolean
- #prototyping(obj, &b) ⇒ Object
- #version ⇒ Object
Instance Method Details
#clone(src, *a, &b) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/prototype.rb', line 45 def clone src, *a, &b c = src.class c = Class.new c dst = c.new *a sc = class << src self end modules = sc.instance_variable_get('@modules') || [] modules.each{|mod| extend dst, mod} src.instance_variables.each do |ivar| value = src.instance_variable_get ivar dst.instance_variable_set ivar, value end prototyping dst, &b dst end |
#context(&b) ⇒ Object
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/prototype.rb', line 109 def context &b mod = Module.new sc = class << mod self end sc.module_eval do __method__ = {} and define_method(:__method__){ __method__ } def __call__(m, *a, &b) meth = __method__[m.to_s] raise NoMethodError, m.to_s unless meth meth.call *a, &b end alias_method '__define_method__', 'define_method' end mod.methods.each{|m| mod.__method__[m] = mod.method(m) unless %w( __call__ __method__ ).include?(m)} mod.methods.each do |m| unless m =~ /^__|attribute/ sc.module_eval{ undef_method m } end end sc.module_eval do def method_missing m, *a, &b if b __define_method__ m, *a, &b else ivar = "@#{ m }" case a.size when 0 if eval("defined? #{ ivar }") __call__ 'instance_variable_get', ivar else super end when 1 value = a.shift __call__ 'instance_variable_set', ivar, value else super end end end end mod.__call__ 'module_eval', &b if b mod.__call__ 'module_eval' do defined = __call('instance_methods').inject({}){|h,m| h.update m => true} __call__('instance_variables').each do |ivar| m = ivar[1..-1] getter, setter, query = defined["#{ m }"], defined["#{ m }="], defined["#{ m }?"] if getter.nil? and setter.nil? and query.nil? __call__ 'attribute', m next end if getter.nil? __call__ 'module_eval', "def #{ m }() #{ ivar } end" end if setter.nil? __call__ 'module_eval', "def #{ m }=(v) #{ ivar }=v end" end if query.nil? __call__ 'module_eval', "def #{ m }?() defined?(#{ ivar }) and #{ ivar } end" end end end mod end |
#extend(obj, mod) ⇒ Object
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/prototype.rb', line 78 def extend obj, mod sc = class << obj self end sc.module_eval do (@modules ||= []) << mod include mod end end |
#inherit(c = Object, *a, &b) ⇒ Object Also known as: exnihilo, ex_nihilo
30 31 32 33 34 |
# File 'lib/prototype.rb', line 30 def inherit c = Object, *a, &b c = c.class unless Class === c c = Class.new c new c, *a, &b end |
#new(c = Object, *a, &b) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/prototype.rb', line 38 def new c = Object, *a, &b c = c.class unless Class === c obj = c.new *a prototyping obj, &b obj end |
#prototyped!(obj) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/prototype.rb', line 99 def prototyped! obj sc = class << obj self end sc.module_eval do @prototyped = true end end |
#prototyped?(obj) ⇒ Boolean
89 90 91 92 93 94 95 96 97 |
# File 'lib/prototype.rb', line 89 def prototyped? obj sc = class << obj self end sc.module_eval do defined?(@prototyped) and @prototyped end end |
#prototyping(obj, &b) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/prototype.rb', line 66 def prototyping obj, &b obj.extend InstanceMethods mod = Prototype.context &b ivars = mod.__method__["instance_variables"].call() ivars.each do |ivar| value = mod.__method__["instance_variable_get"].call(ivar) obj.instance_eval{ instance_variable_set ivar, value } end extend obj, mod obj end |