Class: Wallaby::ActiveRecord::ModelServiceProvider::Normalizer
- Inherits:
-
Object
- Object
- Wallaby::ActiveRecord::ModelServiceProvider::Normalizer
- Defined in:
- lib/adapters/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
Returns a new instance of Normalizer.
9 10 11 |
# File 'lib/adapters/wallaby/active_record/model_service_provider/normalizer.rb', line 9 def initialize(model_decorator) @model_decorator = model_decorator end |
Instance Method Details
#normalize(params) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/adapters/wallaby/active_record/model_service_provider/normalizer.rb', line 14 def normalize(params) params.each do |field_name, values| = @model_decorator.(field_name).presence || @model_decorator.(field_name) type = [:type].try(:[], /range|point|binary/) next unless type try "normalize_#{type}_values", params, field_name, values end params end |
#normalize_binary_values(params, field_name, values) ⇒ Object
Turn values into binary
49 50 51 |
# File 'lib/adapters/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
40 41 42 43 |
# File 'lib/adapters/wallaby/active_record/model_service_provider/normalizer.rb', line 40 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
31 32 33 34 |
# File 'lib/adapters/wallaby/active_record/model_service_provider/normalizer.rb', line 31 def normalize_range_values(params, field_name, values) normalized = Array(values).map(&:presence).compact params[field_name] = ((normalized.present? && values.length == 2) && (values.first...values.last)) || nil end |