Class: RSchema::Coercers::Chain

Inherits:
Object
  • Object
show all
Defined in:
lib/rschema/coercers/chain.rb

Overview

Applies a list of coercers, in order

Defined Under Namespace

Classes: Builder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subcoercers) ⇒ Chain


15
16
17
# File 'lib/rschema/coercers/chain.rb', line 15

def initialize(subcoercers)
  @subcoercers = subcoercers
end

Instance Attribute Details

#subcoercersObject (readonly)

Returns the value of attribute subcoercers


9
10
11
# File 'lib/rschema/coercers/chain.rb', line 9

def subcoercers
  @subcoercers
end

Class Method Details

.[](*subbuilders) ⇒ Object


11
12
13
# File 'lib/rschema/coercers/chain.rb', line 11

def self.[](*subbuilders)
  Builder.new(subbuilders)
end

Instance Method Details

#call(value) ⇒ Object


19
20
21
22
23
24
25
26
# File 'lib/rschema/coercers/chain.rb', line 19

def call(value)
  result = Result.success(value)
  subcoercers.each do |coercer|
    result = coercer.call(result.value)
    break if result.invalid?
  end
  result
end

#will_affect?(value) ⇒ Boolean


28
29
30
# File 'lib/rschema/coercers/chain.rb', line 28

def will_affect?(value)
  subcoercers.any? { |sc| sc.will_affect?(value) }
end