Class: DataAnon::Strategy::Base

Inherits:
Object
  • Object
show all
Includes:
Utils::Logging
Defined in:
lib/strategy/base.rb

Direct Known Subclasses

Blacklist, MongoDB::Whitelist, Whitelist

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Logging

#logger, #logger=

Constructor Details

#initialize(source_database, destination_database, name, user_strategies) ⇒ Base

Returns a new instance of Base.


8
9
10
11
12
13
14
15
16
17
# File 'lib/strategy/base.rb', line 8

def initialize source_database, destination_database, name, user_strategies
  @name = name
  @user_strategies = user_strategies
  @fields = {}
  @source_database = source_database
  @destination_database = destination_database
  @fields_missing_strategy = DataAnon::Core::FieldsMissingStrategy.new name
  @errors = DataAnon::Core::TableErrors.new(@name)
  @primary_keys = []
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors


6
7
8
# File 'lib/strategy/base.rb', line 6

def errors
  @errors
end

#fieldsObject

Returns the value of attribute fields


6
7
8
# File 'lib/strategy/base.rb', line 6

def fields
  @fields
end

#fields_missing_strategyObject

Returns the value of attribute fields_missing_strategy


6
7
8
# File 'lib/strategy/base.rb', line 6

def fields_missing_strategy
  @fields_missing_strategy
end

#user_strategiesObject

Returns the value of attribute user_strategies


6
7
8
# File 'lib/strategy/base.rb', line 6

def user_strategies
  @user_strategies
end

Class Method Details

.whitelist?Boolean

Returns:

  • (Boolean)

19
20
21
# File 'lib/strategy/base.rb', line 19

def self.whitelist?
  false
end

Instance Method Details

#anonymize(*fields, &block) ⇒ Object


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/strategy/base.rb', line 40

def anonymize *fields, &block
  if block.nil?
    fields.each { |f| @fields[f.downcase] = DataAnon::Strategy::Field::DefaultAnon.new(@user_strategies) }
    temp = self
    return Class.new do
      @temp_fields = fields
      @table_fields = temp.fields
      def self.using field_strategy
        @temp_fields.each { |f| @table_fields[f.downcase] = field_strategy }
      end
    end
  else
    fields.each { |f| @fields[f.downcase] = DataAnon::Strategy::Field::Anonymous.new(&block) }
  end
end

#batch_size(size) ⇒ Object


32
33
34
# File 'lib/strategy/base.rb', line 32

def batch_size size
  @batch_size = size
end

#default_strategy(field_name) ⇒ Object


60
61
62
63
# File 'lib/strategy/base.rb', line 60

def default_strategy field_name
  @fields_missing_strategy.missing field_name
  DataAnon::Strategy::Field::DefaultAnon.new(@user_strategies)
end

#dest_tableObject


65
66
67
68
69
# File 'lib/strategy/base.rb', line 65

def dest_table
  return @dest_table unless @dest_table.nil?
  DataAnon::Utils::DestinationDatabase.establish_connection @destination_database if @destination_database
  @dest_table = Utils::DestinationTable.create @name, @primary_keys
end

#is_primary_key?(field) ⇒ Boolean

Returns:

  • (Boolean)

56
57
58
# File 'lib/strategy/base.rb', line 56

def is_primary_key? field
  @primary_keys.select { |key| field.downcase == key.downcase }.length > 0
end

#primary_key(*fields) ⇒ Object


28
29
30
# File 'lib/strategy/base.rb', line 28

def primary_key *fields
  @primary_keys = fields
end

#processObject


77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/strategy/base.rb', line 77

def process
  logger.debug "Processing table #{@name} with fields strategies #{@fields}"
  total = source_table.count
  if total > 0
    progress = progress_bar.new(@name, total)
    if @primary_keys.empty? || !@batch_size.present?
      process_table progress
    else
      process_table_in_batches progress
    end
    progress.close
  end
end

#process_fields(&block) ⇒ Object


23
24
25
26
# File 'lib/strategy/base.rb', line 23

def process_fields &block
  self.instance_eval &block
  self
end

#process_table(progress) ⇒ Object


91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/strategy/base.rb', line 91

def process_table progress
  index = 0
  source_table.all.each do |record|
    index += 1
    begin
      process_record index, record
    rescue => exception
      @errors.log_error record, exception
    end
    progress.show index
  end
end

#process_table_in_batches(progress) ⇒ Object


104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/strategy/base.rb', line 104

def process_table_in_batches progress
  logger.info "Processing table #{@name} records in batch size of #{@batch_size}"
  index = 0
  source_table.find_each(:batch_size => @batch_size) do |record|
    index += 1
    begin
      process_record index, record
    rescue => exception
      @errors.log_error record, exception
    end
    progress.show index
  end
end

#progress_barObject


118
119
120
# File 'lib/strategy/base.rb', line 118

def progress_bar
  @progress_bar || DataAnon::Utils::ProgressBar
end

#progress_bar_class(progress_bar) ⇒ Object


122
123
124
# File 'lib/strategy/base.rb', line 122

def progress_bar_class progress_bar
  @progress_bar = progress_bar
end

#source_tableObject


71
72
73
74
75
# File 'lib/strategy/base.rb', line 71

def source_table
  return @source_table unless @source_table.nil?
  DataAnon::Utils::SourceDatabase.establish_connection @source_database
  @source_table = Utils::SourceTable.create @name, @primary_keys
end

#whitelist(*fields) ⇒ Object


36
37
38
# File 'lib/strategy/base.rb', line 36

def whitelist *fields
  fields.each { |f| @fields[f.downcase] = DataAnon::Strategy::Field::Whitelist.new }
end