Module: PersistentMap::Include

Included in:
HammerCLIImport::BaseCommand
Defined in:
lib/hammer_cli_import/persistentmap.rb

Instance Method Summary collapse

Instance Method Details

#load_persistent_mapsObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/hammer_cli_import/persistentmap.rb', line 109

def load_persistent_maps
  @pm = {}
  maps.each do |map_sym|
    hash = {}
    Dir[File.join data_dir, "#{map_sym}-*.csv"].sort.each do |filename|
      reader = CSV.open(filename, 'r')
      header = reader.shift
      raise PersistentMapError, "Importing :#{map_sym} from file #{filename}" unless header == (pm_csv_headers map_sym)
      reader.each do |row|
        key, value = pm_decode_row map_sym, row
        delkey = row[-1] == '-'
        if delkey
          hash.delete key
        else
          hash[key] = value
        end
      end
    end
    @pm[map_sym] = add_checks(DeltaHash[hash], self.class.map_description[map_sym], map_sym)
  end
end

#map_target_entityObject



105
106
107
# File 'lib/hammer_cli_import/persistentmap.rb', line 105

def map_target_entity
  self.class.map_target_entity
end

#mapsObject



101
102
103
# File 'lib/hammer_cli_import/persistentmap.rb', line 101

def maps
  self.class.maps
end

#prune_persistent_maps(cache) ⇒ Object

Consider entities deleted if they are not present in cache



151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/hammer_cli_import/persistentmap.rb', line 151

def prune_persistent_maps(cache)
  maps.each do |map_sym|
    entity_ids = cache[map_target_entity[map_sym]].keys
    pm_hash = @pm[map_sym].to_hash
    extra = pm_hash.values.to_set - entity_ids.to_set

    next if extra.empty?

    debug "Removing #{map_sym} from persistent map: #{extra.to_a.join(' ')}"
    pm_hash.each do |key, value|
      @pm[map_sym].delete key if extra.include? value
    end
  end
end

#save_persistent_mapsObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/hammer_cli_import/persistentmap.rb', line 131

def save_persistent_maps
  maps.each do |map_sym|
    next unless @pm[map_sym].changed?
    CSV.open((File.join data_dir, "#{map_sym}-#{Time.now.utc.iso8601}.csv"), 'wb') do |csv|
      csv << (pm_csv_headers map_sym)
      @pm[map_sym].new.each do |key, value|
        key = [key] unless key.is_a? Array
        value = [value] unless value.is_a? Array
        csv << key + value + [nil]
      end
      delval = [nil] * (val_arity map_sym)
      @pm[map_sym].del.each do |key|
        key = [key] unless key.is_a? Array
        csv << key + delval + ['-']
      end
    end
  end
end