Class: NRSER::Types::Attributes

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

Overview

Note:

Construct Attributes types using the Attributes factory.

Specify types for value attributes.

Instance Attribute Summary collapse

Display Instance Methods 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?, #symbolic, #test, #to_data, #to_proc, #to_s, #union, #xor

Constructor Details

#initialize(attrs, **options) ⇒ Attributes

Construct an ‘AttrsType`.

Parameters:

  • attrs (Hash<#to_sym, 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.



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/nrser/types/attributes.rb', line 54

def initialize attrs, **options
  super **options
  
  if attrs.empty?
    raise ArgumentError,
      "Must provide at least one attribute name/type pair"
  end
  
  @types = attrs.map { |k, v|
    [ k.to_sym, NRSER::Types.make( v ) ]
  }.to_h.freeze
end

Instance Attribute Details

#typesHash<Symbol, Type> (readonly)

Attribute types by name.

Returns:



43
44
45
# File 'lib/nrser/types/attributes.rb', line 43

def types
  @types
end

Instance Method Details

#default_nameObject



78
79
80
81
82
83
84
85
86
# File 'lib/nrser/types/attributes.rb', line 78

def default_name
  type_strings = self.type_strings method: :name

  if type_strings.length == 1
    type_strings[0]
  else
    L_PAREN + type_strings.join( " #{ AND } " ) + R_PAREN
  end
end

#default_symbolicObject



89
90
91
92
93
94
95
96
97
# File 'lib/nrser/types/attributes.rb', line 89

def default_symbolic
  type_strings = self.type_strings method: :symbolic

  if type_strings.length == 1
    type_strings[0]
  else
    L_PAREN + type_strings.join( " #{ AND } " ) + R_PAREN
  end
end

#explainString

Returns:

See Also:



104
105
106
107
108
# File 'lib/nrser/types/attributes.rb', line 104

def explain
  "#{ self.class.demod_name }<" +
  type_strings( method: :explain ).join( ', ' ) +
  ">"
end

#test?(value) ⇒ Boolean

Returns:

See Also:



115
116
117
118
119
120
# File 'lib/nrser/types/attributes.rb', line 115

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

#type_strings(method:) ⇒ Object




71
72
73
74
75
# File 'lib/nrser/types/attributes.rb', line 71

def type_strings method:
  types.map { |name, type|
    "##{ name }#{ RESPONDS_WITH }#{ type.public_send method }"
  }
end