Class: ImportManager::Status

Inherits:
Hash
  • Object
show all
Defined in:
lib/import_manager/status.rb,
lib/import_manager/status/counts.rb

Defined Under Namespace

Classes: Counts

Instance Method Summary collapse

Constructor Details

#initialize(status) ⇒ Status

Returns a new instance of Status.

Parameters:

  • initial_status (Hash, Integer, String)

    a hash, a hash as json string or just the total number of imports



7
8
9
10
11
12
13
# File 'lib/import_manager/status.rb', line 7

def initialize status
  hash = normalize_init_args status
  counts = hash.delete(:counts)
  replace hash
  self[:counts] = Counts.new counts
  init_missing_values
end

Instance Method Details

#init_missing_valuesObject



15
16
17
18
19
20
21
# File 'lib/import_manager/status.rb', line 15

def init_missing_values
  self[:errors] ||= Hash.new { |h, k| h[k] = [] }
  self[:reports] ||= Hash.new { |h, k| h[k] = [] }
  %i[imported skipped overridden failed].each do |n|
    self[n] ||= {}
  end
end

#normalize_init_args(status) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/import_manager/status.rb', line 23

def normalize_init_args status
  sym_hash = case status
             when Integer
               { counts: { total: status } }
             when String
               JSON.parse status
             when Hash
               status
             else
               {}
             end
  unstringify_keys sym_hash
rescue JSON::ParserError => _e
  {}
end

#unstringify_keys(hash) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/import_manager/status.rb', line 39

def unstringify_keys hash
  hash.deep_symbolize_keys!
  hash.keys.each do |k|
    next if k == :counts || !hash[k].is_a?(Hash)
    hash[k] = hash[k].each_with_object({}) do |(key, value), options|
      options[(Integer(key.to_s) rescue key)] = value
    end
  end
  hash
end