Class: Contracts::ArgsValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/contracts/args_validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ ArgsValidator

Returns a new instance of ArgsValidator.



4
5
6
7
8
9
10
11
# File 'lib/contracts/args_validator.rb', line 4

def initialize(opts)
  @splat_args_contract_index = opts.fetch(:splat_args_contract_index)
  @klass                     = opts.fetch(:klass)
  @method                    = opts.fetch(:method)
  @contracts                 = opts.fetch(:contracts)
  @args_contracts            = opts.fetch(:args_contracts)
  @args_validators           = opts.fetch(:args_validators)
end

Instance Attribute Details

#args_contractsObject

Returns the value of attribute args_contracts.



3
4
5
# File 'lib/contracts/args_validator.rb', line 3

def args_contracts
  @args_contracts
end

#args_validatorsObject

Returns the value of attribute args_validators.



3
4
5
# File 'lib/contracts/args_validator.rb', line 3

def args_validators
  @args_validators
end

#contractsObject

Returns the value of attribute contracts.



3
4
5
# File 'lib/contracts/args_validator.rb', line 3

def contracts
  @contracts
end

#klassObject

Returns the value of attribute klass.



3
4
5
# File 'lib/contracts/args_validator.rb', line 3

def klass
  @klass
end

#methodObject

Returns the value of attribute method.



3
4
5
# File 'lib/contracts/args_validator.rb', line 3

def method
  @method
end

#splat_args_contract_indexObject

Returns the value of attribute splat_args_contract_index.



3
4
5
# File 'lib/contracts/args_validator.rb', line 3

def splat_args_contract_index
  @splat_args_contract_index
end

Instance Method Details

#validate_args_before_splat!(args) ⇒ Object

Loop forward validating the arguments up to the splat (if there is one) may change the ‘args` param



15
16
17
18
19
# File 'lib/contracts/args_validator.rb', line 15

def validate_args_before_splat!(args)
  (splat_args_contract_index || args.size).times do |i|
    validate!(args, i)
  end
end

#validate_splat_args_and_after!(args) ⇒ Object

possibilities

  • splat is last argument, like: def hello(a, b, *args)

  • splat is not last argument, like: def hello(*args, n)



24
25
26
27
28
29
30
31
32
33
# File 'lib/contracts/args_validator.rb', line 24

def validate_splat_args_and_after!(args)
  return unless splat_args_contract_index
  from, count = splat_range(args)
  validate_splat(args, from, count)

  splat_upper_bound = from + count
  return if splat_upper_bound == args.size

  validate_rest(args, from, splat_upper_bound)
end