Class: Ledger::Entry

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/libledger/entry.rb

Overview

Declaration for entry object

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Entry

Returns a new instance of Entry.



18
19
20
# File 'lib/libledger/entry.rb', line 18

def initialize(params = {})
  @data = params
end

Class Method Details

.from_lines(lines) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# 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

Instance Method Details

#<=>(other) ⇒ Object



56
57
58
59
# File 'lib/libledger/entry.rb', line 56

def <=>(other)
  return nil unless other.is_a? Ledger::Entry
  date <=> other.date
end

#actionsObject



44
45
46
# File 'lib/libledger/entry.rb', line 44

def actions
  @actions ||= @data[:actions]
end

#dateObject



38
39
40
41
42
# 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

#nameObject



22
23
24
# File 'lib/libledger/entry.rb', line 22

def name
  @name ||= @data[:name]
end

#stateObject



26
27
28
# File 'lib/libledger/entry.rb', line 26

def state
  @state ||= @data[:state]
end

#state_strObject



34
35
36
# File 'lib/libledger/entry.rb', line 34

def state_str
  @state_str ||= state.is_a?(Symbol) ? STATE_MAPPING[state] : state
end

#state_symObject



30
31
32
# File 'lib/libledger/entry.rb', line 30

def state_sym
  @state_sym ||= state.is_a?(Symbol) ? state : STATE_MAPPING.invert[state]
end

#tagsObject



48
49
50
# File 'lib/libledger/entry.rb', line 48

def tags
  @tags ||= @data[:tags] || {}
end

#to_sObject



52
53
54
# File 'lib/libledger/entry.rb', line 52

def to_s
  subject_line + tag_lines + action_lines.join("\n") + "\n"
end