Module: Functional::Protocol

Defined in:
lib/functional/protocol.rb

Overview

Protocols provide a polymorphism and method-dispatch mechanism that eschews strong typing and embraces the dynamic duck typing of Ruby. Rather than interrogate a module, class, or object for its type and ancestry, protocols allow modules, classes, and methods to be interrogated based on their behavior. It is a logical extension of the respond_to? method, but vastly more powerful.

Constant Summary collapse

@@info =

The global registry of specified protocols.

{}

Class Method Summary collapse

Class Method Details

.Satisfy!(target, *protocols) ⇒ Symbol

Does the given module/class/object fully satisfy the given protocol(s)? Raises a Functional::ProtocolError on failure.

Parameters:

  • target (Object)

    the method/class/object to interrogate

  • protocols (Symbol)

    one or more protocols to check against the target

Returns:

  • (Symbol)

    the target

Raises:



87
88
89
90
91
# File 'lib/functional/protocol.rb', line 87

def Satisfy!(target, *protocols)
  Protocol::Satisfy?(target, *protocols) or
    Protocol.error(target, 'does not', *protocols)
  target
end

.Satisfy?(target, *protocols) ⇒ Boolean

Does the given module/class/object fully satisfy the given protocol(s)?

Parameters:

  • target (Object)

    the method/class/object to interrogate

  • protocols (Symbol)

    one or more protocols to check against the target

Returns:

  • (Boolean)

    true if the target satisfies all given protocols else false

Raises:

  • (ArgumentError)

    when no protocols given



72
73
74
75
# File 'lib/functional/protocol.rb', line 72

def Satisfy?(target, *protocols)
  raise ArgumentError.new('no protocols given') if protocols.empty?
  protocols.all?{|protocol| Protocol.satisfies?(target, protocol.to_sym) }
end

.Specified!(*protocols) ⇒ Boolean

Have the given protocols been specified? Raises a Functional::ProtocolError on failure.

Parameters:

  • protocols (Symbol)

    the list of protocols to check

Returns:

  • (Boolean)

    true if all given protocols have been specified

Raises:

  • (Functional::ProtocolError)

    if one or more of the given protocols have not been specified

  • (ArgumentError)

    when no protocols are given



115
116
117
118
119
# File 'lib/functional/protocol.rb', line 115

def Specified!(*protocols)
  raise ArgumentError.new('no protocols given') if protocols.empty?
  (unspecified = Protocol.unspecified(*protocols)).empty? or
    raise ProtocolError.new("The following protocols are unspecified: :#{unspecified.join('; :')}.")
end

.Specified?(*protocols) ⇒ Boolean

Have the given protocols been specified?

Parameters:

  • protocols (Symbol)

    the list of protocols to check

Returns:

  • (Boolean)

    true if all given protocols have been specified else false

Raises:

  • (ArgumentError)

    when no protocols are given



100
101
102
103
# File 'lib/functional/protocol.rb', line 100

def Specified?(*protocols)
  raise ArgumentError.new('no protocols given') if protocols.empty?
  Protocol.unspecified(*protocols).empty?
end