class ShapeOf::Union

Union[shape1, shape2, …] denotes that it can be of one the provided shapes.

Public Class Methods

[](*shapes) click to toggle source
# File lib/shape_of.rb, line 248
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
inspect() click to toggle source
# File lib/shape_of.rb, line 261
def self.inspect
  @class_name
end
name() click to toggle source
# File lib/shape_of.rb, line 253
def self.name
  @class_name
end
shape_of?(object) click to toggle source
# File lib/shape_of.rb, line 265
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
to_s() click to toggle source
# File lib/shape_of.rb, line 257
def self.to_s
  @class_name
end