Module: Clinvoice::GenerateNextTemplate
- Defined in:
- lib/clinvoice/generate_next_template.rb
Constant Summary collapse
- DATE_FORMAT =
'%m/%d/%Y'- MONTH_YEAR_REGEX =
/\b(#{Date::ABBR_MONTHNAMES.compact.join('|')}) (\d{4})\b/.freeze
Class Method Summary collapse
- .call(file) ⇒ Object
- .find_last_data_file(file) ⇒ Object
- .increment_date(value) ⇒ Object
- .increment_month_year(text) ⇒ Object
- .open_in_editor(file) ⇒ Object
Class Method Details
.call(file) ⇒ Object
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 |
# File 'lib/clinvoice/generate_next_template.rb', line 11 def self.call(file) last_data_file = find_last_data_file(file) unless last_data_file puts "No existing data file found for '#{file}'." exit 1 end yaml = YAML.load_file(last_data_file) invoice_data = yaml['data'] invoice_data['id'] += 1 invoice_data['issue_date'] = increment_date(invoice_data['issue_date']) if invoice_data['issue_date'] invoice_data['due_date'] = increment_date(invoice_data['due_date']) if invoice_data['due_date'] invoice_data['items'].each do |item| item['description'] = increment_month_year(item['description']) if item['description'] end next_file = "#{file}-#{invoice_data['id']}.yml" File.write(next_file, YAML.dump(yaml)) open_in_editor(next_file) next_file end |
.find_last_data_file(file) ⇒ Object
38 39 40 |
# File 'lib/clinvoice/generate_next_template.rb', line 38 def self.find_last_data_file(file) Dir["#{file}-*.yml"].max_by { |f| f[/-(\d+)\.yml\z/, 1].to_i } end |
.increment_date(value) ⇒ Object
42 43 44 |
# File 'lib/clinvoice/generate_next_template.rb', line 42 def self.increment_date(value) (Date.strptime(value, DATE_FORMAT) >> 1).strftime(DATE_FORMAT) end |
.increment_month_year(text) ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/clinvoice/generate_next_template.rb', line 46 def self.increment_month_year(text) text.gsub(MONTH_YEAR_REGEX) do month_number = Date::ABBR_MONTHNAMES.index(Regexp.last_match(1)) year = Regexp.last_match(2).to_i next_month = Date.new(year, month_number, 1) >> 1 "#{Date::ABBR_MONTHNAMES[next_month.month]} #{next_month.year}" end end |
.open_in_editor(file) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/clinvoice/generate_next_template.rb', line 56 def self.open_in_editor(file) editor = ENV['EDITOR'] return if editor.nil? || editor.empty? system(editor, file) end |