Class: ShapeOf::Optional

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

Overview

Optional denotes that the usual type is a Shape, but is optional (meaning if it is nil or the key is not present in the Hash, it’s still true)

Class Method Summary collapse

Methods inherited from Shape

#initialize, shape_of?

Constructor Details

This class inherits a constructor from ShapeOf::Shape

Class Method Details

.[](shape) ⇒ Object

Raises:

  • (TypeError)


285
286
287
288
289
290
291
292
293
294
295
# File 'lib/shape_of.rb', line 285

def self.[](shape)
  raise TypeError, "Shape cannot be nil" if shape.nil? || shape == NilClass

  Union[shape, NilClass].tap do |this|
    new_class_name = this.name.sub('Union', 'Optional').sub(/(?<=\[).*(?=\])/, shape.inspect)
    this.instance_variable_set(:@class_name, new_class_name)
    def this.required?
      false
    end
  end
end

.required?Boolean

Returns:



297
298
299
# File 'lib/shape_of.rb', line 297

def self.required?
  false
end