lib/active_interaction/filter.rb
def clean(value, context)
      value = cast(value, context)
      if value.nil?
        default(context)
      else
        value
      end
    end
# Convert a value into the expected type. If no value is given, fall back
#   to the default value.
#
# @example
#   ActiveInteraction::Filter.new(:example).clean(nil, nil)
#   # => ActiveInteraction::MissingValueError: example
# @example
#   ActiveInteraction::Filter.new(:example).clean(0, nil)
#   # => ActiveInteraction::InvalidValueError: example: 0
# @example
#   ActiveInteraction::Filter.new(:example, default: nil).clean(nil, nil)
#   # => nil
# @example
#   ActiveInteraction::Filter.new(:example, default: 0).clean(nil, nil)
#   # => ActiveInteraction::InvalidDefaultError: example: 0
#
# @param value [Object]
# @param context [Base, nil]
#
# @return [Object]
#
# @raise [MissingValueError] If the value is missing and there is no
#   default.
# @raise [InvalidValueError] If the value is invalid.
# @raise (see #default)
Convert a value into the expected type. If no value is given, fall back
to the default value.
Convert a value into the expected type . If no value is given fall back to the default value .
def clean(value, context)
      value = cast(value, context)
      if value.nil?
        default(context)
      else
        value
      end
    end
def clean ( value , context ) value = cast ( value , context ) if value . nil? default ( context ) else value end end
Convert a value into the expected type.
Convert a value into the expected type .
None
Object
[MissingValueError] If the value is missing and there is no default.
[ MissingValueError ] If the value is missing and there is no default .
[InvalidValueError] If the value is invalid.
[ InvalidValueError ] If the value is invalid .
None
value
context
None
example
:Filter.new(:example).clean(nil, nil) # => ActiveInteraction::MissingValueError: example
: Filter . new ( : example ) . clean ( nil nil ) # = > ActiveInteraction :: MissingValueError : example
example
:Filter.new(:example).clean(0, nil) # => ActiveInteraction::InvalidValueError: example: 0
: Filter . new ( : example ) . clean ( 0 nil ) # = > ActiveInteraction :: InvalidValueError : example : 0
example
None
example
:Filter.new(:example, default: 0).clean(nil, nil) # => ActiveInteraction::InvalidDefaultError: example: 0
: Filter . new ( : example default : 0 ) . clean ( nil nil ) # = > ActiveInteraction :: InvalidDefaultError : example : 0