Class: DataTaster::Flavors

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/data_taster/flavors.rb

Overview

helper methods made to make data_taster_export_tables.yml files more user-friendly

Instance Method Summary collapse

Methods included from Helper

#db_config, #logg, #sanitize_command

Instance Method Details

#current_dateObject



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

def current_date
  @current_date ||= Date.current
end

#dateObject



13
14
15
16
17
18
19
# File 'lib/data_taster/flavors.rb', line 13

def date
  @date ||= if DataTaster.config.months
              (current_date - DataTaster.config.months.to_i.months).beginning_of_day.to_s(:db)
            else
              (current_date - 1.week).beginning_of_day.to_s(:db)
            end
end

#default_value_for(column) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/data_taster/flavors.rb', line 50

def default_value_for(column)
  case column
  when /date_of_birth/, /dob/
    (Date.current - 25.years).strftime("%m/%d/%Y")
  when /ssn/, /license/
    "111111111"
  when /compensation/
    1
  else
    "1"
  end
end

#deprecated_tableObject

skips dumping both schema and data



22
23
24
# File 'lib/data_taster/flavors.rb', line 22

def deprecated_table
  DataTaster::SKIP_CODE
end

#encrypt(klass, column, value = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/data_taster/flavors.rb', line 30

def encrypt(klass, column, value = nil)
  value_to_encrypt = value || default_value_for(column)

  klass_instance = klass.new
  if klass_instance.respond_to?(:attr_encrypted_encrypt)
    klass_instance.attr_encrypted_encrypt(column, value_to_encrypt)
  elsif klass_instance.respond_to?(:encrypt)
    klass_instance.encrypt(column, value_to_encrypt)
  else
    raise encryption_error_message
  end
end

#encryption_error_messageObject



43
44
45
46
47
48
# File 'lib/data_taster/flavors.rb', line 43

def encryption_error_message
  [
    "DataTaster only supports encryption if your model is configured with attr_encrypted.",
    "Please visit https://github.com/attr-encrypted/attr_encrypted for more details on setup.",
  ].join(" ")
end

#full_table_dumpObject



63
64
65
# File 'lib/data_taster/flavors.rb', line 63

def full_table_dump
  "1 = 1"
end

#recent_ids(table_name, col_name) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/data_taster/flavors.rb', line 71

def recent_ids(table_name, col_name)
  <<~SQL.squish
    (SELECT DISTINCT(#{col_name})
    FROM #{source_db}.#{table_name}
    WHERE
    created_at >= '#{date}'
    OR
    updated_at >= '#{date}')
  SQL
end

#recent_table_updatesObject



67
68
69
# File 'lib/data_taster/flavors.rb', line 67

def recent_table_updates
  "created_at >= '#{date}' OR updated_at >= '#{date}'"
end

#skip_sanitizationObject



26
27
28
# File 'lib/data_taster/flavors.rb', line 26

def skip_sanitization
  DataTaster::SKIP_CODE
end

#source_dbObject



82
83
84
# File 'lib/data_taster/flavors.rb', line 82

def source_db
  @source_db ||= db_config[:database]
end