Class: Sp2db::BaseTable

Inherits:
Object
  • Object
show all
Defined in:
lib/sp2db/base_table.rb

Direct Known Subclasses

ModelTable, NonModelTable

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ BaseTable

Returns a new instance of BaseTable.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sp2db/base_table.rb', line 11

def initialize opts={}

  if opts[:name].blank? && opts[:sheet_name].blank?
    raise "Must specify at least one of name or sheet name"
  end

  opts.each do |k, v|
    self.send "#{k}=", v
  end

  self.sheet_name ||= opts[:sheet_name] = config[:sheet_name] || worksheet.try(:title)
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



4
5
6
# File 'lib/sp2db/base_table.rb', line 4

def client
  @client
end

#find_columnsObject

Returns the value of attribute find_columns.



4
5
6
# File 'lib/sp2db/base_table.rb', line 4

def find_columns
  @find_columns
end

#nameObject

Table name



29
30
31
# File 'lib/sp2db/base_table.rb', line 29

def name
  @name
end

#sheet_nameObject

Returns the value of attribute sheet_name.



4
5
6
# File 'lib/sp2db/base_table.rb', line 4

def sheet_name
  @sheet_name
end

#spreadsheet_idObject

Returns the value of attribute spreadsheet_id.



4
5
6
# File 'lib/sp2db/base_table.rb', line 4

def spreadsheet_id
  @spreadsheet_id
end

#worksheetObject

Returns the value of attribute worksheet.



4
5
6
# File 'lib/sp2db/base_table.rb', line 4

def worksheet
  @worksheet
end

Class Method Details

.all_tablesObject



219
220
221
# File 'lib/sp2db/base_table.rb', line 219

def all_tables
  ModelTable.all_tables + NonModelTable.all_tables
end

.model_table_classObject



239
240
241
# File 'lib/sp2db/base_table.rb', line 239

def model_table_class
  ModelTable
end

.sp_to_csv(*table_names) ⇒ Object



235
236
237
# File 'lib/sp2db/base_table.rb', line 235

def sp_to_csv *table_names
  table_by_names(*table_names).map(&__method__)
end

.table_by_names(*names) ⇒ Object



224
225
226
227
228
229
230
231
232
233
# File 'lib/sp2db/base_table.rb', line 224

def table_by_names *names
  all_tables = self.all_tables
  if names.blank?
    all_tables
  else
    names.map do |n|
      all_tables.find {|tb| tb.name == n.to_sym} || raise("Not found: #{n}")
    end
  end
end

Instance Method Details

#active_record?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/sp2db/base_table.rb', line 24

def active_record?
  false
end

#configObject

Global config



128
129
130
# File 'lib/sp2db/base_table.rb', line 128

def config
  {}.with_indifferent_access
end

#csv_dataObject



80
81
82
83
84
# File 'lib/sp2db/base_table.rb', line 80

def csv_data
  raw_data = CSV.parse File.open(csv_file)
  data = process_data raw_data, source: :csv
  data
end

#csv_fileObject



97
98
99
# File 'lib/sp2db/base_table.rb', line 97

def csv_file
  "#{csv_folder}/#{name}.csv"
end

#csv_folderObject



91
92
93
94
95
# File 'lib/sp2db/base_table.rb', line 91

def csv_folder
  folder = "#{Sp2db.config.export_location}/csv"
  FileUtils.mkdir_p folder
  folder
end

#data_transform(raw_data, opts = {}) ⇒ Object

Tranform data to standard csv format



141
142
143
144
145
146
147
# File 'lib/sp2db/base_table.rb', line 141

def data_transform raw_data, opts={}
  if config[:data_transform].present?
    config[:data_transform].call *args, &block
  else
    raw_data
  end
end

#header_rowObject



86
87
88
89
# File 'lib/sp2db/base_table.rb', line 86

def header_row
  # @header_row ||= config[:header_row] || 0
  0
end

#process_data(raw_data, opts = {}) ⇒ Object



132
133
134
135
136
137
# File 'lib/sp2db/base_table.rb', line 132

def process_data raw_data, opts={}
  raw_data = data_transform raw_data, opts unless opts[:skip_data_transform]
  raw_data = raw_filter raw_data, opts unless opts[:skip_data_filter]
  data = call_process_data raw_data, opts
  data
end

#required_columnsObject



45
46
47
# File 'lib/sp2db/base_table.rb', line 45

def required_columns
  @required_columns ||= config[:required_columns] || []
end

#sp_dataObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/sp2db/base_table.rb', line 65

def sp_data
  retries = 2
  begin
    raw_data = CSV.parse worksheet.export_as_string
  rescue Google::Apis::RateLimitError => e
    retries -= 1
    sleep(5)
    retry if retries >= 0
    raise e
  end

  data = process_data raw_data, source: :sp
  data
end

#sp_to_csv(opts = {}) ⇒ Object



101
102
103
# File 'lib/sp2db/base_table.rb', line 101

def sp_to_csv opts={}
  write_csv to_csv(sp_data)
end

#spreadsheetObject



53
54
55
# File 'lib/sp2db/base_table.rb', line 53

def spreadsheet
  client.spreadsheet spreadsheet_id
end

#to_csv(data) ⇒ Object

Array of hash data to csv format



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/sp2db/base_table.rb', line 113

def to_csv data
  attributes = data.first&.keys || []

  CSV.generate(headers: true) do |csv|
    csv << attributes

    data.each do |row|
      csv << attributes.map do |att|
        row[att]
      end
    end
  end
end

#write_csv(data) ⇒ Object



105
106
107
108
109
110
# File 'lib/sp2db/base_table.rb', line 105

def write_csv data
  File.open csv_file, "wb" do |f|
    f.write data
  end
  csv_file
end