Class: Contracts::InputType

Inherits:
Precondition show all
Defined in:
lib/ruby_contracts/input_type.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Precondition

#after?, #before?

Methods inherited from Contract

#after?, #before?

Constructor Details

#initialize(expected_classes) ⇒ InputType

Returns a new instance of InputType.



5
6
7
8
# File 'lib/ruby_contracts/input_type.rb', line 5

def initialize(expected_classes)
  @expected_classes = expected_classes
  @expected_argument_count = expected_classes.size
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



3
4
5
# File 'lib/ruby_contracts/input_type.rb', line 3

def message
  @message
end

Instance Method Details

#satisfied?(context, arguments, result = nil) ⇒ Boolean

Returns:



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ruby_contracts/input_type.rb', line 10

def satisfied?(context, arguments, result=nil)
  if !match_size?(arguments)
    @message = "the method expect #{@expected_argument_count} arguments when #{arguments.size} was given"
    false
  elsif unmatched = unmatched_type_from(arguments)
    i, arg, expected_klass = unmatched
    @message = "the method expect a kind of #{expected_klass} for argument ##{i} when a kind of #{arg.class} was given"
    false
  else
    true
  end
end