Class: Wlog::Attachment

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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_idObject

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_nameObject

The class name of the calling object



79
80
81
# File 'lib/wlog/domain/attachment.rb', line 79

def caller_name
  @caller_name
end

#dataObject

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

#dbObject

The database handle for the active record



85
86
87
# File 'lib/wlog/domain/attachment.rb', line 85

def db
  @db
end

#filenameObject

The original filename of the file



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

def filename
  @filename
end

#given_nameObject

optional given name for the attachment



76
77
78
# File 'lib/wlog/domain/attachment.rb', line 76

def given_name
  @given_name
end

#idObject

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

Parameters:

  • id

    is the identifier of the attachment to find

  • name

    is the name of the polymorphic thing



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

Parameters:

  • id

    is the attachment id to find



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

Parameters:

  • id

    the attachment with the id to delete



32
33
34
# File 'lib/wlog/domain/attachment.rb', line 32

def delete_by_discriminator(name, id)
  @db.execute(DeleteSql, id)
end

#insertObject

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