Class: Grift::MockMethod::MockExecutions::MockArguments
- Inherits:
-
Object
- Object
- Grift::MockMethod::MockExecutions::MockArguments
- Includes:
- Enumerable
- Defined in:
- lib/grift/mock_method/mock_executions/mock_arguments.rb
Overview
An immutable Enumerable that stores the arguments used in a call for Grift::MockMethod::MockExecutions.
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#kwargs ⇒ Object
readonly
Returns the value of attribute kwargs.
Instance Method Summary collapse
-
#[](index) ⇒ Object
Retrieves a stored argument by an accessor.
-
#each(&block) ⇒ void
Calls the given block once for each argument value, passing that value as a parameter.
-
#empty? ⇒ Boolean
Returns true if the call included no arguments.
-
#initialize(args: [], kwargs: {}) ⇒ Grift::MockMethod::MockExecutions::MockArguments
constructor
A new instance of MockArguments.
-
#keys ⇒ Array<Symbol>
Returns the keyword parameter keys passed in the call.
-
#last ⇒ Any
Returns the last argument passed in the call.
-
#length ⇒ Integer
Returns the number of arguments passed in the call.
-
#values ⇒ Array
Returns the arguments passed in the call.
Constructor Details
#initialize(args: [], kwargs: {}) ⇒ Grift::MockMethod::MockExecutions::MockArguments
A new instance of MockArguments.
19 20 21 22 |
# File 'lib/grift/mock_method/mock_executions/mock_arguments.rb', line 19 def initialize(args: [], kwargs: {}) @args = args.freeze @kwargs = kwargs.freeze end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
12 13 14 |
# File 'lib/grift/mock_method/mock_executions/mock_arguments.rb', line 12 def args @args end |
#kwargs ⇒ Object (readonly)
Returns the value of attribute kwargs.
12 13 14 |
# File 'lib/grift/mock_method/mock_executions/mock_arguments.rb', line 12 def kwargs @kwargs end |
Instance Method Details
#[](index) ⇒ Object
Retrieves a stored argument by an accessor. For positional arguments this would be an integer corresponding to the arguments position in the method call signature. For keyword arguments this would be the key / parameter name as a symbol or string.
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/grift/mock_method/mock_executions/mock_arguments.rb', line 44 def [](index) if index.instance_of?(Integer) @args[index] elsif index.instance_of?(Symbol) @kwargs[index] elsif index.instance_of?(String) @kwargs[index.to_sym] else raise(Grift::Error, "Cannot access by type '#{index.class}'. Expected an Integer, Symbol, or String") end end |
#each(&block) ⇒ void
This method returns an undefined value.
Calls the given block once for each argument value, passing that value as a parameter.
62 63 64 65 |
# File 'lib/grift/mock_method/mock_executions/mock_arguments.rb', line 62 def each(&block) @args.each(&block) @kwargs.each_value(&block) end |
#empty? ⇒ Boolean
Returns true if the call included no arguments.
123 124 125 |
# File 'lib/grift/mock_method/mock_executions/mock_arguments.rb', line 123 def empty? @args.empty? && @kwargs.empty? end |
#keys ⇒ Array<Symbol>
Returns the keyword parameter keys passed in the call.
To get the values, use #values.
141 142 143 |
# File 'lib/grift/mock_method/mock_executions/mock_arguments.rb', line 141 def keys @kwargs.keys end |
#last ⇒ Any
Returns the last argument passed in the call.
81 82 83 |
# File 'lib/grift/mock_method/mock_executions/mock_arguments.rb', line 81 def last @args.last || @kwargs.values.last end |
#length ⇒ Integer
Returns the number of arguments passed in the call.
102 103 104 |
# File 'lib/grift/mock_method/mock_executions/mock_arguments.rb', line 102 def length @args.length + @kwargs.length end |
#values ⇒ Array
Returns the arguments passed in the call.
If a keyword argument was used, then only the value will be returned. To get the keys, use #keys.
160 161 162 |
# File 'lib/grift/mock_method/mock_executions/mock_arguments.rb', line 160 def values @args + @kwargs.values end |