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
-
#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
a combinator may attempt to parse from a string if any of it’s types can do so.
-
#initialize(*types, **options) ⇒ Combinator
constructor
A new instance of Combinator.
- #name ⇒ Object
Methods inherited from Type
#check, #default_name, #respond_to?, short_name, #test, #to_s
Constructor Details
#initialize(*types, **options) ⇒ Combinator
Returns a new instance of Combinator.
11 12 13 14 |
# File 'lib/nrser/types/combinators.rb', line 11 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
48 49 50 51 52 |
# File 'lib/nrser/types/combinators.rb', line 48 def == other equal?(other) || ( other.class == self.class && other.types == @types ) 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.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/nrser/types/combinators.rb', line 33 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 union types could convert #{ string.inspect }" end |
#has_from_s? ⇒ Boolean
a combinator may attempt to parse from a string if any of it’s types can do so
26 27 28 |
# File 'lib/nrser/types/combinators.rb', line 26 def has_from_s? @types.any? {|type| type.has_from_s?} end |
#name ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/nrser/types/combinators.rb', line 16 def name @name || ( "#{ self.class.short_name }(" + @types.map {|type| type.name }.join(',') + ")" ) end |