Class: NRSER::Types::Attributes
- 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
-
#types ⇒ Hash<Symbol, Type>
readonly
Attribute types by name.
Display Instance Methods collapse
- #default_name ⇒ Object
- #default_symbolic ⇒ Object
- #explain ⇒ String
- #test?(value) ⇒ Boolean
-
#type_strings(method:) ⇒ Object
————————————————————————–.
Instance Method Summary collapse
-
#initialize(attrs, **options) ⇒ Attributes
constructor
Construct an ‘AttrsType`.
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`.
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/nrser/types/attributes.rb', line 54 def initialize attrs, ** super ** 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
Instance Method Details
#default_name ⇒ Object
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_symbolic ⇒ Object
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 |
#explain ⇒ String
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
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 |