Class: Wlog::LogEntry
- Inherits:
-
Object
- Object
- Wlog::LogEntry
- 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
-
#date ⇒ Object
Date the entry was created.
-
#description ⇒ Object
Text description for the log entry.
-
#id ⇒ Object
The identity field for the log entry DO.
Class Method Summary collapse
-
.create_table ⇒ Object
TODO this shouldn’t be here.
- .delete(id) ⇒ Object
- .find(id) ⇒ Object
- .find_all ⇒ Object
-
.search_descriptions(term) ⇒ Object
Search by string to find a matching description with ‘LIKE’.
Instance Method Summary collapse
-
#delete ⇒ Object
Delete the loaded log entry currently in memory, by passing its id.
-
#initialize ⇒ LogEntry
constructor
A new instance of LogEntry.
- #insert ⇒ Object
-
#to_s ⇒ Object
Print things nicely formmated no more than 80 cars (well, unless you stick the time in the end which is not counted for).
-
#update ⇒ Object
update the entry.
Constructor Details
#initialize ⇒ LogEntry
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
#date ⇒ Object
Date the entry was created
89 90 91 |
# File 'lib/wlog/log_entry.rb', line 89 def date @date end |
#description ⇒ Object
Text description for the log entry
86 87 88 |
# File 'lib/wlog/log_entry.rb', line 86 def description @description end |
#id ⇒ Object
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_table ⇒ Object
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_all ⇒ Object
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
#delete ⇒ Object
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 |
#insert ⇒ Object
61 62 63 |
# File 'lib/wlog/log_entry.rb', line 61 def insert DbRegistry.instance.execute(@@insert_sql,@description,@date.to_i) end |
#to_s ⇒ Object
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 |
#update ⇒ Object
update the entry
44 45 46 |
# File 'lib/wlog/log_entry.rb', line 44 def update DbRegistry.instance.execute(@@update_sql,@description,@id) end |