Class: ShapeOf::Pattern

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

Overview

Matches a Regexp against a String using Regexp#match?. Pretty much a wrapper around Regexp because a Regexp instance will be tested for equality in the ShapeOf::Hash and ShapeOf::Array since it’s not a class.

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)


326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/shape_of.rb', line 326

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