class TheFox::Wallet::Entry

Attributes

balance[R]
category[R]
comment[R]
date[R]
expense[R]
id[R]
revenue[R]
title[R]

Public Class Methods

from_h(h) click to toggle source

Restore a Entry from a Hash.

# 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
new(id = nil, title = nil, date = nil, revenue = nil, expense = nil, category = nil, comment = nil) click to toggle source
# 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

Public Instance Methods

category=(category) click to toggle source
# File lib/wallet/entry.rb, line 98
def category=(category)
  @category = category.nil? ? 'default' : category.to_s
end
comment=(comment) click to toggle source
# File lib/wallet/entry.rb, line 102
def comment=(comment)
  @comment = comment.nil? ? '' : comment.to_s
end
date=(date) click to toggle source
# File lib/wallet/entry.rb, line 60
def date=(date)
  case date
  when String
    # String
    @date = Date.parse(date)
  when Integer
    # Integer
    @date = Time.at(date).to_date
  when Date
    # Date
    @date = date
  else
    raise ArgumentError, "Wrong class: #{date.class}"
  end
end
expense=(expense) click to toggle source
# File lib/wallet/entry.rb, line 87
def expense=(expense)
  expense_t = expense.to_f
  
  if expense_t > 0
    raise RangeError, "expense (#{expense_t}) cannot be > 0. use revenue instead!"
  end
  
  @expense = expense_t
  calc_balance
end
id=(id) click to toggle source
# File lib/wallet/entry.rb, line 52
def id=(id)
  @id = id
end
revenue=(revenue) click to toggle source
# File lib/wallet/entry.rb, line 76
def revenue=(revenue)
  revenue_t = revenue.to_f
  
  if revenue_t < 0
    raise RangeError, "revenue (#{revenue_t}) cannot be < 0. use expense instead!"
  end
  
  @revenue = revenue_t
  calc_balance
end
title=(title) click to toggle source
# File lib/wallet/entry.rb, line 56
def title=(title)
  @title = title.to_s
end
to_h() click to toggle source

Convert Entry to a Hash.

# 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

Private Instance Methods

calc_balance() click to toggle source
# File lib/wallet/entry.rb, line 136
def calc_balance
  @balance = (@revenue.round(NUMBER_ROUND) + @expense.round(NUMBER_ROUND)).to_f.round(NUMBER_ROUND)
end