Class: Wlog::LogEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/wlog/log_entry.rb

Overview

Active Record Domain object for a log entry

Constant Summary collapse

@@table_name =

SQL ##

"log_entries"
@@insert_sql =
"INSERT INTO #{@@table_name} (description,date) values (?,?);"
@@delete_sql =
"DELETE FROM #{@@table_name} WHERE id = ? ;"
@@select_all =
"SELECT * FROM #{@@table_name} ORDER BY date ASC;"
@@update_sql =
"UPDATE #{@@table_name} SET description=? WHERE id=?;"
@@select =

@@select_all = “SELECT * FROM #@@table_name WHERE date >=#- 604800 - 24 * 60 * 60 ORDER BY date ASC”

"SELECT * FROM #{@@table_name} WHERE id = ? ;"
@@select_description_like =
"SELECT * FROM #{@@table_name} WHERE description LIKE ?;"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLogEntry

Returns a new instance of LogEntry.



9
10
11
# File 'lib/wlog/log_entry.rb', line 9

def initialize
  @date = Time.new
end

Instance Attribute Details

#dateObject

Date the entry was created



89
90
91
# File 'lib/wlog/log_entry.rb', line 89

def date
  @date
end

#descriptionObject

Text description for the log entry



86
87
88
# File 'lib/wlog/log_entry.rb', line 86

def description
  @description
end

#idObject

The identity field for the log entry DO



83
84
85
# File 'lib/wlog/log_entry.rb', line 83

def id
  @id
end

Class Method Details

.create_tableObject

TODO this shouldn’t be here



39
40
41
# File 'lib/wlog/log_entry.rb', line 39

def self.create_table
  DbRegistry.instance.execute(@@create_sql)
end

.delete(id) ⇒ Object



34
35
36
# File 'lib/wlog/log_entry.rb', line 34

def self.delete(id)
  DbRegistry.instance.execute(@@delete_sql,id)
end

.find(id) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/wlog/log_entry.rb', line 13

def self.find(id)
  row = DbRegistry.instance.execute(@@select,id).first
  le = LogEntry.new
  le.id = row[0]
  le.description = row[1]
  le.date = Time.at(row[2])
  le
end

.find_allObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/wlog/log_entry.rb', line 22

def self.find_all
  all = Array.new
  DbRegistry.instance.execute(@@select_all).each do |row|
    le = LogEntry.new
    le.id = row[0]
    le.description = row[1]
    le.date = Time.at(row[2])
    all.push le
  end
  all
end

.search_descriptions(term) ⇒ Object

Search by string to find a matching description with ‘LIKE’.



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/wlog/log_entry.rb', line 49

def self.search_descriptions(term)
  all = Array.new
  DbRegistry.instance.execute(@@select_description_like,"%#{term}%").each do |row|
    le = LogEntry.new
    le.id = row[0]
    le.description = row[1]
    le.date = Time.at(row[2])
    all.push le
  end
  all
end

Instance Method Details

#deleteObject

Delete the loaded log entry currently in memory, by passing its id



66
67
68
# File 'lib/wlog/log_entry.rb', line 66

def delete
  self.delete(self.id)
end

#insertObject



61
62
63
# File 'lib/wlog/log_entry.rb', line 61

def insert
  DbRegistry.instance.execute(@@insert_sql,@description,@date.to_i)
end

#to_sObject

Print things nicely formmated no more than 80 cars (well, unless you stick the time in the end which is not counted for).



72
73
74
75
76
77
78
79
80
# File 'lib/wlog/log_entry.rb', line 72

def to_s
  str    = "[#{id}] "
  tmp    = @description + " [#{@date.strftime("%H:%M:%S")}]"
  desc   = Wlog::Helpers.break_string(tmp,80)
  indent = " " * (id.to_s.split('').count + 5)
  desc.gsub!(/#{$/}/, "#{$/}#{indent}")
  str.concat(desc)
  str
end

#updateObject

update the entry



44
45
46
# File 'lib/wlog/log_entry.rb', line 44

def update
  DbRegistry.instance.execute(@@update_sql,@description,@id)
end