Module: RVGP::Base::Grid::HasMultipleSheets

Defined in:
lib/rvgp/base/grid.rb

Overview

This module can be included into classes descending from RVGP::Base::Grid, in order to add support for multiple sheets, per year. These sheets can be declared using the provided ‘has_sheets’ class method, like so: “‘ has_sheets(’cashflow’) { %w(personal business) } “‘ This declaration will ensure the creation of “#{year}-cashflow-business.csv” and “#{year}-cashflow-personal.csv” grids, in the project’s build/grids output. This is achieved by providing the sheet name as a parameter to your #sheet_header, and #sheet_body methods. (see the below example)

## Example Here’s a simple example of a grid that’s segmented both by year, as well as by “property”. The property an expense correlates with, is determined by the value of it’s property tag (should one exist). This grid will build a separate grid for every property that we’ve tagged expenses for, with the expenses for that tag, separated by year. “‘ class PropertyExpensesGrid < RVGP::Base::Grid

include HasMultipleSheets

grid 'expenses_by_property', 'Generate Property Expense Grids', 'Property Expenses by month (%s)'

has_sheets('property-expenses') { |year| pta.tags 'property', values: true, begin: year, end: year + 1 }

def sheet_header(property)
  ['Date'] + sheet_series(property)
end

def sheet_body(property)
  months = property_expenses(property).values.map(&:keys).flatten.uniq.sort

  months_through_dates(months.first, months.last).map do |month|
    [month.strftime('%m-%y')] + sheet_series(property).map { |col| property_expenses(property)[col][month] }
  end
end

private

def sheet_series(property)
  property_expenses(property).keys.sort
end

def property_expenses(property)
  @property_expenses ||= {}
  @property_expenses[property] ||= (
    ledger_args: [format('%%property=%s', property), 'and', 'Expense'],
    hledger_args: [format('tag:property=%s', property), 'Expense']
  )
end

end “‘

This PropertyExpensesGrid, depending on your data, will output a series of grids in your build directory, such as the following:

  •  build/grids/2018-property-expenses-181_yurakucho.csv

  •  build/grids/2018-property-expenses-101_0021tokyo.csv

  •  build/grids/2019-property-expenses-181_yurakucho.csv

  •  build/grids/2019-property-expenses-101_0021tokyo.csv

  •  build/grids/2020-property-expenses-181_yurakucho.csv

  •  build/grids/2020-property-expenses-101_0021tokyo.csv

  •  build/grids/2021-property-expenses-181_yurakucho.csv

  •  build/grids/2021-property-expenses-101_0021tokyo.csv

  •  build/grids/2022-property-expenses-181_yurakucho.csv

  •  build/grids/2022-property-expenses-101_0021tokyo.csv

  •  build/grids/2023-property-expenses-181_yurakucho.csv

  •  build/grids/2023-property-expenses-101_0021tokyo.csv

And, inside each of this files, will be a csv similar to: “‘ Date,Business:Expenses:Banking:Interest:181Yurakucho,Business:Expenses:Home:Improvement:181Yurakucho 01-23,123.45,678.90,123.45,678.90,123.45,678.90,123.45,678.90,123.45 02-23,123.45,678.90,123.45,678.90,123.45,678.90,123.45,678.90,123.45 03-23,123.45,678.90,123.45,678.90,123.45,678.90,123.45,678.90,123.45 04-23,123.45,678.90,123.45,678.90,123.45,678.90,123.45,678.90,123.45 05-23,123.45,678.90,123.45,678.90,123.45,678.90,123.45,678.90,123.45 06-23,123.45,678.90,123.45,678.90,123.45,678.90,123.45,678.90,123.45 07-23,123.45,678.90,123.45,678.90,123.45,678.90,123.45,678.90,123.45 07-23,123.45,678.90,123.45,678.90,123.45,678.90,123.45,678.90,123.45 08-23,123.45,678.90,123.45,678.90,123.45,678.90,123.45,678.90,123.45 09-23,123.45,678.90,123.45,678.90,123.45,678.90,123.45,678.90,123.45 10-23,123.45,678.90,123.45,678.90,123.45,678.90,123.45,678.90,123.45 11-23,123.45,678.90,123.45,678.90,123.45,678.90,123.45,678.90,123.45 12-23,123.45,678.90,123.45,678.90,123.45,678.90,123.45,678.90,123.45 “`

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#sheets(year) ⇒ Object

see (RVGP::Base::Grid::HasMultipleSheets.sheets)



471
472
473
# File 'lib/rvgp/base/grid.rb', line 471

def sheets(year)
  self.class.sheets year
end

#to_file!void

This method returns an undefined value.

Write the computed grid, to its default build path



463
464
465
466
467
468
# File 'lib/rvgp/base/grid.rb', line 463

def to_file!
  self.class.sheets(year).each do |sheet|
    write! self.class.output_path(year, sheet.to_s.downcase), to_table(sheet)
  end
  nil
end

#to_table(sheet) ⇒ Array[Array<String>]

Return the computed grid, in a parsed form, before it’s serialized to a string.

Returns:

  • (Array[Array<String>])

    Each row is an array, itself composed of an array of cells.



457
458
459
# File 'lib/rvgp/base/grid.rb', line 457

def to_table(sheet)
  [sheet_header(sheet)] + sheet_body(sheet)
end