Class: RSchema::Schemas::Maybe

Inherits:
Object
  • Object
show all
Defined in:
lib/rschema/schemas/maybe.rb

Overview

A schema representing that a value may be nil

If the value is not nil, it must conform to the subschema

Examples:

A nil-able Integer

schema = RSchema.define{ maybe(_Integer) }
schema.valid?(5) #=> true
schema.valid?(nil) #=> true

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subschema) ⇒ Maybe

Returns a new instance of Maybe.


18
19
20
# File 'lib/rschema/schemas/maybe.rb', line 18

def initialize(subschema)
  @subschema = subschema
end

Instance Attribute Details

#subschemaObject (readonly)

Returns the value of attribute subschema


16
17
18
# File 'lib/rschema/schemas/maybe.rb', line 16

def subschema
  @subschema
end

Instance Method Details

#call(value, options) ⇒ Object


22
23
24
25
26
27
28
# File 'lib/rschema/schemas/maybe.rb', line 22

def call(value, options)
  if value.nil?
    Result.success(value)
  else
    @subschema.call(value, options)
  end
end

#with_wrapped_subschemas(wrapper) ⇒ Object


30
31
32
# File 'lib/rschema/schemas/maybe.rb', line 30

def with_wrapped_subschemas(wrapper)
  self.class.new(wrapper.wrap(subschema))
end