Class: Anonymizer
- Inherits:
-
Object
- Object
- Anonymizer
- Defined in:
- lib/anonymizer.rb
Instance Attribute Summary collapse
-
#anonymizations ⇒ Object
a hash, the keys are the original filepaths, the values are the corresponding anonymized names.
-
#destination_dir ⇒ Object
a string, the directory to which all of the anoymizations and the key should be copied.
-
#filepaths ⇒ Object
an array of strings, the paths to the items to be anonymized.
Instance Method Summary collapse
-
#anonymize ⇒ Object
Reshuffles the anonymized names.
-
#initialize(filepaths) ⇒ Anonymizer
constructor
Creates a new instance with an array of filepaths (strings) A new set of anaonymizations will be created automatically at initialization.
-
#record_to(destination_dir, options = {}) ⇒ Object
Copies the collection of files/directories to the destination directory using the current anonymized names.
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
#anonymizations ⇒ Object
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_dir ⇒ Object
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 |
#filepaths ⇒ Object
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
#anonymize ⇒ Object
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”)
44 45 46 47 48 49 50 51 52 |
# File 'lib/anonymizer.rb', line 44 def record_to(destination_dir, = {}) 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 = [:keymap_name].nil? ? "keymap" : [:keymap_name] write_yml(destination_dir, keymap_filename + '.yml') write_csv(destination_dir, keymap_filename + '.csv') if [:include_csv] end |