Class: NRSER::Types::Respond

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

Overview

Type holding an Message and response type. Satisfied by objects that respond with a value that satisfies the respond type when sent the message.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Type

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

Constructor Details

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

Instantiate a new ‘Respond`.



42
43
44
45
46
47
48
49
50
51
# File 'lib/nrser/types/responds.rb', line 42

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:



19
20
21
# File 'lib/nrser/types/responds.rb', line 19

def message
  @message
end

#publiclyBoolean (readonly)

TODO document ‘publicly` attribute.

Returns:

  • (Boolean)


34
35
36
# File 'lib/nrser/types/responds.rb', line 34

def publicly
  @publicly
end

#responseNRSER::Types::Type (readonly)

Type tested values must respond with when sent the message.

Returns:



26
27
28
# File 'lib/nrser/types/responds.rb', line 26

def response
  @response
end

Instance Method Details

#explainString

Returns:



63
64
65
66
67
68
69
70
71
# File 'lib/nrser/types/responds.rb', line 63

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:



80
81
82
# File 'lib/nrser/types/responds.rb', line 80

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