Class: NRSER::Types::Combinator

Inherits:
Type
  • Object
show all
Defined in:
lib/nrser/types/combinators.rb

Direct Known Subclasses

Intersection, Union

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Type

#check, #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, **options
  super **options
  @types = types.map {|type| NRSER::Types.make type}
end

Instance Attribute Details

#typesObject (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

#default_nameObject



16
17
18
19
20
21
22
# File 'lib/nrser/types/combinators.rb', line 16

def default_name
  @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.

Raises:

  • (TypeError)


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 combinator #{ self.to_s } types could convert #{ s.inspect }"
end

#has_from_s?Boolean

a combinator may attempt to parse from a string if any of it’s types can do so

Returns:

  • (Boolean)


26
27
28
# File 'lib/nrser/types/combinators.rb', line 26

def has_from_s?
  @types.any? {|type| type.has_from_s?}
end