Class: Wallaby::ActiveRecord::ModelOperator::Normalizer

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

Instance Method Summary collapse

Constructor Details

#initialize(model_decorator) ⇒ Normalizer

Returns a new instance of Normalizer.



2
3
4
# File 'lib/adaptors/wallaby/active_record/model_operator/normalizer.rb', line 2

def initialize(model_decorator)
  @model_decorator = model_decorator
end

Instance Method Details

#normalize(params) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/adaptors/wallaby/active_record/model_operator/normalizer.rb', line 6

def normalize(params)
  params.each do |field_name, values|
    next unless  = @model_decorator.fields[field_name]

    case [:type]
    when /range/
      normalize_range_values params, field_name, values
    when /point/
      normalize_point_values params, field_name, values
    when /binary/
      normalize_binary_values params, field_name, values
    end
  end
end

#normalize_binary_values(params, field_name, values) ⇒ Object



39
40
41
42
43
# File 'lib/adaptors/wallaby/active_record/model_operator/normalizer.rb', line 39

def normalize_binary_values(params, field_name, values)
  if values.is_a?(ActionDispatch::Http::UploadedFile)
    params[field_name] = values.read
  end
end

#normalize_point_values(params, field_name, values) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/adaptors/wallaby/active_record/model_operator/normalizer.rb', line 30

def normalize_point_values(params, field_name, values)
  normalized = Array(values).map(&:presence).compact
  if normalized.length > 0
    params[field_name] = values.map &:to_f
  else
    params[field_name] = nil
  end
end

#normalize_range_values(params, field_name, values) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/adaptors/wallaby/active_record/model_operator/normalizer.rb', line 21

def normalize_range_values(params, field_name, values)
  normalized = Array(values).map(&:presence).compact
  if normalized.length > 0 && values.length == 2
    params[field_name] = values.first...values.last
  else
    params[field_name] = nil
  end
end