Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 61 lines (46 sloc) 1.37 KB
#!/usr/bin/env ruby
require 'date'
require 'yaml'
require 'ballista'
require 'cymbal'
CONFIG_FILE = 'projections.yml'.freeze
JOURNAL_DIR = 'journals'.freeze
def config
@config ||= Cymbal.symbolize(YAML.safe_load(File.read(CONFIG_FILE)))
end
def full_range
@full_range ||= [Date.today, Date.new(Date.today.year + 1, 12)]
end
def month_pairs
return @month_pairs if @month_pairs
month_starts = (full_range.first..full_range.last).select do |date|
date.day == 1 || date == full_range.first
end
@month_pairs = month_starts.map { |x| [x, Date.new(x.year, x.month, -1)] }
end
def month_file(start)
"#{JOURNAL_DIR}/#{start.strftime('%Y/%m')}.ldg"
end
def header
'; Records below generated by ./scripts/project using ballista'
end
def existing_content(file)
return '' unless File.exist? file
content = File.read(file).partition(header).first.rstrip
return '' if content.empty?
content + "\n\n"
end
def walk_months
month_pairs.each do |first, last|
results = Ballista.new(entries: config).project(first, last)
file = month_file(first)
new_content = "#{existing_content(file)}#{header}\n\n#{results}"
Dir.mkdir(File.dirname(file)) unless Dir.exist?(File.dirname(file))
File.open(file, 'w') { |fh| fh << new_content }
end
end
unless File.exist? CONFIG_FILE
puts 'Run this from directory with journals and projections.yml'
exit 1
end
walk_months