Class: CSVundle::AccessCSV

Inherits:
Object
  • Object
show all
Defined in:
lib/csvundle/access_csv.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ AccessCSV

Returns a new instance of AccessCSV.



10
11
12
13
14
15
16
17
18
19
# File 'lib/csvundle/access_csv.rb', line 10

def initialize(type)
  @setup_data = YAML.load(ERB.new(File.read("#{GEM_ROOT}/config/csv_setup_data.yml")).result).fetch(type.to_s)
  @rows, @columns, @full_csv = [], [], []
  @headers = @setup_data['headers']
  @columns = @headers.keys
  @column_values = @headers.values
  @normalized_columns = normalized_columns
  @type = type if type_usable? type
  setup_by_type
end

Instance Attribute Details

#column_valuesObject (readonly)

Returns the value of attribute column_values.



8
9
10
# File 'lib/csvundle/access_csv.rb', line 8

def column_values
  @column_values
end

#columnsObject

Returns the value of attribute columns.



7
8
9
# File 'lib/csvundle/access_csv.rb', line 7

def columns
  @columns
end

#full_csvObject

Returns the value of attribute full_csv.



7
8
9
# File 'lib/csvundle/access_csv.rb', line 7

def full_csv
  @full_csv
end

#headersObject (readonly)

Returns the value of attribute headers.



8
9
10
# File 'lib/csvundle/access_csv.rb', line 8

def headers
  @headers
end

#rowsObject

Returns the value of attribute rows.



7
8
9
# File 'lib/csvundle/access_csv.rb', line 7

def rows
  @rows
end

#setup_dataObject

Returns the value of attribute setup_data.



7
8
9
# File 'lib/csvundle/access_csv.rb', line 7

def setup_data
  @setup_data
end

Instance Method Details

#normalized_columnsObject



49
50
51
# File 'lib/csvundle/access_csv.rb', line 49

def normalized_columns
  columns.map { |c| viperize(c) }
end

#rows_for(column) ⇒ Object



53
54
55
# File 'lib/csvundle/access_csv.rb', line 53

def rows_for(column)
  { "#{column}": @rows.map { |row| row[@columns.index(column)] } }
end

#serve(mapped_data) ⇒ Object



21
22
23
24
25
# File 'lib/csvundle/access_csv.rb', line 21

def serve(mapped_data)
  mapped_data.each { |row| @rows << row }
  mapped_data.each { |row| @full_csv << row }
  CSV.generate { |csv| @full_csv.each { |row| csv << row } }
end

#setup_by_typeObject



27
28
29
30
31
32
# File 'lib/csvundle/access_csv.rb', line 27

def setup_by_type
  full_csv << @setup_data['initial_data'] if @setup_data['initial_data'].any?
  @setup_data['filler_row_count'].times { full_csv << [] }
  @setup_data['filler_row_count'].times { rows << [] }
  full_csv << columns
end

#type_usable?(type) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/csvundle/access_csv.rb', line 34

def type_usable?(type)
  [:lienalytics, :grant_street, :lumentum, :mtag, :old_lienalytics,
   :real_auction, :tsr, :tsr_js, :core_logic, :blox_trade].include? type.to_sym
end

#viperize(symbol) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/csvundle/access_csv.rb', line 39

def viperize(symbol)
  word = symbol.to_s.dup
  word.gsub!(/::/, '/')
  word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
  word.tr!("-", "_")
  word.downcase!
  word.to_sym
end