Module: Surrogate::ClassMethods
- Defined in:
- lib/surrogate/endower.rb
Overview
use a module so that the method is inherited (important for substitutability)
Instance Method Summary collapse
-
#clone(overrides = {}) ⇒ Object
Should this be dup? (dup seems to copy singleton methods) and may be able to use #initialize_copy to reset ivars Can we just remove this feature an instead provide a reset feature which could be hooked into in before/after blocks (e.g. github.com/rspec/rspec-core/blob/622505d616d950ed53d12c6e82dbb953ba6241b4/lib/rspec/core/mocking/with_rspec.rb).
- #inspect ⇒ Object
- #last_instance ⇒ Object
- #last_instance=(instance) ⇒ Object
-
#new(*args) ⇒ Object
Custom new, because user can define initialize, and we need to record it.
Instance Method Details
#clone(overrides = {}) ⇒ Object
Should this be dup? (dup seems to copy singleton methods) and may be able to use #initialize_copy to reset ivars Can we just remove this feature an instead provide a reset feature which could be hooked into in before/after blocks (e.g. github.com/rspec/rspec-core/blob/622505d616d950ed53d12c6e82dbb953ba6241b4/lib/rspec/core/mocking/with_rspec.rb)
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/surrogate/endower.rb', line 126 def clone(overrides={}) hatchling, hatchery, parent_name = @hatchling, @hatchery, name Class.new self do extend Module.new { define_method(:name) { parent_name && parent_name + '.clone' } } # inherit the name -- use module so that ApiComparison comes out correct (real classes inherit their name method) Surrogate.endow self, hatchery. do hatchling.api_methods.each { |name, | define name, .to_hash, &.default_proc } end hatchery.api_methods.each { |name, | define name, .to_hash, &.default_proc } overrides.each { |attribute, value| @hatchling.prepare_method attribute, [value] } end end |
#inspect ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/surrogate/endower.rb', line 154 def inspect return name if name methods = SurrogateClassReflector.new(self).methods method_inspections = [] # add class methods if methods[:class][:api].any? meth_names = methods[:class][:api].to_a.sort.take(4) meth_names[-1] = '...' if meth_names.size == 4 method_inspections << "Class: #{meth_names.join ' '}" end # add instance methods if methods[:instance][:api].any? meth_names = methods[:instance][:api].to_a.sort.take(4) meth_names[-1] = '...' if meth_names.size == 4 method_inspections << "Instance: #{meth_names.join ' '}" end # when no class or instance methods method_inspections << "no api" if method_inspections.empty? "AnonymousSurrogate(#{method_inspections.join ', '})" end |
#last_instance ⇒ Object
145 146 147 |
# File 'lib/surrogate/endower.rb', line 145 def last_instance Thread.current["surrogate_last_instance_#{self.object_id}"] end |
#last_instance=(instance) ⇒ Object
149 150 151 |
# File 'lib/surrogate/endower.rb', line 149 def last_instance=(instance) Thread.current["surrogate_last_instance_#{self.object_id}"] = instance end |
#new(*args) ⇒ Object
Custom new, because user can define initialize, and we need to record it
139 140 141 142 143 |
# File 'lib/surrogate/endower.rb', line 139 def new(*args) instance = Endower.uninitialized_instance_for self instance.__send__ :initialize, *args instance end |