Sha256: 104ca94cad1a52545c7a95fd5a8401e9c532b9675d8b7605af55c36f074b2a04

Contents?: true

Size: 1.11 KB

Versions: 17

Compression:

Stored size: 1.11 KB

Contents

module Workarea
  class HashUpdate
    def self.parse_values(value)
      parsed = CSV.parse(value).first
      parsed.map(&:to_s).map(&:strip).reject(&:blank?) if parsed.present?
    end

    def initialize(original: {}, adds: [], updates: [], removes: [])
      @original = original
      @adds = Array(adds).flatten.each_slice(2).to_a
      @updates = Array(updates).flatten.each_slice(2).to_a
      @removes = Array(removes).flatten
    end

    def result
      apply_to(@original.deep_dup)
    end

    # TODO v3.6 remove this method, doesn't work when the field is localized
    # @deprecated
    def apply(hash)
      Workarea.deprecation.deprecate_methods(self.class, apply: :result)
      apply_to(hash)
    end

    private

    def apply_to(hash)
      @adds.each do |tuple|
        key, value = *tuple
        hash[key] = self.class.parse_values(value)
      end

      @updates.each do |tuple|
        key, value = *tuple
        hash[key] = self.class.parse_values(value)
      end

      @removes.each do |key|
        hash.delete(key)
      end

      hash.delete_if { |k, v| k.blank? || v.blank? }
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/services/workarea/hash_update.rb
workarea-core-3.5.26 app/services/workarea/hash_update.rb
workarea-core-3.5.25 app/services/workarea/hash_update.rb
workarea-core-3.5.23 app/services/workarea/hash_update.rb
workarea-core-3.5.22 app/services/workarea/hash_update.rb
workarea-core-3.5.21 app/services/workarea/hash_update.rb
workarea-core-3.5.20 app/services/workarea/hash_update.rb
workarea-core-3.5.19 app/services/workarea/hash_update.rb
workarea-core-3.5.18 app/services/workarea/hash_update.rb
workarea-core-3.5.17 app/services/workarea/hash_update.rb
workarea-core-3.5.16 app/services/workarea/hash_update.rb
workarea-core-3.5.15 app/services/workarea/hash_update.rb
workarea-core-3.5.14 app/services/workarea/hash_update.rb
workarea-core-3.5.13 app/services/workarea/hash_update.rb
workarea-core-3.5.12 app/services/workarea/hash_update.rb
workarea-core-3.5.11 app/services/workarea/hash_update.rb
workarea-core-3.5.10 app/services/workarea/hash_update.rb