Class: TDP::DAO
- Inherits:
-
Object
- Object
- TDP::DAO
- Defined in:
- lib/tdp.rb
Overview
Data access object that encapsulates all operations with the database.
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Sequel::Database object.
Instance Method Summary collapse
-
#applied_patches ⇒ Object
Fetches the information about applied patches and returns it as { name => signature } hash.
-
#applied_patches_inverse ⇒ Object
Fetches the information about applied patches and returns it as { signature => name } hash.
-
#apply(patch) ⇒ Object
Applies a patch (a Patch object).
-
#bootstrap ⇒ Object
Initializes database tables for keeping track of applied patches.
-
#erase ⇒ Object
Erases all data about applied patches.
-
#initialize(db) ⇒ DAO
constructor
Creates a new DAO object.
-
#patch_signature(name) ⇒ Object
Looks up a signature of a patch by its name.
-
#register(patch) ⇒ Object
Registers a patch (a Patch object) as applied.
-
#rename(old_name, new_name) ⇒ Object
Renames a patch.
Constructor Details
#initialize(db) ⇒ DAO
Creates a new DAO object.
- db
-
must be either of:
-
instance of Sequel::Database class
-
database URL that can be passed to Sequel.connect()
216 217 218 219 220 221 222 223 224 225 |
# File 'lib/tdp.rb', line 216 def initialize(db) case db when Sequel::Database @db = db when String @db = Sequel.connect(db) else raise ArgumentError, "Invalid argument #{db} of class #{db.class}" end end |
Instance Attribute Details
#db ⇒ Object (readonly)
Sequel::Database object
207 208 209 |
# File 'lib/tdp.rb', line 207 def db @db end |
Instance Method Details
#applied_patches ⇒ Object
Fetches the information about applied patches and returns it as { name => signature } hash.
245 246 247 248 249 250 251 |
# File 'lib/tdp.rb', line 245 def applied_patches result = {} @db[:tdp_patch].select(:name, :signature).each do |row| result[row[:name]] = row[:signature] end result end |
#applied_patches_inverse ⇒ Object
Fetches the information about applied patches and returns it as { signature => name } hash.
257 258 259 260 261 262 263 264 |
# File 'lib/tdp.rb', line 257 def applied_patches_inverse result = {} applied_patches.each do |name, sig| raise DuplicateError, [result[sig], name] if result.key?(sig) result[sig] = name end result end |
#apply(patch) ⇒ Object
Applies a patch (a Patch object).
277 278 279 280 281 282 283 |
# File 'lib/tdp.rb', line 277 def apply(patch) @db << patch.content register(patch) rescue Sequel::Error => ex raise Sequel::Error, "Failed to apply patch #{patch.full_filename}: #{ex}" end |
#bootstrap ⇒ Object
Initializes database tables for keeping track of applied patches.
231 232 233 234 235 236 237 238 239 |
# File 'lib/tdp.rb', line 231 def bootstrap return if @db.table_exists?(:tdp_patch) @db << %{ CREATE TABLE tdp_patch( name VARCHAR PRIMARY KEY , signature VARCHAR NOT NULL ) } end |
#erase ⇒ Object
Erases all data about applied patches.
303 304 305 |
# File 'lib/tdp.rb', line 303 def erase @db[:tdp_patch].delete end |
#patch_signature(name) ⇒ Object
Looks up a signature of a patch by its name.
269 270 271 272 |
# File 'lib/tdp.rb', line 269 def patch_signature(name) row = @db[:tdp_patch].select(:signature).where(name: name).first row.nil? ? nil : row[:signature] end |
#register(patch) ⇒ Object
Registers a patch (a Patch object) as applied.
288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/tdp.rb', line 288 def register(patch) q = @db[:tdp_patch].where(name: patch.name) if q.empty? @db[:tdp_patch].insert( name: patch.name, signature: patch.signature ) else q.update(signature: patch.signature) end end |
#rename(old_name, new_name) ⇒ Object
Renames a patch.
310 311 312 |
# File 'lib/tdp.rb', line 310 def rename(old_name, new_name) @db[:tdp_patch].where(name: old_name).update(name: new_name) end |