Class: NRSER::Types::Respond

Inherits:
Type show all
Defined in:
lib/nrser/types/responds.rb

Overview

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.

Examples:

Type whose members have non-empty string names.

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Type

#===, #builtin_inspect, #check, #check!, #default_name, #default_symbolic, #from_data, #from_s, #has_from_data?, #has_from_s?, #has_to_data?, #inspect, #intersection, #name, #not, #respond_to?, #symbolic, #test, #to_data, #to_proc, #to_s, #union, #xor

Constructor Details

#initialize(to:, with:, publicly: true, **options) ⇒ Respond

Instantiate a new instance.

See construction example in the class header: NRSER::Types::Respond.

Parameters:

  • to (String | Symbol | Array)

    Fed in to 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).

  • with (Type | Object)

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

  • publicly (Boolean) (defaults to: true)

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

  • options (Hash)

    Additional options that will be passed up to Type#initialize.



82
83
84
85
86
87
88
89
90
91
# 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

Instance Attribute Details

#messageNRSER::Message (readonly)

Message that will be sent to tested values.

Returns:



39
40
41
# File 'lib/nrser/types/responds.rb', line 39

def message
  @message
end

#publiclyBoolean (readonly)

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

Returns:



55
56
57
# File 'lib/nrser/types/responds.rb', line 55

def publicly
  @publicly
end

#responseType (readonly)

Type tested values must respond with when sent the message.

Returns:



46
47
48
# File 'lib/nrser/types/responds.rb', line 46

def response
  @response
end

Instance Method Details

#explainString

Returns:



103
104
105
106
107
108
109
110
111
# 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) ⇒ Boolean

Test value for membership.

Parameters:

  • value (Object)

    Value to test for type satisfaction.

Returns:

  • (Boolean)

    ‘true` if the `value` satisfies the type.

Raises:



120
121
122
# File 'lib/nrser/types/responds.rb', line 120

def test? value
  response.test message.send_to( value, publicly: publicly )
end