Class: NRSER::Types::AttrsType

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

Overview

Specify types for value attributes.

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(attrs, **options) ⇒ AttrsType

Construct an ‘AttrsType`.

Parameters:

  • attrs (Hash<Symbol, TYPE>)

    Map of attribute names to their types (‘TYPE` values will be passed through NRSER::Types.make to get a type instance).

    May not be empty.



35
36
37
38
39
40
41
42
43
44
# File 'lib/nrser/types/attrs.rb', line 35

def initialize attrs, **options
  super **options
  
  if attrs.empty?
    raise ArgumentError,
      "Must provide at least one attribute name/type pair"
  end
  
  @attrs = attrs.transform_values &NRSER::Types.maker
end

Instance Method Details

#explainString

Returns:

See Also:



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/nrser/types/attrs.rb', line 51

def explain
  attrs_str = @attrs.map { |name, type|
    "##{ name }#{ RESPONDS_WITH }#{ type.name }"
  }.join(', ')
  
  if @attrs.length < 2
    attrs_str
  else
    L_PAREN + attrs_str + R_PAREN
  end
end

#test?(value) ⇒ Boolean

Returns:

  • (Boolean)

See Also:



68
69
70
71
72
73
# File 'lib/nrser/types/attrs.rb', line 68

def test? value
  @attrs.all? { |name, type|
    value.respond_to?( name ) &&
      type.test?( value.method( name ).call )
  }
end