Class: ShapeOf::Pattern

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

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

.[](shape) ⇒ Object

Raises:

  • (TypeError)


320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/shape_of.rb', line 320

def self.[](shape)
  raise TypeError, "Shape must be #{Regexp.inspect}, was #{shape.inspect}" unless shape.instance_of? Regexp

  Class.new(self) do
    @class_name = "#{superclass.name}[#{shape.inspect}]"
    @shape = shape

    def self.name
      @class_name
    end

    def self.to_s
      @class_name
    end

    def self.inspect
      @class_name
    end

    def self.shape_of?(object)
      raise TypeError, "expected #{String.inspect}, was instead #{object.inspect}" unless object.instance_of?(String)

      @shape.match?(object)
    end
  end
end