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
-
.Satisfy!(target, *protocols) ⇒ Symbol
Does the given module/class/object fully satisfy the given protocol(s)? Raises a ProtocolError on failure.
-
.Satisfy?(target, *protocols) ⇒ Boolean
Does the given module/class/object fully satisfy the given protocol(s)?.
-
.Specified!(*protocols) ⇒ Boolean
Have the given protocols been specified? Raises a ProtocolError on failure.
-
.Specified?(*protocols) ⇒ Boolean
Have the given protocols been specified?.
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.
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)?
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.
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?
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 |