Class: Wlog::LogEntry

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

Overview

Active Record Domain object for a log entry

Constant Summary

Constants included from LogEntrySql

Wlog::LogEntrySql::DeleteSql, Wlog::LogEntrySql::InsertSql, Wlog::LogEntrySql::Select, Wlog::LogEntrySql::SelectAll, Wlog::LogEntrySql::SelectAllByIssue, Wlog::LogEntrySql::SelectAllLimit, Wlog::LogEntrySql::SelectDescriptionLike, Wlog::LogEntrySql::TableName, Wlog::LogEntrySql::UpdateSql

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db_handle) ⇒ LogEntry

Returns a new instance of LogEntry.



12
13
14
15
# File 'lib/wlog/domain/log_entry.rb', line 12

def initialize(db_handle)
  @date = Time.new
  @db = db_handle
end

Instance Attribute Details

#dateObject

Date the entry was created



95
96
97
# File 'lib/wlog/domain/log_entry.rb', line 95

def date
  @date
end

#dbObject

The db handle



101
102
103
# File 'lib/wlog/domain/log_entry.rb', line 101

def db
  @db
end

#descriptionObject

Text description for the log entry



92
93
94
# File 'lib/wlog/domain/log_entry.rb', line 92

def description
  @description
end

#idObject

The identity field for the log entry DO



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

def id
  @id
end

#issue_idObject

The issue id (parent of this log entry)



98
99
100
# File 'lib/wlog/domain/log_entry.rb', line 98

def issue_id
  @issue_id
end

Class Method Details

.delete_by_id(db, id) ⇒ Object

Delete a log entry with a given id.

Examples:

Simple usage

# Since this is a class method:
LogEntry.delete(12)


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

def self.delete_by_id(db, id)
  db.execute(DeleteSql,id)
end

.find(db, id) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/wlog/domain/log_entry.rb', line 17

def self.find(db, id)
  row = db.execute(Select,id).first
  le = nil
  if row && !row.empty?
  le = LogEntry.new(db)
  le.quick_assign!(row[0], row[1], Time.at(row[2]), row[3])
  end
le end

.find_all(db) ⇒ Object



26
27
28
# File 'lib/wlog/domain/log_entry.rb', line 26

def self.find_all(db)
  self.generic_find_all(db, SelectAll)
end

.find_all_by_issue_id(db, id) ⇒ Object



30
31
32
# File 'lib/wlog/domain/log_entry.rb', line 30

def self.find_all_by_issue_id(db, id)
  self.generic_find_all(db, SelectAllByIssue, id)
end

.search_descriptions(db, term) ⇒ Object

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



48
49
50
51
52
53
54
55
# File 'lib/wlog/domain/log_entry.rb', line 48

def self.search_descriptions(db, term)
  all = Array.new
  db.execute(SelectDescriptionLike,"%#{term}%").each do |row|
    le = LogEntry.new
    le.quick_assign!(row[0], row[1], 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
69
70
71
# File 'lib/wlog/domain/log_entry.rb', line 66

def delete
  if @id
    LogEntry.delete_by_id(@db, @id)
    @id = nil
  end
end

#insertObject



57
58
59
60
61
62
63
# File 'lib/wlog/domain/log_entry.rb', line 57

def insert
  raise 'Need issue_id' unless @issue_id
  unless @id
    @db.execute(InsertSql, @description, @date.to_i, @issue_id)
    @id = @db.last_row_from(TableName).first[0].to_i
  end
end

#quick_assign!(id, desc, date, issue_id) ⇒ Object



73
74
75
# File 'lib/wlog/domain/log_entry.rb', line 73

def quick_assign!(id,desc,date,issue_id)
  @id, @description, @date, @issue_id = id, desc, date, issue_id
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).



79
80
81
82
83
84
85
86
# File 'lib/wlog/domain/log_entry.rb', line 79

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

#updateObject

update the entry



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

def update
  @db.execute(UpdateSql,@description,@id)
end