class ShapeOf::Array

Array denotes that it is an array of shapes. It checks every element in the array and verifies that the element is in the correct shape. This, along with Hash, are the core components of this module. Note that a ShapeOf::Array.shape_of?([]) will pass because it is vacuously true for an empty array.

Public Class Methods

[](shape) click to toggle source
# File lib/shape_of.rb, line 141
def self.[](shape)
  Class.new(self) do
    @class_name = "#{superclass.name}[#{shape.inspect}]"
    @shape = shape
    @internal_class = superclass.instance_variable_get(:@internal_class)

    def self.name
      @class_name
    end

    def self.to_s
      @class_name
    end

    def self.inspect
      @class_name
    end

    def self.shape_of?(array)
      super && array.all? do |elem|
        if @shape.respond_to? :shape_of?
          @shape.shape_of? elem
        elsif @shape.is_a? ::Array
          Array[@shape.first].shape_of? elem
        elsif @shape.is_a? ::Hash
          Hash[@shape].shape_of? elem
        elsif @shape.is_a? Class
          elem.instance_of? @shape
        else
          elem == @shape
        end
      end
    end
  end
end
inspect() click to toggle source
# File lib/shape_of.rb, line 155
def self.inspect
  @class_name
end
name() click to toggle source
# File lib/shape_of.rb, line 147
def self.name
  @class_name
end
shape_of?(object) click to toggle source
# File lib/shape_of.rb, line 137
def self.shape_of?(object)
  object.instance_of? @internal_class
end
to_s() click to toggle source
# File lib/shape_of.rb, line 151
def self.to_s
  @class_name
end