Class: Workarea::HashUpdate

Inherits:
Object
  • Object
show all
Defined in:
app/services/workarea/hash_update.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adds: [], updates: [], removes: []) ⇒ HashUpdate

Returns a new instance of HashUpdate.



8
9
10
11
12
# File 'app/services/workarea/hash_update.rb', line 8

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

Class Method Details

.parse_values(value) ⇒ Object



3
4
5
6
# File 'app/services/workarea/hash_update.rb', line 3

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

Instance Method Details

#apply(hash) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/services/workarea/hash_update.rb', line 14

def apply(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