Sha256: e083a4863bb3370b736ab5a1befe49ee6db9c1f6c845effb9af78c825a727d52

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

module InputSanitizer
  class PositiveIntegerConverter < IntegerConverter
    def call(value)
      val = super
      raise ConversionError.new("invalid integer (neagtive or zero)") if val <= 0
      val
    end
  end

  class CommaJoinedIntegersConverter
    def call(value)
      non_valid = value.gsub(/[0-9,]/, "")
      if non_valid.empty?
        parts = value.split(",").map(&:to_i)
      else
        invalid_chars = non_valid.split(//)
        invalid_chars_desc = invalid_chars.join(", ")
        raise InputSanitizer::ConversionError.new("Invalid chars: #{invalid_chars_desc}")
      end
    end
  end

  class SpecificValuesConverter
    def initialize(values)
      @valid_values = values
    end

    def call(value)
      found = @valid_values.include?(value) ? value : nil
      if !found
        found = @valid_values.include?(value.to_sym) ? value.to_sym : nil
      end
      if !found
        values_joined = @valid_values.join(", ")
        error_message = "Possible values: #{values_joined}"
        raise InputSanitizer::ConversionError.new(error_message)
      else
        found
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
input_sanitizer-0.2.0 lib/input_sanitizer/extended_converters.rb
input_sanitizer-0.1.10 lib/input_sanitizer/extended_converters.rb
input_sanitizer-0.1.8 lib/input_sanitizer/extended_converters.rb