Class: NRSER::Types::Combinator
- Defined in:
- lib/nrser/types/combinators.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#types ⇒ Object
readonly
Returns the value of attribute types.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #default_name ⇒ Object
-
#from_s(s) ⇒ Object
a combinator iterates through each of it’s types, trying the conversion and seeing if the result satisfies the combinator type itself.
-
#has_from_s? ⇒ Boolean
Overridden to delegate functionality to the combined types:.
-
#has_to_data? ⇒ Boolean
Overridden to delegate functionality to the combined types:.
-
#initialize(*types, **options) ⇒ Combinator
constructor
A new instance of Combinator.
-
#to_data(value) ⇒ Object
Overridden to delegate functionality to the combined types:.
Methods inherited from Type
#check, #from_data, #has_from_data?, #name, #respond_to?, short_name, #test, #to_s
Constructor Details
#initialize(*types, **options) ⇒ Combinator
Returns a new instance of Combinator.
12 13 14 15 |
# File 'lib/nrser/types/combinators.rb', line 12 def initialize *types, ** super ** @types = types.map {|type| NRSER::Types.make type} end |
Instance Attribute Details
#types ⇒ Object (readonly)
Returns the value of attribute types.
9 10 11 |
# File 'lib/nrser/types/combinators.rb', line 9 def types @types end |
Instance Method Details
#==(other) ⇒ Object
96 97 98 99 100 |
# File 'lib/nrser/types/combinators.rb', line 96 def == other equal?(other) || ( other.class == self.class && other.types == @types ) end |
#default_name ⇒ Object
18 19 20 21 22 |
# File 'lib/nrser/types/combinators.rb', line 18 def default_name "#{ self.class.short_name }<" + @types.map {|type| type.name }.join(',') + ">" end |
#from_s(s) ⇒ Object
a combinator iterates through each of it’s types, trying the conversion and seeing if the result satisfies the combinator type itself. the first such value found is returned.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/nrser/types/combinators.rb', line 35 def from_s s @types.each { |type| if type.respond_to? :from_s begin return check type.from_s(s) rescue TypeError => e # pass end end } raise TypeError, "none of combinator #{ self.to_s } types could convert #{ s.inspect }" end |
#has_from_s? ⇒ Boolean
Overridden to delegate functionality to the combined types:
A combinator may attempt to parse from a string if any of it’s types can do so.
27 28 29 |
# File 'lib/nrser/types/combinators.rb', line 27 def has_from_s? @types.any? {|type| type.has_from_s?} end |
#has_to_data? ⇒ Boolean
Overridden to delegate functionality to the combined types:
A combinator can convert a value to data if any of it’s types can.
69 70 71 |
# File 'lib/nrser/types/combinators.rb', line 69 def has_to_data? @types.any? { |type| type.has_to_data? } end |
#to_data(value) ⇒ Object
Overridden to delegate functionality to the combined types:
The first of the combined types that responds to ‘#to_data` is used to dump the value.
85 86 87 88 89 90 91 92 93 |
# File 'lib/nrser/types/combinators.rb', line 85 def to_data value @types.each { |type| if type.respond_to? :to_data return type.to_data value end } raise NoMethodError, "#to_data not defined" end |