Class: Wallaby::ActiveRecord::ModelServiceProvider::Normalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/adaptors/wallaby/active_record/model_service_provider/normalizer.rb

Overview

Normalize the values for a model

Instance Method Summary collapse

Constructor Details

#initialize(model_decorator) ⇒ Normalizer

Returns a new instance of Normalizer.

Parameters:



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

Parameters:

  • params (ActionController::Parameters)


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

Parameters:

  • params (ActionController::Parameters)
  • field_name (String)
  • values (Object)


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

Parameters:

  • params (ActionController::Parameters)
  • field_name (String)
  • values (Array)


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

Parameters:

  • params (ActionController::Parameters)
  • field_name (String)
  • values (Array)


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