Class: DataAnon::Strategy::Field::SelectFromDatabase

Inherits:
SelectFromFile show all
Includes:
Utils::Logging
Defined in:
lib/strategy/field/string/select_from_database.rb

Overview

Similar to SelectFromList with difference is the list of values are collected from the database table using distinct column query.

# values are collected using `select distinct state from customers` query connecting to specified database in connection_spec
anonymize('state').using FieldStrategy::SelectFromDatabase.new('customers','state', connection_spec)

Instance Method Summary collapse

Methods included from Utils::Logging

#logger, #logger=

Constructor Details

#initialize(table_name, field_name, connection_spec) ⇒ SelectFromDatabase

Returns a new instance of SelectFromDatabase.


14
15
16
17
18
# File 'lib/strategy/field/string/select_from_database.rb', line 14

def initialize table_name, field_name, connection_spec
  @table_name = table_name
  @field_name = field_name
  @connection_spec = connection_spec
end

Instance Method Details

#anonymize(field) ⇒ Object


20
21
22
23
24
25
26
27
28
29
30
# File 'lib/strategy/field/string/select_from_database.rb', line 20

def anonymize field
  @values ||= begin
    DataAnon::Utils::SourceDatabase.establish_connection @connection_spec
    source = Utils::SourceTable.create @table_name, []
    values = source.select(@field_name).distinct.collect { |record| record[@field_name]}
    logger.debug "For field strategy #{@table_name}:#{@field_name} using values #{values} "
    values
  end

  super
end