Class: Wlog::Attachment
- Inherits:
-
Object
- Object
- Wlog::Attachment
- Includes:
- AttachmentSql, PolymorphicAttachmentsSql
- Defined in:
- lib/wlog/domain/attachment.rb
Overview
OO way of handling blobs of data, to be stored in memory or in db.
Constant Summary
Constants included from PolymorphicAttachmentsSql
PolymorphicAttachmentsSql::DeleteSql, PolymorphicAttachmentsSql::InsertSql, PolymorphicAttachmentsSql::SelectSql, PolymorphicAttachmentsSql::TableName
Constants included from AttachmentSql
Wlog::AttachmentSql::DeleteSql, Wlog::AttachmentSql::InsertSql, Wlog::AttachmentSql::SelectSql, Wlog::AttachmentSql::TableName
Instance Attribute Summary collapse
-
#caller_id ⇒ Object
The caller id of the calling object.
-
#caller_name ⇒ Object
The class name of the calling object.
-
#data ⇒ Object
Container for the actual binary data of whatever you’re attaching.
-
#db ⇒ Object
The database handle for the active record.
-
#filename ⇒ Object
The original filename of the file.
-
#given_name ⇒ Object
optional given name for the attachment.
-
#id ⇒ Object
Identifier of the object.
Class Method Summary collapse
-
.find(db, name, id) ⇒ Object
Find an attachment by an identifier and polymorphic name.
-
.find_all_by_discriminator(db, name, id) ⇒ Object
Find an attachment given an id.
Instance Method Summary collapse
-
#delete_by_discriminator(name, id) ⇒ Object
Delete an attachment.
-
#initialize(dbhandle, caller_name, caller_id) ⇒ Attachment
constructor
Can only initialize with a caller name and id, since relations to attachments are polymorphic.
-
#insert ⇒ Object
Insert an attachment.
-
#quick_assign!(row) ⇒ Object
Assign a row of data to self.
Constructor Details
#initialize(dbhandle, caller_name, caller_id) ⇒ Attachment
Can only initialize with a caller name and id, since relations to attachments are polymorphic.
14 15 16 |
# File 'lib/wlog/domain/attachment.rb', line 14 def initialize(dbhandle, caller_name, caller_id) @caller_name, @caller_id, @db = caller_name, caller_id, dbhandle end |
Instance Attribute Details
#caller_id ⇒ Object
The caller id of the calling object
82 83 84 |
# File 'lib/wlog/domain/attachment.rb', line 82 def caller_id @caller_id end |
#caller_name ⇒ Object
The class name of the calling object
79 80 81 |
# File 'lib/wlog/domain/attachment.rb', line 79 def caller_name @caller_name end |
#data ⇒ Object
Container for the actual binary data of whatever you’re attaching.
70 71 72 |
# File 'lib/wlog/domain/attachment.rb', line 70 def data @data end |
#db ⇒ Object
The database handle for the active record
85 86 87 |
# File 'lib/wlog/domain/attachment.rb', line 85 def db @db end |
#filename ⇒ Object
The original filename of the file
73 74 75 |
# File 'lib/wlog/domain/attachment.rb', line 73 def filename @filename end |
#given_name ⇒ Object
optional given name for the attachment
76 77 78 |
# File 'lib/wlog/domain/attachment.rb', line 76 def given_name @given_name end |
#id ⇒ Object
Identifier of the object
67 68 69 |
# File 'lib/wlog/domain/attachment.rb', line 67 def id @id end |
Class Method Details
.find(db, name, id) ⇒ Object
Find an attachment by an identifier and polymorphic name
39 40 41 42 43 44 45 46 |
# File 'lib/wlog/domain/attachment.rb', line 39 def self.find(db, name, id) row = db.execute(AttachmentSql::SelectSql, id).first att = nil if row && !row.empty? att = Attachment.new(db, name, id) att.quick_assign!(row) end att end |
.find_all_by_discriminator(db, name, id) ⇒ Object
Find an attachment given an id
20 21 22 23 24 25 26 27 28 |
# File 'lib/wlog/domain/attachment.rb', line 20 def self.find_all_by_discriminator(db, name, id) arr = Array.new rows = db.execute( PolymorphicAttachmentsSql::SelectSql, name, id) rows.each do |row| arr.push self.find(db, name, row[2]) end arr end |
Instance Method Details
#delete_by_discriminator(name, id) ⇒ Object
Delete an attachment
32 33 34 |
# File 'lib/wlog/domain/attachment.rb', line 32 def delete_by_discriminator(name, id) @db.execute(DeleteSql, id) end |
#insert ⇒ Object
Insert an attachment. This also creates the relation in the polymorphic table.
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/wlog/domain/attachment.rb', line 50 def insert unless @id @db.execute( AttachmentSql::InsertSql, @filename, @given_name, @data) ret = @db.last_row_from(AttachmentSql::TableName) @id = ret.first[0].to_i @db.execute( PolymorphicAttachmentsSql::InsertSql, @caller_name, @caller_id, @id) end end |
#quick_assign!(row) ⇒ Object
Assign a row of data to self
62 63 64 |
# File 'lib/wlog/domain/attachment.rb', line 62 def quick_assign!(row) @id, @filename, @given_name, @data = row[0], row[1], row[2], row[3] nil end |