Sha256: 2f62edb58d399847a1afb2467d14694d1d39f6ea89d813bcb137b7037f191af5

Contents?: true

Size: 1.07 KB

Versions: 25

Compression:

Stored size: 1.07 KB

Contents

module Workarea
  module Configuration
    class Params
      def initialize(params)
        @params = params
      end

      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

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

      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

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

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
workarea-core-3.5.23 app/models/workarea/configuration/params.rb
workarea-core-3.5.22 app/models/workarea/configuration/params.rb
workarea-core-3.5.21 app/models/workarea/configuration/params.rb
workarea-core-3.5.20 app/models/workarea/configuration/params.rb
workarea-core-3.5.19 app/models/workarea/configuration/params.rb
workarea-core-3.5.18 app/models/workarea/configuration/params.rb
workarea-core-3.5.17 app/models/workarea/configuration/params.rb
workarea-core-3.5.16 app/models/workarea/configuration/params.rb
workarea-core-3.5.15 app/models/workarea/configuration/params.rb
workarea-core-3.5.14 app/models/workarea/configuration/params.rb
workarea-core-3.5.13 app/models/workarea/configuration/params.rb
workarea-core-3.5.12 app/models/workarea/configuration/params.rb
workarea-core-3.5.11 app/models/workarea/configuration/params.rb
workarea-core-3.5.10 app/models/workarea/configuration/params.rb
workarea-core-3.5.9 app/models/workarea/configuration/params.rb
workarea-core-3.5.8 app/models/workarea/configuration/params.rb
workarea-core-3.5.7 app/models/workarea/configuration/params.rb
workarea-core-3.5.6 app/models/workarea/configuration/params.rb
workarea-core-3.5.5 app/models/workarea/configuration/params.rb
workarea-core-3.5.4 app/models/workarea/configuration/params.rb