Class: RSchema::Coercers::Any

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

Overview

Applies subcoercers, in order, until one succeeds

Defined Under Namespace

Classes: Builder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subcoercers) ⇒ Any

Returns a new instance of Any.


15
16
17
# File 'lib/rschema/coercers/any.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/any.rb', line 9

def subcoercers
  @subcoercers
end

Class Method Details

.[](*subbuilders) ⇒ Object


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

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

Instance Method Details

#call(value) ⇒ Object


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

def call(value)
  subcoercers.each do |coercer|
    result = coercer.call(value)
    return result if result.valid?
  end
  Result.failure
end

#will_affect?(value) ⇒ Boolean

Returns:


27
28
29
# File 'lib/rschema/coercers/any.rb', line 27

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