Sha256: 6988ef3bf881dd8b4e380b8cd119f916bfcaf67f2b4dacf9b20a6b8532694e7a

Contents?: true

Size: 1.15 KB

Versions: 3

Compression:

Stored size: 1.15 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)
             .map { |v| field.values_type_class.mongoize(v) }
      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

3 entries across 3 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/models/workarea/configuration/params.rb
workarea-core-3.5.26 app/models/workarea/configuration/params.rb
workarea-core-3.5.25 app/models/workarea/configuration/params.rb