Class: YardTypes::DuckType

Inherits:
Type
  • Object
show all
Defined in:
lib/yard_types/types.rb

Overview

A DuckType constraint is specified as #some_message, and indicates that the object must respond to the method some_message.

Instance Attribute Summary collapse

Attributes inherited from Type

#name

Instance Method Summary collapse

Methods inherited from Type

for, #to_s

Constructor Details

#initialize(name) ⇒ DuckType

Returns a new instance of DuckType

Parameters:

  • name (String)

    The YARD identifier, eg #some_message.


108
109
110
111
# File 'lib/yard_types/types.rb', line 108

def initialize(name)
  @name    = name
  @message = name[1..-1]
end

Instance Attribute Details

#messageString (readonly)

Returns The method the object must respond to; this does not include the leading # character.

Returns:

  • (String)

    The method the object must respond to; this does not include the leading # character.


105
106
107
# File 'lib/yard_types/types.rb', line 105

def message
  @message
end

Instance Method Details

#check(obj) ⇒ Boolean

Returns true if the object responds to message.

Parameters:

  • obj (Object)

    Any object.

Returns:

  • (Boolean)

    true if the object responds to message.


120
121
122
# File 'lib/yard_types/types.rb', line 120

def check(obj)
  obj.respond_to? message
end

#descriptionString

Returns an English phrase describing this type.

Returns:

  • (String)

    an English phrase describing this type.


114
115
116
# File 'lib/yard_types/types.rb', line 114

def description
  "an object that responds to #{name}"
end