Class: Contracts::Builtin::RespondTo

Inherits:
CallableClass show all
Defined in:
lib/contracts/builtin_contracts.rb

Overview

Takes a variable number of method names as symbols. The contract passes if the argument responds to all of those methods. Example: RespondTo[:password, :credit_card]

Instance Method Summary collapse

Methods inherited from CallableClass

[]

Constructor Details

#initialize(*meths) ⇒ RespondTo

Returns a new instance of RespondTo.



165
166
167
# File 'lib/contracts/builtin_contracts.rb', line 165

def initialize(*meths)
  @meths = meths
end

Instance Method Details

#to_sObject



175
176
177
# File 'lib/contracts/builtin_contracts.rb', line 175

def to_s
  "a value that responds to #{@meths.inspect}"
end

#valid?(val) ⇒ Boolean

Returns:

  • (Boolean)


169
170
171
172
173
# File 'lib/contracts/builtin_contracts.rb', line 169

def valid?(val)
  @meths.all? do |meth|
    val.respond_to? meth
  end
end