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.



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

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

Class Method Details

.from_lines(lines) ⇒ Object



76
77
78
79
80
# File 'lib/libledger/entry.rb', line 76

def from_lines(lines)
  params = parse_first_line(lines.shift)
  params[:actions] = lines.map { |x| parse_action_line x }
  Entry.new(params)
end

Instance Method Details

#<=>(other) ⇒ Object



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

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

#actionsObject



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

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

#dateObject



37
38
39
40
41
# File 'lib/libledger/entry.rb', line 37

def date
  return @date if @date
  @date ||= @data[:date] if @data[:date].is_a? Date
  @date ||= Date.parse(@data[:date])
end

#nameObject



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

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

#stateObject



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

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

#state_strObject



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

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

#state_symObject



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

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

#to_sObject



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

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