Class: Wallaby::ActiveRecord::ModelServiceProvider::Normalizer
- Inherits:
-
Object
- Object
- Wallaby::ActiveRecord::ModelServiceProvider::Normalizer
- Defined in:
- lib/adaptors/wallaby/active_record/model_service_provider/normalizer.rb
Overview
Normalize the values for a model
Instance Method Summary collapse
-
#initialize(model_decorator) ⇒ Normalizer
constructor
A new instance of Normalizer.
- #normalize(params) ⇒ Object
-
#normalize_binary_values(params, field_name, values) ⇒ Object
Turn values into binary.
-
#normalize_point_values(params, field_name, values) ⇒ Object
Turn values into points.
-
#normalize_range_values(params, field_name, values) ⇒ Object
Turn values into range.
Constructor Details
#initialize(model_decorator) ⇒ Normalizer
8 9 10 |
# File 'lib/adaptors/wallaby/active_record/model_service_provider/normalizer.rb', line 8 def initialize(model_decorator) @model_decorator = model_decorator end |
Instance Method Details
#normalize(params) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/adaptors/wallaby/active_record/model_service_provider/normalizer.rb', line 13 def normalize(params) params.each do |field_name, values| type = @model_decorator.(field_name)[:type] .try(:[], /range|point|binary/) next unless type public_send "normalize_#{type}_values", params, field_name, values end end |
#normalize_binary_values(params, field_name, values) ⇒ Object
Turn values into binary
49 50 51 52 53 |
# File 'lib/adaptors/wallaby/active_record/model_service_provider/normalizer.rb', line 49 def normalize_binary_values(params, field_name, values) params[field_name] = values.is_a?(::ActionDispatch::Http::UploadedFile) && values.read || nil end |
#normalize_point_values(params, field_name, values) ⇒ Object
Turn values into points
38 39 40 41 42 43 |
# File 'lib/adaptors/wallaby/active_record/model_service_provider/normalizer.rb', line 38 def normalize_point_values(params, field_name, values) normalized = Array(values).map(&:presence).compact params[field_name] = normalized.present? && values.map(&:to_f) || nil end |
#normalize_range_values(params, field_name, values) ⇒ Object
Turn values into range
26 27 28 29 30 31 32 |
# File 'lib/adaptors/wallaby/active_record/model_service_provider/normalizer.rb', line 26 def normalize_range_values(params, field_name, values) normalized = Array(values).map(&:presence).compact params[field_name] = if normalized.present? && values.length == 2 values.first...values.last end end |