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
# 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])

  @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

#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



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

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

#element_of_array?Boolean

Returns:

  • (Boolean)


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

def element_of_array?
  !!@element_of_array
end

#has_default?Boolean

Returns:

  • (Boolean)


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

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

#rename?Boolean

Returns:

  • (Boolean)


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

def rename?
  !!@rename_as
end

#sub_definitionObject



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

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

#transform?Boolean

Returns:

  • (Boolean)


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

def transform?
  !!@transform
end

#validate_custom_validation?Boolean

Returns:

  • (Boolean)


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

def validate_custom_validation?
  !!@custom_validation
end

#validate_format?Boolean

Returns:

  • (Boolean)


67
68
69
70
71
# File 'lib/request_params_validation/definitions/param.rb', line 67

def validate_format?
  return false if [Params::DATE_TYPE, Params::DATETIME_TYPE].include?(@type)

  !!@format
end

#validate_inclusion?Boolean

Returns:

  • (Boolean)


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

def validate_inclusion?
  !!@inclusion
end

#validate_length?Boolean

Returns:

  • (Boolean)


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

def validate_length?
  !!@length
end

#validate_presence?Boolean

Returns:

  • (Boolean)


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

def validate_presence?
  !!@required
end

#validate_type?Boolean

Returns:

  • (Boolean)


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

def validate_type?
  !!@type
end

#validate_value?Boolean

Returns:

  • (Boolean)


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

def validate_value?
  !!@value
end