Class: Camille::Types::Array

Inherits:
BasicType show all
Defined in:
lib/camille/types/array.rb

Instance Attribute Summary collapse

Attributes inherited from BasicType

#fingerprint

Instance Method Summary collapse

Methods inherited from BasicType

&, #&, #[], [], directly_instantiable?, inherited, instance, #transform, #|, |

Constructor Details

#initialize(content) ⇒ Array

Returns a new instance of Array.



6
7
8
9
# File 'lib/camille/types/array.rb', line 6

def initialize content
  @content = Camille::Type.instance content
  @fingerprint = Digest::MD5.hexdigest "#{self.class.name}#{@content.fingerprint}"
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



4
5
6
# File 'lib/camille/types/array.rb', line 4

def content
  @content
end

Instance Method Details

#check(value) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/camille/types/array.rb', line 11

def check value
  if value.is_a? ::Array
    results = value.map.with_index do |element, index|
      [index, @content.check(element)]
    end

    errors = results.map do |index, result|
      if result.type_error?
        ["array[#{index}]", result]
      else
        nil
      end
    end.compact

    if errors.empty?
      Camille::Checked.new(fingerprint, results.map{|_, checked| checked.value})
    else
      Camille::TypeError.new(**errors.to_h)
    end
  else
    Camille::TypeError.new("Expected array, got #{value.inspect}.")
  end
end

#literalObject



35
36
37
# File 'lib/camille/types/array.rb', line 35

def literal
  "#{@content.literal}[]"
end