Class: Kharon::Processors::NumericProcessor
- Inherits:
-
Kharon::Processor
- Object
- Kharon::Processor
- Kharon::Processors::NumericProcessor
- Defined in:
- lib/kharon/processors/numeric_processor.rb
Overview
Processor to validate integers. It has the :between, :round, :floor, :ceil, :min, and :max options with the default ones.
Instance Attribute Summary
Attributes inherited from Kharon::Processor
Instance Method Summary (collapse)
-
- (Object) process(key, options = {})
Checks if the given key is an integer or not.
-
- (Object) store(key, process, options)
Stores a decimal number, then apply the eventually passed round, ceil, or floor options.
Methods inherited from Kharon::Processor
Constructor Details
This class inherits a constructor from Kharon::Processor
Instance Method Details
- (Object) process(key, options = {})
Checks if the given key is an integer or not.
15 16 17 18 |
# File 'lib/kharon/processors/numeric_processor.rb', line 15 def process(key, = {}) before_all(key, ) match?(key, /\A([+-]?\d+)([,.](\d+))?\Z/) ? store(key, ->(item){item.to_s.sub(/,/, ".").to_f}, ) : raise_type_error(key, "Numeric") end |
- (Object) store(key, process, options)
Stores a decimal number, then apply the eventually passed round, ceil, or floor options.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/kharon/processors/numeric_processor.rb', line 24 def store(key, process, ) if(.has_key?(:between)) check_min_value(key, [:between][0]) check_max_value(key, [:between][1]) else check_min_value(key, [:min]) if(.has_key?(:min)) check_max_value(key, [:max]) if(.has_key?(:max)) end super(key, process, ) if .has_key?(:round) if [:round].kind_of?(Integer) validator.filtered[key] = validator.filtered[key].round([:round]) if validator.filtered.has_key?(key) elsif [:round] == true validator.filtered[key] = validator.filtered[key].round if validator.filtered.has_key?(key) end elsif(.has_key?(:floor) and [:floor] == true) validator.filtered[key] = validator.filtered[key].floor if validator.filtered.has_key?(key) elsif(.has_key?(:ceil) and [:ceil] == true) validator.filtered[key] = validator.filtered[key].ceil if validator.filtered.has_key?(key) end end |