Sha256: bac05739cf4d6159517f6cd58de2618f29e8cd8dde2bbc5c4ef51432dadd7006

Contents?: true

Size: 827 Bytes

Versions: 2

Compression:

Stored size: 827 Bytes

Contents

module Dragonfly
  module ParamValidators
    class InvalidParameter < RuntimeError; end

    module_function

    IS_NUMBER = ->(param) {
      param.is_a?(Numeric) || /\A[\d\.]+\z/ === param
    }

    IS_WORD = ->(param) {
      /\A\w+\z/ === param
    }

    IS_WORDS = ->(param) {
      /\A[\w ]+\z/ === param
    }

    def is_number; IS_NUMBER; end
    def is_word; IS_WORD; end
    def is_words; IS_WORDS; end

    def validate!(parameter, &validator)
      return if parameter.nil?
      raise InvalidParameter unless validator.(parameter)
    end

    def validate_all!(parameters, &validator)
      parameters.each { |p| validate!(p, &validator) }
    end

    def validate_all_keys!(obj, keys, &validator)
      parameters = keys.map { |key| obj[key] }
      validate_all!(parameters, &validator)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dragonfly-1.4.1 lib/dragonfly/param_validators.rb
dragonfly-1.4.0 lib/dragonfly/param_validators.rb