Class: ShapeOf::Array

Inherits:
Shape
  • Object
show all
Defined in:
lib/shape_of.rb

Overview

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 Array, 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.

Class Method Summary collapse

Methods inherited from Shape

#initialize, required?

Constructor Details

This class inherits a constructor from ShapeOf::Shape

Class Method Details

.[](shape) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# 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

.shape_of?(object) ⇒ Boolean

Returns:



137
138
139
# File 'lib/shape_of.rb', line 137

def self.shape_of?(object)
  object.instance_of? @internal_class
end