Class: RSchema::Schemas::Pipeline

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

Overview

A schema that chains together an ordered list of other schemas

Examples:

A schema for positive floats

schema = RSchema.define do
  pipeline(
    _Float,
    predicate{ |f| f > 0.0 },
  )
end
schema.valid?(6.2) #=> true
schema.valid?('hi') #=> false (because it's not a Float)
schema.valid?(-6.2) #=> false (because predicate failed)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subschemas) ⇒ Pipeline


21
22
23
# File 'lib/rschema/schemas/pipeline.rb', line 21

def initialize(subschemas)
  @subschemas = subschemas
end

Instance Attribute Details

#subschemasObject (readonly)

Returns the value of attribute subschemas


19
20
21
# File 'lib/rschema/schemas/pipeline.rb', line 19

def subschemas
  @subschemas
end

Instance Method Details

#call(value, options) ⇒ Object


25
26
27
28
29
30
31
32
33
34
# File 'lib/rschema/schemas/pipeline.rb', line 25

def call(value, options)
  result = Result.success(value)

  subschemas.each do |subsch|
    result = subsch.call(result.value, options)
    break if result.invalid?
  end

  result
end

#with_wrapped_subschemas(wrapper) ⇒ Object


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

def with_wrapped_subschemas(wrapper)
  wrapped_subschemas = subschemas.map{ |ss| wrapper.wrap(ss) }
  self.class.new(wrapped_subschemas)
end