class RequestParamsValidation::Definitions::Param
Attributes
allow_blank[R]
custom_validation[R]
decimal_precision[R]
elements[R]
format[R]
if_given[R]
inclusion[R]
key[R]
length[R]
rename_as[R]
required[R]
transform[R]
type[R]
value[R]
Public Class Methods
new(options, &block)
click to toggle source
# File lib/request_params_validation/definitions/param.rb, line 9 def initialize(options, &block) @key = options[:key] @required = options[:required] @allow_blank = options[:allow_blank] @type = options[:type].try(:to_sym) @rename_as = options[:as].try(:to_sym) @default = options[:default] @transform = options[:transform] @decimal_precision = options[:precision] @element_of_array = options[:element_of_array] @inclusion = build_inclusion_option(options[:inclusion]) @length = build_length_option(options[:length]) @value = build_value_option(options[:value]) @format = build_format_option(options[:format]) @custom_validation = build_custom_validation_option(options[:validate]) @if_given = build_if_given_option(options[:if_given]) @elements = build_elements_option(options[:elements], &block) @sub_definition = build_sub_definition(&block) end
Public Instance Methods
default()
click to toggle source
# File lib/request_params_validation/definitions/param.rb, line 36 def default @default.respond_to?(:call) ? @default.call : @default end
element_of_array?()
click to toggle source
# File lib/request_params_validation/definitions/param.rb, line 44 def element_of_array? !!@element_of_array end
has_default?()
click to toggle source
# File lib/request_params_validation/definitions/param.rb, line 32 def has_default? !@default.nil? # default value could be `false` end
rename?()
click to toggle source
# File lib/request_params_validation/definitions/param.rb, line 78 def rename? !!@rename_as end
skip?(request_params)
click to toggle source
# File lib/request_params_validation/definitions/param.rb, line 86 def skip?(request_params) return false unless @if_given if_given_param_value = request_params[@if_given.param] if @if_given.function !@if_given.function.call(if_given_param_value) else if_given_param_value.blank? end end
sub_definition()
click to toggle source
# File lib/request_params_validation/definitions/param.rb, line 40 def sub_definition @sub_definition || @elements.try(:sub_definition) end
transform?()
click to toggle source
# File lib/request_params_validation/definitions/param.rb, line 82 def transform? !!@transform end
validate_custom_validation?()
click to toggle source
# File lib/request_params_validation/definitions/param.rb, line 74 def validate_custom_validation? !!@custom_validation end
validate_format?()
click to toggle source
# File lib/request_params_validation/definitions/param.rb, line 68 def validate_format? return false if [Params::DATE_TYPE, Params::DATETIME_TYPE].include?(@type) !!@format end
validate_inclusion?()
click to toggle source
# File lib/request_params_validation/definitions/param.rb, line 56 def validate_inclusion? !!@inclusion end
validate_length?()
click to toggle source
# File lib/request_params_validation/definitions/param.rb, line 60 def validate_length? !!@length end
validate_presence?()
click to toggle source
# File lib/request_params_validation/definitions/param.rb, line 48 def validate_presence? !!@required end
validate_type?()
click to toggle source
# File lib/request_params_validation/definitions/param.rb, line 52 def validate_type? !!@type end
validate_value?()
click to toggle source
# File lib/request_params_validation/definitions/param.rb, line 64 def validate_value? !!@value end
Private Instance Methods
build_custom_validation_option(validation)
click to toggle source
# File lib/request_params_validation/definitions/param.rb, line 160 def build_custom_validation_option(validation) case validation when Proc function = validation when Hash function = validation[:function] message = validation[:message] end return unless function Struct.new(:function, :message).new(function, message) end
build_elements_option(elements, &block)
click to toggle source
# File lib/request_params_validation/definitions/param.rb, line 188 def build_elements_option(elements, &block) return unless @type == Params::ARRAY_TYPE elements_options = { key: @key, element_of_array: true } case elements when Hash elements_options.merge!(elements) when String, Symbol elements_options.merge!(type: elements) end self.class.new(elements_options, &block) end
build_format_option(format)
click to toggle source
# File lib/request_params_validation/definitions/param.rb, line 143 def build_format_option(format) case format when Regexp regexp = format when String strptime = format when Hash strptime = format[:strptime] regexp = format[:regexp] message = format[:message] end return if regexp.nil? && !strptime Struct.new(:regexp, :strptime, :message).new(regexp, strptime, message) end
build_if_given_option(if_given)
click to toggle source
# File lib/request_params_validation/definitions/param.rb, line 174 def build_if_given_option(if_given) case if_given when String, Symbol param = if_given.to_sym when Hash param = if_given.first.try(:first) function = if_given.first.try(:last) end return unless param Struct.new(:param, :function).new(param, function) end
build_inclusion_option(inclusion)
click to toggle source
# File lib/request_params_validation/definitions/param.rb, line 100 def build_inclusion_option(inclusion) case inclusion when Array include_in = inclusion when Hash include_in = inclusion[:in] message = inclusion[:message] end return unless include_in Struct.new(:in, :message).new(include_in, message) end
build_length_option(length)
click to toggle source
# File lib/request_params_validation/definitions/param.rb, line 114 def build_length_option(length) case length when Integer min = length max = length when Hash min = length[:min] max = length[:max] message = length[:message] end return unless min || max Struct.new(:min, :max, :message).new(min, max, message) end
build_sub_definition(&block)
click to toggle source
# File lib/request_params_validation/definitions/param.rb, line 206 def build_sub_definition(&block) return unless @type == Params::HASH_TYPE request = Request.new request.instance_eval(&block) if block_given? request.params end
build_value_option(value)
click to toggle source
# File lib/request_params_validation/definitions/param.rb, line 130 def build_value_option(value) case value when Hash min = value[:min] max = value[:max] message = value[:message] end return unless min || max Struct.new(:min, :max, :message).new(min, max, message) end