-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathldgproject
executable file
·55 lines (44 loc) · 1.31 KB
/
ldgproject
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
54
55
#!/usr/bin/env ruby
require 'date'
require 'yaml'
require 'cymbal'
require 'ballista'
CONFIG_FILE = 'projections.yml'.freeze
JOURNAL_DIR = 'journals'.freeze
def config
@config ||= Cymbal.symbolize(YAML.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
File.read(file).partition(header).first.rstrip
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)}\n\n#{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