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, #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, **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



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_nameObject



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.

Raises:

  • (TypeError)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Parameters:

  • value (Object)

    Value of this type (though it is not checked).

Returns:

  • (Object)

    The data representation of the value.

Raises:

  • (NoMethodError)


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