Class: Caricature::MethodCallRecording
- Defined in:
- lib/caricature/method_call_recorder.rb
Overview
A recording that represents a method call it contains argument variations that can be matched too
Instance Attribute Summary collapse
-
#args ⇒ Object
add args.
-
#block ⇒ Object
gets or sets the block for this method call.
-
#count ⇒ Object
gets or sets the amount of times the method was called.
-
#method_name ⇒ Object
gets or sets the method name.
Instance Method Summary collapse
-
#add_argument_variation(args, block) ⇒ Object
add an argument variation.
-
#find_argument_variations(args, block_args) ⇒ Object
finds an argument variation that matches the provided
args. -
#has_argument_variations? ⇒ Boolean
indicates if it has an argument variation.
-
#initialize(method_name, count = 0) ⇒ MethodCallRecording
constructor
Initializes a new instance of a method call recording every time a method gets called in an isolated object this gets stored in the method call recorder It expects a
method_nameat the very least. - #match_hash(args, block_args) ⇒ Object
Constructor Details
#initialize(method_name, count = 0) ⇒ MethodCallRecording
Initializes a new instance of a method call recording every time a method gets called in an isolated object this gets stored in the method call recorder It expects a method_name at the very least.
98 99 100 101 102 |
# File 'lib/caricature/method_call_recorder.rb', line 98 def initialize(method_name, count=0) @method_name = method_name @count = count @variations = [] end |
Instance Attribute Details
#args ⇒ Object
add args
89 90 91 |
# File 'lib/caricature/method_call_recorder.rb', line 89 def args @args end |
#block ⇒ Object
gets or sets the block for this method call
92 93 94 |
# File 'lib/caricature/method_call_recorder.rb', line 92 def block @block end |
#count ⇒ Object
gets or sets the amount of times the method was called
86 87 88 |
# File 'lib/caricature/method_call_recorder.rb', line 86 def count @count end |
#method_name ⇒ Object
gets or sets the method name
83 84 85 |
# File 'lib/caricature/method_call_recorder.rb', line 83 def method_name @method_name end |
Instance Method Details
#add_argument_variation(args, block) ⇒ Object
add an argument variation
115 116 117 118 119 120 121 122 |
# File 'lib/caricature/method_call_recorder.rb', line 115 def add_argument_variation(args, block) variation = find_argument_variations args, nil if variation.empty? @variations << ArgumentRecording.new(args, @variations.size+1, block) if variation == [] else variation.first.call_number += 1 end end |
#find_argument_variations(args, block_args) ⇒ Object
finds an argument variation that matches the provided args
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/caricature/method_call_recorder.rb', line 125 def find_argument_variations(args, block_args) return @variations if args.first.is_a?(Symbol) and args.last == :any return match_hash(args, block_args) if args.size == 1 and args.last.is_a?(Hash) return @variations.select { |ar| ar.args == args } if block_args.nil? return @variations.select { |ar| ar.args == args and ar.block } if block_args.last == :any return @variations.select do |ar| av = ar.args == args # idx = 0 # ags = ar.args # av = args.all? do |ag| # rag = ags[idx] # res = if ag.is_a?(Hash) and rag.is_a?(Hash) # ag.all? { |k, v| ar.args[idx][k] == v } # else # ag == rag # end # idx += 1 # res # end av and not ar.find_block_variation(*block_args).empty? end end |
#has_argument_variations? ⇒ Boolean
indicates if it has an argument variation
110 111 112 |
# File 'lib/caricature/method_call_recorder.rb', line 110 def has_argument_variations? @variations.size > 1 end |
#match_hash(args, block_args) ⇒ Object
148 149 150 151 152 153 |
# File 'lib/caricature/method_call_recorder.rb', line 148 def match_hash(args, block_args) @variations.select do |ar| ags = ar.args.last args.last.all? { |k, v| ags[k] == v } end end |