Class: Workarea::Configuration::Params

Inherits:
Object
  • Object
show all
Defined in:
app/models/workarea/configuration/params.rb

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Params

Returns a new instance of Params.



4
5
6
# File 'app/models/workarea/configuration/params.rb', line 4

def initialize(params)
  @params = params
end

Instance Method Details

#array(field, value) ⇒ Object



29
30
31
# File 'app/models/workarea/configuration/params.rb', line 29

def array(field, value)
  Array.wrap(CSV.parse(value).first).map(&:strip)
end

#duration(field, value) ⇒ Object



40
41
42
# File 'app/models/workarea/configuration/params.rb', line 40

def duration(field, value)
  value.first.to_i.send(value.last.to_sym)
end

#hash(field, value) ⇒ Object



33
34
35
36
37
38
# File 'app/models/workarea/configuration/params.rb', line 33

def hash(field, value)
  value.each_slice(2)
       .to_h
       .reject { |k, v| k.blank? || v.blank? }
       .transform_values { |v| field.values_type_class.mongoize(v) }
end

#to_hObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/models/workarea/configuration/params.rb', line 8

def to_h
  Admin.definition.fields.each_with_object({}) do |field, memo|
    next unless @params.key?(field.key)
    value = @params[field.key]

    formatted_value =
      if value.present? && respond_to?(field.type)
        send(field.type, field, value)
      else
        value
      end

    memo[field.key] =
      if formatted_value.blank? && !field.allow_blank?
        field.default
      else
        formatted_value
      end
  end
end