Sha256: 40e8c9331e63520f4c05570774a116e1b4d6f0fe5b3f6d6bf19f99ba8cc893c7

Contents?: true

Size: 799 Bytes

Versions: 5

Compression:

Stored size: 799 Bytes

Contents

InputSanitizer::V1::CleanField = MethodStruct.new(:data, :has_key, :converter, :required, :collection, :namespace, :default, :provide, :allow) do
  def call
    if has_key
      convert
    elsif default
      converter.call(default)
    elsif required
      raise InputSanitizer::ValueMissingError
    else
      raise InputSanitizer::OptionalValueOmitted
    end
  end

  private
  def convert
    if collection
      data.map { |value| convert_single(value) }
    else
      convert_single(data)
    end
  end

  def convert_single(value)
    if namespace
      { namespace => convert_value(value[namespace]) }
    else
      convert_value(value)
    end
  end

  def convert_value(value)
    if provide
      converter.call(value, provide)
    else
      converter.call(value)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
input_sanitizer-0.6.0 lib/input_sanitizer/v1/clean_field.rb
input_sanitizer-0.5.0 lib/input_sanitizer/v1/clean_field.rb
input_sanitizer-0.4.1 lib/input_sanitizer/v1/clean_field.rb
input_sanitizer-0.4.0 lib/input_sanitizer/v1/clean_field.rb
input_sanitizer-0.3.33 lib/input_sanitizer/v1/clean_field.rb