Class: Cartos::Spreadsheet::CartosSpreadsheet

Inherits:
Object
  • Object
show all
Defined in:
lib/cartos/spreadsheet/cartos_spreadsheet.rb

Instance Method Summary collapse

Constructor Details

#initialize(spreeadsheet_key) ⇒ CartosSpreadsheet

Returns a new instance of CartosSpreadsheet.



4
5
6
7
# File 'lib/cartos/spreadsheet/cartos_spreadsheet.rb', line 4

def initialize(spreeadsheet_key)
  @spreadsheet =  Cartos::Google::Spreadsheet.new spreeadsheet_key
  logger.info "Exporting data to #{@spreadsheet.url}"
end

Instance Method Details

#monthly(entries) ⇒ Object



10
11
12
13
14
# File 'lib/cartos/spreadsheet/cartos_spreadsheet.rb', line 10

def monthly(entries)
  entries.each_month do |month, month_entries|
    new_month month, month_entries
  end
end

#summary(entries) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cartos/spreadsheet/cartos_spreadsheet.rb', line 16

def summary(entries)
  summary_sheet = Cartos::Spreadsheet::Summary.new @spreadsheet.new_sheet "summary"
  categories = {}
  totals = {}
  totals["expendings"] = {}
  totals["earnings"] = {}
  totals["total"] = {}
  entries.each_month do |month, month_entries|
    totals["expendings"][month] = 0
    totals["earnings"][month] = 0
    month_entries.each do |entry|
      entry.amount < 0 ? totals["expendings"][month] += entry.amount : totals["earnings"][month] += entry.amount
    end
    totals["total"][month] = totals["earnings"][month] + totals["expendings"][month]
    totals["expendings"][month] = totals["expendings"][month]
    month_entries.each_category do |category, elements|
      categories[category] ||= {}
      categories[category][month] = elements.inject(0) {|total, element| total += element.amount}
    end
  end
  summary_sheet.push_categories categories
  summary_sheet.push_totals totals
  summary_sheet.save
end