Class: Grift::MockMethod::MockExecutions::MockArguments

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#argsObject (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

#kwargsObject (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.

Examples:

my_mock = Grift.mock(Request, :new)
request = Request.new('/users', method: 'GET')
#=> <Request object>
my_mock.mock.calls.last[0] # integer access for positional
#=> '/users'
my_mock.mock.calls.last[:method] # key access for keyword
#=> 'GET'

Parameters:

  • index (Integer, Symbol, String)

    the accessor for the desired argument

Returns:

  • the specified value

Raises:

  • (Grift::Error)

    exception if accessor is not a supported type



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.

Examples:

arr = [1, 2, 3]
my_mock = Grift.mock(Array, :count)
arr.count
#=> 3
my_mock.mock.calls.last.empty?
#=> true
arr.count(2)
#=> 1
my_mock.mock.calls.last.empty?
#=> false

Returns:

  • (Boolean)

    true if the no arguments were used



123
124
125
# File 'lib/grift/mock_method/mock_executions/mock_arguments.rb', line 123

def empty?
  @args.empty? && @kwargs.empty?
end

#keysArray<Symbol>

Returns the keyword parameter keys passed in the call.

To get the values, use #values.

Examples:

my_mock = Grift.mock(Request, :new)
request = Request.new('/users', method: 'GET')
#=> <Request object>
my_mock.mock.calls.last.keys
#=> [:method]

Returns:

  • (Array<Symbol>)

    the parameter keys



141
142
143
# File 'lib/grift/mock_method/mock_executions/mock_arguments.rb', line 141

def keys
  @kwargs.keys
end

#lastAny

Returns the last argument passed in the call.

Examples:

my_mock = Grift.mock(Request, :new)
request = Request.new('/users', method: 'GET')
#=> <Request object>
my_mock.mock.calls.last.last
#=> 'GET'

Returns:

  • (Any)

See Also:

  • Enumerable#first


81
82
83
# File 'lib/grift/mock_method/mock_executions/mock_arguments.rb', line 81

def last
  @args.last || @kwargs.values.last
end

#lengthInteger

Returns the number of arguments passed in the call.

Examples:

arr = [1, 2, 3]
my_mock = Grift.mock(Array, :count)
arr.count
#=> 3
my_mock.mock.calls.last.length
#=> 0
arr.count(3)
#=> 1
my_mock.mock.calls.last.length
#=> 1

Returns:

  • (Integer)


102
103
104
# File 'lib/grift/mock_method/mock_executions/mock_arguments.rb', line 102

def length
  @args.length + @kwargs.length
end

#valuesArray

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.

Examples:

my_mock = Grift.mock(Request, :new)
request = Request.new('/users', method: 'GET')
#=> <Request object>
my_mock.mock.calls.last.keys
#=> [:method]

Returns:

  • (Array)

    the argument values



160
161
162
# File 'lib/grift/mock_method/mock_executions/mock_arguments.rb', line 160

def values
  @args + @kwargs.values
end