Class: ShapeOf::Union

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

Overview

Union[Shape1, Shape2, …] denotes that it can be of one the provided shapes

Class Method Summary collapse

Methods inherited from Shape

#initialize, required?, shape_of?

Constructor Details

This class inherits a constructor from ShapeOf::Shape

Class Method Details

.[](*shapes) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/shape_of.rb', line 247

def self.[](*shapes)
  Class.new(self) do
    @class_name = "#{superclass.name}[#{shapes.map(&:inspect).join(", ")}]"
    @shapes = shapes

    def self.name
      @class_name
    end

    def self.to_s
      @class_name
    end

    def self.inspect
      @class_name
    end

    def self.shape_of?(object)
      @shapes.any? do |shape|
        if shape.respond_to? :shape_of?
          shape.shape_of? object
        elsif shape.is_a? ::Hash
          Hash[shape].shape_of? object
        elsif shape.is_a? ::Array
          Array[shape].shape_of? object
        elsif shape.is_a? Class
          object.instance_of? shape
        else
          object == shape
        end
      end
    end
  end
end