Module: Functional::Protocol

Defined in:
lib/functional/protocol.rb

Overview

Protocols provide a polymorphism and method-dispatch mechanism that exchews stong 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.

Since:

  • 1.0.0 (formerly "behavior")

Constant Summary

@@info =

The global registry of specified protocols.

Since:

  • 1.0.0 (formerly "behavior")

{}

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:

Since:

  • 1.0.0 (formerly "behavior")



89
90
91
92
93
# File 'lib/functional/protocol.rb', line 89

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

Since:

  • 1.0.0 (formerly "behavior")



74
75
76
77
# File 'lib/functional/protocol.rb', line 74

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:

  • (ArgumentError)

    when no protocols are given

  • (Functional::ProtocolError)

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

Since:

  • 1.0.0 (formerly "behavior")



117
118
119
120
121
# File 'lib/functional/protocol.rb', line 117

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

Since:

  • 1.0.0 (formerly "behavior")



102
103
104
105
# File 'lib/functional/protocol.rb', line 102

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