Class: TheFox::Wallet::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/wallet/entry.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = nil, title = nil, date = nil, revenue = nil, expense = nil, category = nil, comment = nil) ⇒ Entry

Returns a new instance of Entry.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/wallet/entry.rb', line 19

def initialize(id = nil, title = nil, date = nil, revenue = nil, expense = nil, category = nil, comment = nil)
  if !id
    uuid = UUID.new
    id = uuid.generate
  end
  date ||= Date.today
  revenue ||= 0.0
  expense ||= 0.0
  category ||= 'default'
  
  self.id = id
  self.title = title
  self.date = date
  
  @revenue = 0.0
  @expense = 0.0
  @balance = 0.0
  
  revenue_t = revenue.to_f
  expense_t = expense.to_f
  if revenue_t < 0 && expense_t == 0
    # Revenue is minus and no expense was provided.
    self.revenue = 0.0
    self.expense = revenue_t
  else
    self.revenue = revenue_t
    self.expense = expense_t
  end
  
  self.category = category
  self.comment = comment
end

Instance Attribute Details

#balanceObject (readonly)

Returns the value of attribute balance.



15
16
17
# File 'lib/wallet/entry.rb', line 15

def balance
  @balance
end

#categoryObject

Returns the value of attribute category.



16
17
18
# File 'lib/wallet/entry.rb', line 16

def category
  @category
end

#commentObject

Returns the value of attribute comment.



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

def comment
  @comment
end

#dateObject

Returns the value of attribute date.



12
13
14
# File 'lib/wallet/entry.rb', line 12

def date
  @date
end

#expenseObject

Returns the value of attribute expense.



14
15
16
# File 'lib/wallet/entry.rb', line 14

def expense
  @expense
end

#idObject

Returns the value of attribute id.



10
11
12
# File 'lib/wallet/entry.rb', line 10

def id
  @id
end

#revenueObject

Returns the value of attribute revenue.



13
14
15
# File 'lib/wallet/entry.rb', line 13

def revenue
  @revenue
end

#titleObject

Returns the value of attribute title.



11
12
13
# File 'lib/wallet/entry.rb', line 11

def title
  @title
end

Class Method Details

.from_h(h) ⇒ Object

Restore a Entry from a Hash.



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/wallet/entry.rb', line 121

def self.from_h(h)
  id = h['id']
  title = h['title']
  date = h['date']
  revenue = h['revenue']
  expense = h['expense']
  # balance = h['balance']
  category = h['category']
  comment = h['comment']
  
  self.new(id, title, date, revenue, expense, category, comment)
end

Instance Method Details

#to_hObject

Convert Entry to a Hash.



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/wallet/entry.rb', line 107

def to_h
  {
    'id' => @id,
    'title' => @title,
    'date' => @date.to_s,
    'revenue' => @revenue,
    'expense' => @expense,
    'balance' => @balance,
    'category' => @category,
    'comment' => @comment,
  }
end