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.


177
178
179
180
# File 'lib/contracts/builtin_contracts.rb', line 177

def initialize(*meths)
  super()
  @meths = meths
end

Instance Method Details

#to_sObject


188
189
190
# File 'lib/contracts/builtin_contracts.rb', line 188

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

#valid?(val) ⇒ Boolean

Returns:

  • (Boolean)

182
183
184
185
186
# File 'lib/contracts/builtin_contracts.rb', line 182

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