Class: Turntables::VersionHistory

Inherits:
Object
  • Object
show all
Includes:
VersionHistorySql
Defined in:
lib/turntables/version_history.rb

Overview

lightweight and small (therefore not too many components are needed yet).

Constant Summary

Constants included from VersionHistorySql

Turntables::VersionHistorySql::Create, Turntables::VersionHistorySql::Insert, Turntables::VersionHistorySql::SelectAll, Turntables::VersionHistorySql::SelectById, Turntables::VersionHistorySql::SelectLast, Turntables::VersionHistorySql::TableName

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version, comment, date = Time.now.to_i) ⇒ VersionHistory

Create the version history object by giving version number and comment. The date can be omitted, and it will be set to the current time of the local system.

Parameters:

  • version

    is the version of the transaction that was performed

  • comment

    was any comments included in the sql file

  • date (defaults to: Time.now.to_i)

    is the optional date, set to the local time if unspecified



22
23
24
25
26
# File 'lib/turntables/version_history.rb', line 22

def initialize(version,comment,date=Time.now.to_i)
  @version = version
  @comment = comment
  @date    = date
end

Instance Attribute Details

#commentObject

Returns the value of attribute comment.



86
87
88
# File 'lib/turntables/version_history.rb', line 86

def comment
  @comment
end

#dateObject

Returns the value of attribute date.



85
86
87
# File 'lib/turntables/version_history.rb', line 85

def date
  @date
end

#idObject

Returns the value of attribute id.



83
84
85
# File 'lib/turntables/version_history.rb', line 83

def id
  @id
end

#versionObject

Returns the value of attribute version.



84
85
86
# File 'lib/turntables/version_history.rb', line 84

def version
  @version
end

Class Method Details

.checkObject

Check if the required database exists. Check to see if the database has the version_histories table as well.



30
31
32
33
34
35
36
# File 'lib/turntables/version_history.rb', line 30

def self.check
  if DbRegistry.instance.table_exists? TableName
    return VersionHistory.find_last
  else # db does not exist
    return :fresh
  end
end

.find(id) ⇒ Object

Find a version history record by id

Returns:

  • VersionHistory object or nil



56
57
58
59
# File 'lib/turntables/version_history.rb', line 56

def self.find(id)
  ret = DbRegistry.instance.execute(SelectById, id)
  VersionHistory.to_version_history(ret.first)
end

.find_allObject

Get all the stored version histories



39
40
41
42
43
44
45
# File 'lib/turntables/version_history.rb', line 39

def self.find_all
  vhs = Array.new
  DbRegistry.instance.execute(SelectAll).each do |row|
    vhs.push VersionHistory.to_version_history(row)
  end
  vhs
end

.find_lastObject

Find the last version history added by checking the max id

Returns:

  • VersionHistory object or nil



49
50
51
52
# File 'lib/turntables/version_history.rb', line 49

def self.find_last
  ret = DbRegistry.instance.execute(SelectLast)
  VersionHistory.to_version_history(ret.first)
end

.insert(vhistory) ⇒ Object

Insert a row into the table for version histories

Returns:

  • insert result



63
64
65
66
# File 'lib/turntables/version_history.rb', line 63

def self.insert(vhistory)
  DbRegistry.instance.execute(Insert, vhistory.version, 
                              vhistory.date, vhistory.comment)
end

.pull_up!Object

Create the table of this model

Returns:

  • nil



70
71
72
# File 'lib/turntables/version_history.rb', line 70

def self.pull_up!
  DbRegistry.instance.execute(Create)
end

.to_version_history(record) ⇒ Object

Make an array representing the persisted object, into an object in memory

Parameters:

  • record

    an array containing the elements to construct a version history

Returns:

  • a VersionHistory object constructed from the records



77
78
79
80
81
# File 'lib/turntables/version_history.rb', line 77

def self.to_version_history(record)
  vh = VersionHistory.new(record[1], record[2], record[3])
  vh.id = record[0]
  vh
end