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.



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

def initialize(subschema)
  @subschema = subschema
end

Instance Attribute Details

#subschemaObject (readonly)

Returns the value of attribute subschema.



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

def subschema
  @subschema
end

Instance Method Details

#call(value, options) ⇒ Object



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

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

#with_wrapped_subschemas(wrapper) ⇒ Object



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

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