Class: NRSER::Types::AttrsType
Overview
Specify types for value attributes.
Instance Method Summary collapse
- #explain ⇒ String
-
#initialize(attrs, **options) ⇒ AttrsType
constructor
Construct an ‘AttrsType`.
- #test?(value) ⇒ Boolean
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`.
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/nrser/types/attrs.rb', line 35 def initialize attrs, ** super ** 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
#explain ⇒ String
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
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 |