Class: FlexData::Spreadsheet

Inherits:
Object
  • Object
show all
Defined in:
lib/flex_data/spreadsheet.rb,
lib/flex_data/spreadsheet/open_struct.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode, args) ⇒ Spreadsheet

Returns a new instance of Spreadsheet.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/flex_data/spreadsheet.rb', line 29

def initialize (mode, args)
  @path = args[:path]

  if mode == :open
    sheet = args[:sheet]
    ext = @path.split('.').last

    case ext
      when 'xlsx'
        open_xlsx(@path, sheet)
      when 'csv'
        open_csv(@path)
      else
        raise ArgumentError 'file extension not recognized'
    end

  elsif mode == :load

    rows = args[:rows]
    load_rows(rows)

  end

  @blank = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



55
56
57
# File 'lib/flex_data/spreadsheet.rb', line 55

def method_missing (method, *args, &block)
  @sheet.send(method, *args, &block)
end

Instance Attribute Details

#blankObject

Returns the value of attribute blank.



27
28
29
# File 'lib/flex_data/spreadsheet.rb', line 27

def blank
  @blank
end

#pathObject

Returns the value of attribute path.



27
28
29
# File 'lib/flex_data/spreadsheet.rb', line 27

def path
  @path
end

Class Method Details

.load_rows(rows) ⇒ Object



15
16
17
# File 'lib/flex_data/spreadsheet.rb', line 15

def load_rows (rows)
  Spreadsheet.new(:load, rows: rows)
end

.open(path, sheet = 0) ⇒ Object



11
12
13
# File 'lib/flex_data/spreadsheet.rb', line 11

def open (path, sheet = 0)
  Spreadsheet.new(:open, path: path, sheet: sheet)
end

.write(rows, path) ⇒ Object



19
20
21
22
23
# File 'lib/flex_data/spreadsheet.rb', line 19

def write (rows, path)
  new_source = self.load_rows(rows)
  new_source.write(path)
  new_source
end

Instance Method Details

#aggregate(col1, col2, operator) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/flex_data/spreadsheet.rb', line 78

def aggregate (col1, col2, operator)
  @sheet.data.inject([]) { |acc, row|
    if row[col1].nil? || row[col2].nil?
      acc
    else
      acc << eval("row[col1] #{operator.to_s} row[col2]")
    end
  }
end

#column(num) ⇒ Object



72
73
74
75
76
# File 'lib/flex_data/spreadsheet.rb', line 72

def column (num)
  @sheet.data.inject([]) { |acc, row|
    acc << row[num]
  }
end

#compact!(verbose = true) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/flex_data/spreadsheet.rb', line 102

def compact! (verbose = true)
  init_count = @sheet.size
  @sheet.reject! { |row|
    row.compact.empty?
  }
  puts "#{init_count - @sheet.size} empty rows rejected" if verbose
  self
end

#load_rows(rows) ⇒ Object



68
69
70
# File 'lib/flex_data/spreadsheet.rb', line 68

def load_rows (rows)
  @sheet = rows
end

#map_column(col1, &block) ⇒ Object



88
89
90
91
# File 'lib/flex_data/spreadsheet.rb', line 88

def map_column (col1, &block)
  original = column(col)
  original.collect { |cell| block.call(cell) }
end

#map_column!(col1, col2, &block) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/flex_data/spreadsheet.rb', line 93

def map_column! (col1, col2, &block)
  new = map_column(col1, &block)
  new.each_with_index { |cell, index|
    @blank[index] = [] if @blank[index].nil?
    @blank[index][col2] = cell
  }
  @blank
end

#map_to_h(first = 1, last = -1)) ⇒ Object



7
8
9
10
11
# File 'lib/flex_data/spreadsheet/open_struct.rb', line 7

def map_to_h (first = 1, last = -1)
  (first..last).inject([]) { |acc, index|
    acc << row_to_h(index)
  }
end

#map_to_ostruct(first = 1, last = -1)) ⇒ Object



13
14
15
16
17
# File 'lib/flex_data/spreadsheet/open_struct.rb', line 13

def map_to_ostruct (first = 1, last = -1)
  (first..last).inject([]) { |acc, index|
    acc << row_to_ostruct(index)
  }
end

#open_csv(path = @path) ⇒ Object



64
65
66
# File 'lib/flex_data/spreadsheet.rb', line 64

def open_csv (path = @path)
  @sheet = CSV.read(path)
end

#open_xlsx(path = @path, sheet = 0) ⇒ Object



59
60
61
62
# File 'lib/flex_data/spreadsheet.rb', line 59

def open_xlsx (path = @path, sheet = 0)
  doc = SimpleXlsxReader.open(path)
  @sheet = doc.sheets[sheet].rows
end

#row_to_h(index) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/flex_data/spreadsheet/open_struct.rb', line 19

def row_to_h (index)
  keys = @sheet[0]
  output = {}
  @sheet[index].each_with_index { |value, i|
    output.store(keys[i], value)
  }
  output
end

#row_to_ostruct(index) ⇒ Object



28
29
30
# File 'lib/flex_data/spreadsheet/open_struct.rb', line 28

def row_to_ostruct (index)
  OpenStruct.new(row_to_h(index))
end

#to_sObject



111
112
113
114
115
# File 'lib/flex_data/spreadsheet.rb', line 111

def to_s
  @sheet.inject('') { |acc, row|
    acc << "#{row.inspect}\n"
  }
end

#write(path = @path, mode = :sheet) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/flex_data/spreadsheet.rb', line 117

def write (path = @path, mode = :sheet)
  CSV.open(path, 'w') do |csv|
    case mode
      when :sheet
        @sheet.each { |row| csv << row }
      when :new
        @blank.each { |row| csv << row }
    end
  end
end