class NRSER::Types::Respond

This {Type} is used to test how the values in question respond to specific messages (method calls).

{Respond} instances hold a {#message} and {#response} type. Instances that respond when sent the message with a value that satisfies the response type are members.

@example Type whose members have non-empty string names.

type =  Types:Respond.new \
          to: :name,
          with: Types.non_empty_str

Attributes

message[R]

Message that will be sent to tested values.

@return [NRSER::Message]

publicly[R]

Controls whether the {#message} will be sent using `#public_send` (the default) or `#send` - which has access to private and protected methods.

@return [Boolean]

response[R]

Type tested values must respond with when sent the message.

@return [Type]

Public Class Methods

new(to:, with:, publicly: true, **options) click to toggle source

Instantiate a new instance.

See construction example in the class header: {Respond}.

@param [String | Symbol | Array] to

Fed in to {NRSER::Message.from} to create the {#message}.

Must be a lone string or symbol representing the method name to call,
or an Array with the string or symbol methods name in the first entry
(and whatever other parameters can follow it).

@param [Type | Object] with

The type members must respond with. If `with:` is not a {Type} it will
be made into on via {Types.make}.

@param [Boolean] publicly

Chooses between using `#public_send` and `#send` to send the {#message}.

@param [Hash] options

Additional options that will be passed up to {Type#initialize}.
Calls superclass method
# File lib/nrser/types/responds.rb, line 82
def initialize  to:,
                with:,
                publicly: true,
                **options
  @message = NRSER::Message.from *to
  @publicly = publicly
  @response = NRSER::Types.make with
  
  super **options
end

Public Instance Methods

explain() click to toggle source

See {Type#explain}.

@param (see Type#explain) @return (see Type#explain) @raise (see Type#explain)

# File lib/nrser/types/responds.rb, line 103
def explain
  args_str = message.args.map( &:inspect ).join ', '
  
  if message.block
    args_str += ', ' + message.block.to_s
  end
  
  "##{ message.symbol }(#{ args_str })#{ RESPONDS_WITH }#{ response.explain }"
end
test?(value) click to toggle source

Test value for membership.

@param (see Type#test?) @return (see Type#test?) @raise (see Type#test?)

# File lib/nrser/types/responds.rb, line 120
def test? value
  response.test message.send_to( value, publicly: publicly )
end