Class: ShapeOf::Regexp

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

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

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
352
353
354
355
356
357
# 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)
      unless object.instance_of?(::Regexp) || object.instance_of?(String)
        raise TypeError, "expected #{::Regexp.inspect} or #{String.inspect}, was instead #{object.inspect}"
      end

      if object.instance_of?(::Regexp)
        @shape == object
      else # string
        @shape.match?(object)
      end
    end
  end
end

.shape_of?(object) ⇒ Boolean

Returns:



322
323
324
# File 'lib/shape_of.rb', line 322

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