Class: RSchema::Schemas::Type

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

Overview

A schema that matches values of a given type (i.e. value.is_a?(type))

Examples:

An Integer schema

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

A namespaced type

schema = RSchema.define do
  # This will not work:
  # _ActiveWhatever::Thing

  # This will work:
  type(ActiveWhatever::Thing)
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ Type


24
25
26
# File 'lib/rschema/schemas/type.rb', line 24

def initialize(type)
  @type = type
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type


22
23
24
# File 'lib/rschema/schemas/type.rb', line 22

def type
  @type
end

Instance Method Details

#call(value, _options) ⇒ Object


28
29
30
31
32
33
34
# File 'lib/rschema/schemas/type.rb', line 28

def call(value, _options)
  if value.is_a?(@type)
    Result.success(value)
  else
    Result.failure(error(value))
  end
end

#with_wrapped_subschemas(_wrapper) ⇒ Object


36
37
38
# File 'lib/rschema/schemas/type.rb', line 36

def with_wrapped_subschemas(_wrapper)
  self
end