class Ledger::Entry
Declaration for entry object
Public Class Methods
new(params = {})
click to toggle source
# File lib/libledger/entry.rb, line 18 def initialize(params = {}) @data = params end
Private Class Methods
from_lines(lines)
click to toggle source
# File lib/libledger/entry.rb, line 87 def from_lines(lines) params = parse_first_line(lines.shift) params[:tags] = {} params[:actions] = lines.map do |x| m = x.match(ENTRY_TAG_LINE_REGEX) next(parse_action_line(x)) unless m params[:tags][m[1]] = m[2] nil end.compact Entry.new(params) end
parse_action_line(line)
click to toggle source
# File lib/libledger/entry.rb, line 114 def parse_action_line(line) name, amount = line.match(ENTRY_ACTION_LINE_REGEX).captures { name: name, amount: amount } end
parse_first_line(line)
click to toggle source
# File lib/libledger/entry.rb, line 105 def parse_first_line(line) date, state, name = line.match(ENTRY_SUBJECT_LINE_REGEX).captures { date: date, state: state, name: name } end
parse_tag_line(line)
click to toggle source
# File lib/libledger/entry.rb, line 101 def parse_tag_line(line) line.match(ENTRY_TAG_LINE_REGEX).captures end
Public Instance Methods
<=>(other)
click to toggle source
# File lib/libledger/entry.rb, line 56 def <=>(other) return nil unless other.is_a? Ledger::Entry date <=> other.date end
actions()
click to toggle source
# File lib/libledger/entry.rb, line 44 def actions @actions ||= @data[:actions] end
date()
click to toggle source
# File lib/libledger/entry.rb, line 38 def date return @date if @date @date ||= @data[:date] if @data[:date].is_a? Date @date ||= Date.parse(@data[:date]) end
name()
click to toggle source
# File lib/libledger/entry.rb, line 22 def name @name ||= @data[:name] end
state()
click to toggle source
# File lib/libledger/entry.rb, line 26 def state @state ||= @data[:state] end
state_str()
click to toggle source
# File lib/libledger/entry.rb, line 34 def state_str @state_str ||= state.is_a?(Symbol) ? STATE_MAPPING[state] : state end
state_sym()
click to toggle source
# File lib/libledger/entry.rb, line 30 def state_sym @state_sym ||= state.is_a?(Symbol) ? state : STATE_MAPPING.invert[state] end
to_s()
click to toggle source
# File lib/libledger/entry.rb, line 52 def to_s subject_line + tag_lines + action_lines.join("\n") + "\n" end
Private Instance Methods
action_lines()
click to toggle source
# File lib/libledger/entry.rb, line 73 def action_lines actions.map do |x| line = " #{x[:name]}" line += ' ' * action_padding(x) + x[:amount] if x[:amount] line end end
action_padding(action, width = 62)
click to toggle source
# File lib/libledger/entry.rb, line 81 def action_padding(action, width = 62) # Minus 4 for intent at front of line width - 4 - action[:name].size - action[:amount].size end
subject_line()
click to toggle source
# File lib/libledger/entry.rb, line 63 def subject_line "#{date.strftime('%Y/%m/%d')} #{state_str} #{name}\n" end
tag_lines()
click to toggle source
# File lib/libledger/entry.rb, line 67 def tag_lines res = tags.map { |k, v| " ; #{k}: #{v}" }.join("\n") res += "\n" unless res.empty? res end