now generates a pdf for the whole given month

main
Ervan Silvert 4 years ago
parent 9c14c0e739
commit d356da89aa
  1. 5
      lib/agenda_pdf.rb
  2. 6
      lib/agenda_pdf/options_parser.rb
  3. 21
      lib/agenda_pdf/pdf_generator.rb

@ -10,9 +10,6 @@ module AgendaPdf
def self.execute(argv) def self.execute(argv)
options = OptionsParser.new options = OptionsParser.new
PdfGenerator.new(options.path) do PdfGenerator.generate_month(options.month, path: options.path)
text 'hello world'
text argv.to_s
end
end end
end end

@ -17,7 +17,7 @@ class OptionsParser
end end
def month def month
@options[:month] || DateTime.now.month @options[:month] || DateTime.now
end end
def verbose def verbose
@ -30,8 +30,8 @@ class OptionsParser
OptionParser.new do |opts| OptionParser.new do |opts|
opts.banner = "Usage: agenda_pdf [options]" opts.banner = "Usage: agenda_pdf [options]"
opts.on("-m", "--month", "Generates a pdf for current month") do |v| opts.on("-mMONTH", "--month=MONTH", "Generates a pdf for given month (ex: 2021-02-01)") do |v|
@options[:month] = DateTime.parse(v) rescue DateTime.now.month @options[:month] = DateTime.parse(v) rescue DateTime.now
end end
opts.on("-o", "--path", "The output file path") do |v| opts.on("-o", "--path", "The output file path") do |v|

@ -5,11 +5,30 @@ class PdfGenerator < Prawn::Document
super() super()
initialize_document initialize_document
instance_eval &block instance_eval(&block)
finalize_document(path) finalize_document(path)
end end
class << self
def generate_month(datetime, path:)
month = datetime.month
year = datetime.year
start_date = DateTime.new(year, month)
end_date = start_date.next_month
new(path) do
(start_date..end_date).each do |day|
text day.year.to_s
text day.strftime('%b')
text day.strftime('%a %d')
start_new_page unless day == end_date
end
end
end
end
protected protected
def initialize_document; end def initialize_document; end

Loading…
Cancel
Save