Class: Functional::ProtocolInfo

Inherits:
Synchronization::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:

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Functional::ProtocolInfo) initialize(name) { ... }

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



22
23
24
25
26
27
28
29
30
31
32
33
# 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?
  super
  @name = name.to_sym
  @info = Info.new({}, {}, [])
  self.instance_eval(&specification)
  @info.each_pair{|col, _| col.freeze}
  @info.freeze
  ensure_ivar_visibility!
  self.freeze
end

Instance Attribute Details

- (Object) name (readonly)

The symbolic name of the protocol



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

def name
  @name
end

Instance Method Details

- (Hash) class_methods

The class methods expected by this protocol.

Returns:

  • (Hash)

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



47
48
49
# File 'lib/functional/protocol_info.rb', line 47

def class_methods
  @info.class_methods
end

- (Array) constants

The constants expected by this protocol.

Returns:

  • (Array)

    a frozen list of the constants expected by this protocol



54
55
56
# File 'lib/functional/protocol_info.rb', line 54

def constants
  @info.constants
end

- (Hash) instance_methods

The instance methods expected by this protocol.

Returns:

  • (Hash)

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



39
40
41
# File 'lib/functional/protocol_info.rb', line 39

def instance_methods
  @info.instance_methods
end

- (Boolean) satisfies?(target)

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

Returns:

  • (Boolean)

    true if the target satisfies this protocol else false



61
62
63
64
65
# File 'lib/functional/protocol_info.rb', line 61

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