Class: RequestParamsValidation::Definitions::Param

Inherits:
Object
  • Object
show all
Defined in:
lib/request_params_validation/definitions/param.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, &block) ⇒ Param

Returns a new instance of Param.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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

Instance Attribute Details

#allow_blankObject (readonly)

Returns the value of attribute allow_blank.



6
7
8
# File 'lib/request_params_validation/definitions/param.rb', line 6

def allow_blank
  @allow_blank
end

#custom_validationObject (readonly)

Returns the value of attribute custom_validation.



6
7
8
# File 'lib/request_params_validation/definitions/param.rb', line 6

def custom_validation
  @custom_validation
end

#decimal_precisionObject (readonly)

Returns the value of attribute decimal_precision.



6
7
8
# File 'lib/request_params_validation/definitions/param.rb', line 6

def decimal_precision
  @decimal_precision
end

#elementsObject (readonly)

Returns the value of attribute elements.



6
7
8
# File 'lib/request_params_validation/definitions/param.rb', line 6

def elements
  @elements
end

#formatObject (readonly)

Returns the value of attribute format.



6
7
8
# File 'lib/request_params_validation/definitions/param.rb', line 6

def format
  @format
end

#if_givenObject (readonly)

Returns the value of attribute if_given.



6
7
8
# File 'lib/request_params_validation/definitions/param.rb', line 6

def if_given
  @if_given
end

#inclusionObject (readonly)

Returns the value of attribute inclusion.



6
7
8
# File 'lib/request_params_validation/definitions/param.rb', line 6

def inclusion
  @inclusion
end

#keyObject (readonly)

Returns the value of attribute key.



6
7
8
# File 'lib/request_params_validation/definitions/param.rb', line 6

def key
  @key
end

#lengthObject (readonly)

Returns the value of attribute length.



6
7
8
# File 'lib/request_params_validation/definitions/param.rb', line 6

def length
  @length
end

#rename_asObject (readonly)

Returns the value of attribute rename_as.



6
7
8
# File 'lib/request_params_validation/definitions/param.rb', line 6

def rename_as
  @rename_as
end

#requiredObject (readonly)

Returns the value of attribute required.



6
7
8
# File 'lib/request_params_validation/definitions/param.rb', line 6

def required
  @required
end

#transformObject (readonly)

Returns the value of attribute transform.



6
7
8
# File 'lib/request_params_validation/definitions/param.rb', line 6

def transform
  @transform
end

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/request_params_validation/definitions/param.rb', line 6

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



6
7
8
# File 'lib/request_params_validation/definitions/param.rb', line 6

def value
  @value
end

Instance Method Details

#defaultObject



36
37
38
# File 'lib/request_params_validation/definitions/param.rb', line 36

def default
  @default.respond_to?(:call) ? @default.call : @default
end

#element_of_array?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/request_params_validation/definitions/param.rb', line 44

def element_of_array?
  !!@element_of_array
end

#has_default?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/request_params_validation/definitions/param.rb', line 32

def has_default?
  !@default.nil? # default value could be `false`
end

#rename?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/request_params_validation/definitions/param.rb', line 78

def rename?
  !!@rename_as
end

#skip?(request_params) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
94
95
96
# 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_definitionObject



40
41
42
# File 'lib/request_params_validation/definitions/param.rb', line 40

def sub_definition
  @sub_definition || @elements.try(:sub_definition)
end

#transform?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/request_params_validation/definitions/param.rb', line 82

def transform?
  !!@transform
end

#validate_custom_validation?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/request_params_validation/definitions/param.rb', line 74

def validate_custom_validation?
  !!@custom_validation
end

#validate_format?Boolean

Returns:

  • (Boolean)


68
69
70
71
72
# 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?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/request_params_validation/definitions/param.rb', line 56

def validate_inclusion?
  !!@inclusion
end

#validate_length?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/request_params_validation/definitions/param.rb', line 60

def validate_length?
  !!@length
end

#validate_presence?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/request_params_validation/definitions/param.rb', line 48

def validate_presence?
  !!@required
end

#validate_type?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/request_params_validation/definitions/param.rb', line 52

def validate_type?
  !!@type
end

#validate_value?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/request_params_validation/definitions/param.rb', line 64

def validate_value?
  !!@value
end