Class: Mocktail::ValidatesArguments

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/mocktail/handles_dry_call/validates_arguments.rb,
lib/mocktail/sorbet/mocktail/handles_dry_call/validates_arguments.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeValidatesArguments

Returns a new instance of ValidatesArguments.



26
27
28
# File 'lib/mocktail/handles_dry_call/validates_arguments.rb', line 26

def initialize
  @simulates_argument_error = SimulatesArgumentError.new
end

Class Method Details

.disable!Object



5
6
7
# File 'lib/mocktail/handles_dry_call/validates_arguments.rb', line 5

def self.disable!
  Thread.current[:mocktail_arity_validation_disabled] = true
end

.disabled?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/mocktail/handles_dry_call/validates_arguments.rb', line 13

def self.disabled?
  !!Thread.current[:mocktail_arity_validation_disabled]
end

.enable!Object



9
10
11
# File 'lib/mocktail/handles_dry_call/validates_arguments.rb', line 9

def self.enable!
  Thread.current[:mocktail_arity_validation_disabled] = false
end

.optional(disable, &blk) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/mocktail/handles_dry_call/validates_arguments.rb', line 17

def self.optional(disable, &blk)
  return blk.call unless disable

  disable!
  ret = blk.call
  enable!
  ret
end

Instance Method Details

#validate(dry_call) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/mocktail/handles_dry_call/validates_arguments.rb', line 30

def validate(dry_call)
  return if self.class.disabled?

  if (error = @simulates_argument_error.simulate(dry_call))
    raise error
  end
end