Class: Ree::Contracts::CalledArgsValidator

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

Defined Under Namespace

Classes: Arg, ArgContract

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contract, params, printed_name) ⇒ CalledArgsValidator

Returns a new instance of CalledArgsValidator.



10
11
12
13
14
15
16
17
# File 'lib/ree/contracts/called_args_validator.rb', line 10

def initialize(contract, params, printed_name)
  @contracts = contract.arg_contracts
  @block_contract = contract.block_contract
  @params = params
  @printed_name = printed_name
  @args = {}
  build_args
end

Instance Attribute Details

#block_contractObject (readonly)

Returns the value of attribute block_contract.



8
9
10
# File 'lib/ree/contracts/called_args_validator.rb', line 8

def block_contract
  @block_contract
end

#contractsObject (readonly)

Returns the value of attribute contracts.



8
9
10
# File 'lib/ree/contracts/called_args_validator.rb', line 8

def contracts
  @contracts
end

#paramsObject (readonly)

Returns the value of attribute params.



8
9
10
# File 'lib/ree/contracts/called_args_validator.rb', line 8

def params
  @params
end

#printed_nameObject (readonly)

Returns the value of attribute printed_name.



8
9
10
# File 'lib/ree/contracts/called_args_validator.rb', line 8

def printed_name
  @printed_name
end

Instance Method Details

#call(args, kwargs, blk) ⇒ Object

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ree/contracts/called_args_validator.rb', line 27

def call(args, kwargs, blk)
  if block_contract == ArgContracts::Block && !blk
    msg = "missing required block"
    raise ContractError, "Wrong number of arguments for #{printed_name}\n\t - #{msg}"
  end

  errors = []

  validate_args(args, errors)
  validate_kwargs(kwargs, errors)

  return if errors.empty?

  msg = error_message(errors)
  puts(colorize(msg))

  raise ContractError, msg
end

#get_arg(arg_name) ⇒ Object



23
24
25
# File 'lib/ree/contracts/called_args_validator.rb', line 23

def get_arg(arg_name)
  @args[arg_name]
end

#get_argsObject



19
20
21
# File 'lib/ree/contracts/called_args_validator.rb', line 19

def get_args
  @args
end