Class: Brita::TypeValidator
- Inherits:
-
Object
- Object
- Brita::TypeValidator
- Defined in:
- lib/brita/type_validator.rb
Overview
TypeValidator validates that the incoming param is of the specified type
Constant Summary collapse
- RANGE_PATTERN =
{ format: { with: /\A.+(?:[^.]\.\.\.[^.]).+\z/, message: "must be a range" } }.freeze
- DECIMAL_PATTERN =
{ numericality: true, allow_nil: true }.freeze
- BOOLEAN_PATTERN =
{ inclusion: { in: [true, false] }, allow_nil: true }.freeze
- WHITELIST_TYPES =
[:int, :decimal, :boolean, :string, :text, :date, :time, :datetime, :scope].freeze
Instance Attribute Summary collapse
-
#param ⇒ Object
readonly
Returns the value of attribute param.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#initialize(param, type) ⇒ TypeValidator
constructor
A new instance of TypeValidator.
- #valid_type? ⇒ Boolean
- #validate ⇒ Object
Constructor Details
#initialize(param, type) ⇒ TypeValidator
Returns a new instance of TypeValidator.
18 19 20 21 |
# File 'lib/brita/type_validator.rb', line 18 def initialize(param, type) @param = param @type = type end |
Instance Attribute Details
#param ⇒ Object (readonly)
Returns the value of attribute param.
23 24 25 |
# File 'lib/brita/type_validator.rb', line 23 def param @param end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
23 24 25 |
# File 'lib/brita/type_validator.rb', line 23 def type @type end |
Instance Method Details
#valid_type? ⇒ Boolean
38 39 40 |
# File 'lib/brita/type_validator.rb', line 38 def valid_type? WHITELIST_TYPES.include?(type) end |
#validate ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/brita/type_validator.rb', line 25 def validate case type when :datetime, :date, :time RANGE_PATTERN when :int valid_int? when :decimal DECIMAL_PATTERN when :boolean BOOLEAN_PATTERN end end |