Class: Functional::ProtocolInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/functional/protocol_info.rb

Overview

An immutable object describing a single protocol and capable of building itself from a block. Used by #SpecifyProtocol.

See Also:

Since:

  • 1.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) { ... } ⇒ Functional::ProtocolInfo

Process a protocol specification block and build a new object.

Parameters:

  • name (Symbol)

    the symbolic name of the protocol

Yields:

  • self to the given specification block

Raises:

  • (ArgumentError)

    when name is nil or an empty string

  • (ArgumentError)

    when no block given

Since:

  • 1.0.0


22
23
24
25
26
27
28
29
30
31
# File 'lib/functional/protocol_info.rb', line 22

def initialize(name, &specification)
  raise ArgumentError.new('no block given') unless block_given?
  raise ArgumentError.new('no name given') if name.nil? || name.empty?
  @name = name.to_sym
  @info = Info.new({}, {}, [])
  self.instance_eval(&specification)
  @info.each_pair{|col, _| col.freeze}
  @info.freeze
  self.freeze
end

Instance Attribute Details

#nameObject (readonly)

The symbolic name of the protocol

Since:

  • 1.0.0


12
13
14
# File 'lib/functional/protocol_info.rb', line 12

def name
  @name
end

Instance Method Details

#class_methodsHash

The class methods expected by this protocol.

Returns:

  • (Hash)

    a frozen hash of all class method names and their expected arity for this protocol

Since:

  • 1.0.0


45
46
47
# File 'lib/functional/protocol_info.rb', line 45

def class_methods
  @info.class_methods
end

#constantsArray

The constants expected by this protocol.

Returns:

  • (Array)

    a frozen list of the constants expected by this protocol

Since:

  • 1.0.0


52
53
54
# File 'lib/functional/protocol_info.rb', line 52

def constants
  @info.constants
end

#instance_methodsHash

The instance methods expected by this protocol.

Returns:

  • (Hash)

    a frozen hash of all instance method names and their expected arity for this protocol

Since:

  • 1.0.0


37
38
39
# File 'lib/functional/protocol_info.rb', line 37

def instance_methods
  @info.instance_methods
end

#satisfies?(target) ⇒ Boolean

Does the given module/class/object satisfy this protocol?

Returns:

  • (Boolean)

    true if the target satisfies this protocol else false

Since:

  • 1.0.0


59
60
61
62
63
# File 'lib/functional/protocol_info.rb', line 59

def satisfies?(target)
  satisfies_constants?(target) &&
    satisfies_instance_methods?(target) &&
    satisfies_class_methods?(target)
end