Class: Invoice
- Inherits:
-
Object
- Object
- Invoice
- Defined in:
- lib/invoices/models/invoice.rb
Instance Attribute Summary collapse
-
#client_id ⇒ Object
Returns the value of attribute client_id.
-
#date ⇒ Object
Returns the value of attribute date.
-
#format_number ⇒ Object
readonly
Returns the value of attribute format_number.
-
#git_log ⇒ Object
readonly
Returns the value of attribute git_log.
-
#hours ⇒ Object
readonly
Returns the value of attribute hours.
-
#line_items_array ⇒ Object
readonly
Returns the value of attribute line_items_array.
-
#number ⇒ Object
Returns the value of attribute number.
-
#rate ⇒ Object
readonly
Returns the value of attribute rate.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#total_cost ⇒ Object
Returns the value of attribute total_cost.
-
#total_hrs ⇒ Object
Returns the value of attribute total_hrs.
Instance Method Summary collapse
- #add_line_item(line_item) ⇒ Object
- #calculate_number ⇒ Object
- #calculate_total_cost ⇒ Object
- #calculate_total_hrs ⇒ Object
- #find_by_invoice_number(invoice_number, *boolean) ⇒ Object
- #format(x) ⇒ Object
- #git_root ⇒ Object
-
#initialize ⇒ Invoice
constructor
A new instance of Invoice.
- #project_root(file) ⇒ Object
- #save(*boolean) ⇒ Object
Constructor Details
#initialize ⇒ Invoice
Returns a new instance of Invoice.
4 5 6 7 8 9 10 11 |
# File 'lib/invoices/models/invoice.rb', line 4 def initialize calculate_number @line_items_array = [] @git_log = [] @total_hrs = 0 @total_cost = 0 @date = Time.now.strftime("%m/%d/%y") end |
Instance Attribute Details
#client_id ⇒ Object
Returns the value of attribute client_id.
3 4 5 |
# File 'lib/invoices/models/invoice.rb', line 3 def client_id @client_id end |
#date ⇒ Object
Returns the value of attribute date.
3 4 5 |
# File 'lib/invoices/models/invoice.rb', line 3 def date @date end |
#format_number ⇒ Object (readonly)
Returns the value of attribute format_number.
2 3 4 |
# File 'lib/invoices/models/invoice.rb', line 2 def format_number @format_number end |
#git_log ⇒ Object (readonly)
Returns the value of attribute git_log.
2 3 4 |
# File 'lib/invoices/models/invoice.rb', line 2 def git_log @git_log end |
#hours ⇒ Object (readonly)
Returns the value of attribute hours.
2 3 4 |
# File 'lib/invoices/models/invoice.rb', line 2 def hours @hours end |
#line_items_array ⇒ Object (readonly)
Returns the value of attribute line_items_array.
2 3 4 |
# File 'lib/invoices/models/invoice.rb', line 2 def line_items_array @line_items_array end |
#number ⇒ Object
Returns the value of attribute number.
3 4 5 |
# File 'lib/invoices/models/invoice.rb', line 3 def number @number end |
#rate ⇒ Object (readonly)
Returns the value of attribute rate.
2 3 4 |
# File 'lib/invoices/models/invoice.rb', line 2 def rate @rate end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
2 3 4 |
# File 'lib/invoices/models/invoice.rb', line 2 def root @root end |
#total_cost ⇒ Object
Returns the value of attribute total_cost.
3 4 5 |
# File 'lib/invoices/models/invoice.rb', line 3 def total_cost @total_cost end |
#total_hrs ⇒ Object
Returns the value of attribute total_hrs.
3 4 5 |
# File 'lib/invoices/models/invoice.rb', line 3 def total_hrs @total_hrs end |
Instance Method Details
#add_line_item(line_item) ⇒ Object
62 63 64 |
# File 'lib/invoices/models/invoice.rb', line 62 def add_line_item(line_item) @line_items_array << line_item end |
#calculate_number ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/invoices/models/invoice.rb', line 12 def calculate_number def format(x) if x >= 1 && x < 10 then "000#{x}" elsif x >= 10 && x < 100 then "00#{x}" elsif x >= 100 && x < 1000 then "0#{x}" elsif x >= 100 && x < 10000 then "#{x}" # raise an exception for x >= 10000 end end i = 1 Dir.foreach(File.('~/invoices')) do |filename| if filename.include?("invoice#{format(i)}.txt") i += 1 else format(i) end end @number = i @format_number = format(i) end |
#calculate_total_cost ⇒ Object
48 49 50 51 52 53 |
# File 'lib/invoices/models/invoice.rb', line 48 def calculate_total_cost @line_items_array.each do |line_item| @total_cost += line_item.cost end @total_cost end |
#calculate_total_hrs ⇒ Object
42 43 44 45 46 47 |
# File 'lib/invoices/models/invoice.rb', line 42 def calculate_total_hrs @line_items_array.each do |line_item| @total_hrs += line_item.hrs end @total_hrs end |
#find_by_invoice_number(invoice_number, *boolean) ⇒ Object
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/invoices/models/invoice.rb', line 65 def find_by_invoice_number(invoice_number, *boolean) invoice = choose_db(*boolean).execute("select * from invoices where invoice_number = #{invoice_number}").first @number = invoice[0] @date = invoice[1] @client_id = invoice[2] @total_hrs = invoice[3] @total_cost = invoice[4] return self end |
#format(x) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/invoices/models/invoice.rb', line 13 def format(x) if x >= 1 && x < 10 then "000#{x}" elsif x >= 10 && x < 100 then "00#{x}" elsif x >= 100 && x < 1000 then "0#{x}" elsif x >= 100 && x < 10000 then "#{x}" # raise an exception for x >= 10000 end end |
#git_root ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/invoices/models/invoice.rb', line 35 def git_root File.open("#{@root}/.git/logs/HEAD", "r") do |file| file.each_line do |line| @git_log << line if line.include?("commit") end end end |
#project_root(file) ⇒ Object
32 33 34 |
# File 'lib/invoices/models/invoice.rb', line 32 def project_root(file) @root = File.(file) end |
#save(*boolean) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/invoices/models/invoice.rb', line 54 def save(*boolean) calculate_total_hrs calculate_total_cost choose_db(*boolean).execute("INSERT INTO invoices (invoice_number, date, client_id, total_hrs, total_cost) VALUES (?, ?, ?, ?, ?)", [@number, @date, @client_id, @total_hrs, @total_cost]) end |