Class: Anonymizer

Inherits:
Object
  • Object
show all
Defined in:
lib/anonymizer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filepaths) ⇒ Anonymizer

Creates a new instance with an array of filepaths (strings) A new set of anaonymizations will be created automatically at initialization. If you want to reshuffle the anonymizations you can call anonymize at any time afterward.



17
18
19
20
21
# File 'lib/anonymizer.rb', line 17

def initialize(filepaths)
  @filepaths = filepaths
  @anonymizations = Hash.new
  anonymize
end

Instance Attribute Details

#anonymizationsObject

a hash, the keys are the original filepaths, the values are the corresponding anonymized names



11
12
13
# File 'lib/anonymizer.rb', line 11

def anonymizations
  @anonymizations
end

#destination_dirObject

a string, the directory to which all of the anoymizations and the key should be copied



9
10
11
# File 'lib/anonymizer.rb', line 9

def destination_dir
  @destination_dir
end

#filepathsObject

an array of strings, the paths to the items to be anonymized



7
8
9
# File 'lib/anonymizer.rb', line 7

def filepaths
  @filepaths
end

Instance Method Details

#anonymizeObject

Reshuffles the anonymized names



25
26
27
28
29
30
31
32
33
# File 'lib/anonymizer.rb', line 25

def anonymize
  keys = Array.new(@filepaths)
  idx = 0
  ndigits = (Math.log10(@filepaths.length)+1).floor
  until keys.empty?
    @anonymizations[keys.pick!] = "%0#{ndigits}d" % idx
    idx += 1
  end
end

#record_to(destination_dir, options = {}) ⇒ Object

Copies the collection of files/directories to the destination directory using the current anonymized names. Records the anonymizations in a keymap.yml file.

Options

:inclue_csv => true – writes the csv format keymap in addition to the yaml

:keymap_name => “basename” – uses an alternate file basename for the keymap files (the default is “keymap”)

Raises:

  • (IOError)


44
45
46
47
48
49
50
51
52
# File 'lib/anonymizer.rb', line 44

def record_to(destination_dir, options = {})
  raise(IOError, "Destination directory not found.") unless File.exists?(destination_dir) and File.directory?(destination_dir)
  @anonymizations.each do |orig, anon|
    copy_directory_or_file(orig, File.join(destination_dir, anon))
  end
  keymap_filename = options[:keymap_name].nil? ? "keymap" : options[:keymap_name]
  write_yml(destination_dir, keymap_filename + '.yml')
  write_csv(destination_dir, keymap_filename + '.csv') if options[:include_csv]
end