Class: DataAnon::Strategy::MongoDB::Whitelist

Inherits:
Base
  • Object
show all
Defined in:
lib/strategy/mongodb/whitelist.rb

Direct Known Subclasses

Blacklist

Instance Attribute Summary

Attributes inherited from Base

#errors, #fields, #fields_missing_strategy, #user_strategies

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#anonymize, #batch_size, #default_strategy, #initialize, #is_primary_key?, #primary_key, #process, #process_fields, #process_table, #process_table_in_batches, #progress_bar, #progress_bar_class, #whitelist

Methods included from Utils::Logging

#logger, #logger=

Constructor Details

This class inherits a constructor from DataAnon::Strategy::Base

Class Method Details

.whitelist?Boolean

Returns:

  • (Boolean)

12
13
14
# File 'lib/strategy/mongodb/whitelist.rb', line 12

def self.whitelist?
  true
end

Instance Method Details

#anonymize_document(document, index, field_strategies = {}) ⇒ Object


45
46
47
48
49
50
51
52
53
54
55
# File 'lib/strategy/mongodb/whitelist.rb', line 45

def anonymize_document document, index, field_strategies = {}
  anonymized_document = {}
  document.each do |field_name, field_value|
    field_strategy = field_strategies[field_name.downcase] if field_strategies.kind_of?(Hash)
    unless field_value.nil?
      field = DataAnon::Core::Field.new(field_name, field_value, index, document, @name)
      anonymized_document[field.name] = AnonymizeField.new(field, field_strategy, self).anonymize
    end
  end
  anonymized_document
end

#collection(field, &block) ⇒ Object Also known as: document


16
17
18
19
20
# File 'lib/strategy/mongodb/whitelist.rb', line 16

def collection field, &block
  whitelist = self.class.new @source_database, @destination_database, @name, @user_strategies
  whitelist.process_fields &block
  @fields[field] = whitelist.fields
end

#dest_collectionObject Also known as: dest_table


29
30
31
32
# File 'lib/strategy/mongodb/whitelist.rb', line 29

def dest_collection
  database = @destination_database
  @dest_collection ||= mongo_collection(database)
end

#mongo_collection(database) ⇒ Object


24
25
26
27
# File 'lib/strategy/mongodb/whitelist.rb', line 24

def mongo_collection(database)
  options = database[:options] || {}
  Mongo::Connection.from_uri(database[:mongodb_uri], options)[database[:database]][@name]
end

#process_record(index, document) ⇒ Object


41
42
43
# File 'lib/strategy/mongodb/whitelist.rb', line 41

def process_record index, document
  dest_collection.insert anonymize_document(document, index, @fields)
end

#source_collectionObject Also known as: source_table


34
35
36
# File 'lib/strategy/mongodb/whitelist.rb', line 34

def source_collection
  @source_collection ||= mongo_collection(@source_database)
end