Class: Caricature::RubyIsolator
- Defined in:
- lib/caricature/isolator.rb
Overview
A proxy to Ruby objects that records method calls this implements all the instance methods that are defined on the class.
Instance Attribute Summary
Attributes inherited from Isolator
#descriptor, #isolation, #subject
Instance Method Summary collapse
-
#create_isolation_for(subj) ⇒ Object
creates the ruby isolator for the specified subject.
-
#initialize(context) ⇒ RubyIsolator
constructor
implemented template method for creating Ruby isolations.
-
#initialize_messenger ⇒ Object
initializes the messaging strategy for the isolator.
Methods inherited from Isolator
#build_isolation, #class_name, for, #initialize_isolation
Constructor Details
#initialize(context) ⇒ RubyIsolator
implemented template method for creating Ruby isolations
242 243 244 245 246 247 248 249 |
# File 'lib/caricature/isolator.rb', line 242 def initialize(context) super klass = @context.subject.respond_to?(:class_eval) ? @context.subject : @context.subject.class inst = @context.subject.respond_to?(:class_eval) ? nil : @context.subject # inst = @context.subject.respond_to?(:class_eval) ? @context.subject.new : @context.subject @descriptor = RubyObjectDescriptor.new klass build_isolation klass, inst end |
Instance Method Details
#create_isolation_for(subj) ⇒ Object
creates the ruby isolator for the specified subject
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/caricature/isolator.rb', line 257 def create_isolation_for(subj) imembers = @descriptor.instance_members cmembers = @descriptor.class_members klass = Object.const_set(class_name(subj), Class.new(subj)) klass.class_eval do include Interception # access to the proxied subject def ___super___ isolation_context.instance end imembers.each do |mn| mn = mn.name.to_s.to_sym define_method mn do |*args| b = nil b = Proc.new { yield } if block_given? isolation_context.(mn, nil, *args, &b) end end def initialize(*args) self end cmembers.each do |mn| mn = mn.name.to_s.to_sym define_cmethod mn do |*args| b = nil b = Proc.new { yield } if block_given? isolation_context.(mn, nil, *args, &b) end end end klass end |
#initialize_messenger ⇒ Object
initializes the messaging strategy for the isolator
252 253 254 |
# File 'lib/caricature/isolator.rb', line 252 def initialize_messenger @context.messenger = RubyMessenger.new @context.expectations, @subject end |