Skip to content

Commit

Permalink
Merge branch 'yamlformat_generators' into 'master'
Browse files Browse the repository at this point in the history
Yamlformat generators

See merge request synyx/buchungsstreber!30
  • Loading branch information
BuJo committed Sep 1, 2020
2 parents 3ff1d67 + 509ce9d commit e6217a0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
20 changes: 19 additions & 1 deletion lib/buchungsstreber/parser/yaml_timesheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ def self.parses?(file)
end

def parse(file_path)
timesheet = YAML.load_file(file_path)
timesheet =
if File.size(file_path) == 0
{}
else
YAML.load_file(file_path)
end
throw 'invalid line: file should contain map' unless timesheet.is_a?(Hash)
result = []

Expand Down Expand Up @@ -44,6 +49,19 @@ def archive(file_path, archive_path, date)
File.write(file_path, "#{next_monday}:\n\n\n---\n# Letzte Woche\n" + old_timesheet)
end

def format(entries)
buf = ""
days = entries.group_by {|e| e[:date] }.to_a.sort_by { |x| x[0] }
days.each do |date, day|
buf << "#{date}:\n"
day.each do |e|
buf << " # #{e[:comment]}\n" if e[:comment]
buf << " - #{qarter_time(e[:time] || 0.0)}\t#{e[:activity]}\t#{e[:redmine]}#{e[:issue]}\t#{e[:text]}\n"
end
end
buf
end

private

def parse_entry(entry, date)
Expand Down
27 changes: 27 additions & 0 deletions spec/timesheet_examples.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'tempfile'

require 'buchungsstreber/parser'

RSpec.shared_examples 'a timesheet parser' do |extension, templates|
Expand Down Expand Up @@ -39,4 +41,29 @@
expect { described_class.new(templates || {}).parse("spec/examples/invalid_time#{extension}") }.to raise_exception(/invalid time/)
end
end

context 'generating' do
it 'can render time entries' do
e = [{ issue: '1234', activity: 'Dev', text: 'asdf', time: 0.5, date: Date.parse('1970-01-01') }]
expect(described_class.new(templates || {}).format(e)).to include('asdf')
end

it 'produces something the same parser can parse' do
parser = described_class.new(templates || {})
e1 = [{ issue: '1234', activity: 'Dev', text: 'asdf', time: 0.5, date: Date.parse('1970-01-01') }]
str = parser.format(e1)

file = Tempfile.new('foo')
begin
file.write(str)
file.close
e2 = parser.parse(file.path)
e1[0].each do |k,v|
expect(e2[0][k]).to eq(v)
end
ensure
file.unlink
end
end
end
end

0 comments on commit e6217a0

Please sign in to comment.