Class: NRSER::Types::Shape

Inherits:
Type show all
Defined in:
lib/nrser/types/shape.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Type

#===, #builtin_inspect, #check, #check!, #from_data, #from_s, #has_from_s?, #has_to_data?, #inspect, #intersection, #name, #not, #respond_to?, #test, #to_data, #to_s, #union, #xor

Constructor Details

#initialize(pairs, **options) ⇒ Shape

Instantiate a new ‘NRSER::Types::Shape`.



27
28
29
30
31
32
# File 'lib/nrser/types/shape.rb', line 27

def initialize pairs, **options
  super **options
  @pairs = pairs.map { |k, v|
    [k, NRSER::Types.make( v )]
  }.to_h.freeze
end

Instance Attribute Details

#pairsHash (readonly)

TODO document ‘pairs` attribute.

Returns:



20
21
22
# File 'lib/nrser/types/shape.rb', line 20

def pairs
  @pairs
end

Instance Method Details

#custom_from_data(data) ⇒ Object



65
66
67
68
69
# File 'lib/nrser/types/shape.rb', line 65

def custom_from_data data
  pairs.map { |key, type|
    [ key, type.from_data( data[key] ) ]
  }.to_h
end

#explainObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/nrser/types/shape.rb', line 48

def explain
  inner = @pairs.map { |k, v|
    "[#{ k.inspect }]→#{ v.name }"
  }.join( ", " )
  
  if @pairs.count == 1
    inner
  else
    '(' + inner + ')'
  end
end

#has_from_data?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/nrser/types/shape.rb', line 60

def has_from_data?
  pairs.values.all? { |type| type.has_from_data? }
end

#test?(value) ⇒ Boolean

Instance Methods

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
# File 'lib/nrser/types/shape.rb', line 38

def test? value
  begin
    @pairs.all? { |k, v|
      v === value[k]
    }
  rescue
    false
  end
end