Class: MuchStub::Stub
- Inherits:
-
Object
- Object
- MuchStub::Stub
- Defined in:
- lib/much-stub.rb
Instance Attribute Summary collapse
-
#do ⇒ Object
Returns the value of attribute do.
-
#ivar_name ⇒ Object
readonly
Returns the value of attribute ivar_name.
-
#method_name ⇒ Object
readonly
Returns the value of attribute method_name.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
- #call(args, orig_caller = nil, &block) ⇒ Object
- #call_method(args, &block) ⇒ Object
-
#initialize(object, method_name, orig_caller = nil, &block) ⇒ Stub
constructor
A new instance of Stub.
- #inspect ⇒ Object
- #teardown ⇒ Object
- #with(*args, &block) ⇒ Object
Constructor Details
#initialize(object, method_name, orig_caller = nil, &block) ⇒ Stub
Returns a new instance of Stub.
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/much-stub.rb', line 48 def initialize(object, method_name, orig_caller = nil, &block) orig_caller ||= caller_locations @metaclass = class << object; self; end @method_name = method_name.to_s @name = "__muchstub_stub__#{object.object_id}_#{@method_name}" @ivar_name = "@__muchstub_stub_#{object.object_id}_" \ "#{@method_name.to_sym.object_id}" setup(object, orig_caller) @do = block @lookup = {} end |
Instance Attribute Details
#do ⇒ Object
Returns the value of attribute do.
46 47 48 |
# File 'lib/much-stub.rb', line 46 def do @do end |
#ivar_name ⇒ Object (readonly)
Returns the value of attribute ivar_name.
46 47 48 |
# File 'lib/much-stub.rb', line 46 def ivar_name @ivar_name end |
#method_name ⇒ Object (readonly)
Returns the value of attribute method_name.
46 47 48 |
# File 'lib/much-stub.rb', line 46 def method_name @method_name end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
46 47 48 |
# File 'lib/much-stub.rb', line 46 def name @name end |
Class Method Details
.key(object, method_name) ⇒ Object
42 43 44 |
# File 'lib/much-stub.rb', line 42 def self.key(object, method_name) "--#{object.object_id}--#{method_name}--" end |
Instance Method Details
#call(args, orig_caller = nil, &block) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/much-stub.rb', line 70 def call(args, orig_caller = nil, &block) orig_caller ||= caller_locations unless arity_matches?(args) msg = "arity mismatch on `#{@method_name}`: " \ "expected #{number_of_args(@method.arity)}, " \ "called with #{args.size}" raise StubArityError, msg, orig_caller.map(&:to_s) end lookup(args, orig_caller).call(*args, &block) rescue NotStubbedError @lookup.rehash lookup(args, orig_caller).call(*args, &block) end |
#call_method(args, &block) ⇒ Object
66 67 68 |
# File 'lib/much-stub.rb', line 66 def call_method(args, &block) @method.call(*args, &block) end |
#inspect ⇒ Object
102 103 104 105 106 |
# File 'lib/much-stub.rb', line 102 def inspect "#<#{self.class}:#{'0x0%x' % (object_id << 1)}" \ " @method_name=#{@method_name.inspect}" \ ">" end |
#teardown ⇒ Object
95 96 97 98 99 100 |
# File 'lib/much-stub.rb', line 95 def teardown @metaclass.send(:undef_method, @method_name) MuchStub.send(:remove_instance_variable, @ivar_name) @metaclass.send(:alias_method, @method_name, @name) @metaclass.send(:undef_method, @name) end |
#with(*args, &block) ⇒ Object
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/much-stub.rb', line 84 def with(*args, &block) orig_caller = caller_locations unless arity_matches?(args) msg = "arity mismatch on `#{@method_name}`: " \ "expected #{number_of_args(@method.arity)}, " \ "stubbed with #{args.size}" raise StubArityError, msg, orig_caller.map(&:to_s) end @lookup[args] = block end |