class BatchKit::Database::MD5

Records an MD5 hash of String objects, which are used to detect when items such as jobs have changed. This in turn is used to increment a version number on objects.

Public Class Methods

check(obj_type, obj_name, string) click to toggle source

Checks to see if the recorded MD5 digest of string matches the MD5 digest of string as calculated by Digest::MD5.

@return [Boolean, String] Returns two values in an array: a boolean

indicating whether the digest value is the same, and the actual
calculated value for the MD5 digest of +string+.
# File lib/batch-kit/database/models.rb, line 46
def self.check(obj_type, obj_name, string)
    digest = Digest::MD5.hexdigest(string)
    # Attempt to retrieve the MD5 for the schema; could fail if not deployed
    md5 = self.for(obj_name, obj_type, digest) rescue nil
    if md5
        [md5.md5_id, md5]
    else
        [nil, self.new(obj_type, obj_name, string, digest)]
    end
end
check_schema(schema) click to toggle source

Checks that the BatchKit database tables have been deployed and match the table definitions in schema.rb.

# File lib/batch-kit/database/models.rb, line 27
def self.check_schema(schema)
    schema_file = IO.read("#{File.dirname(__FILE__)}/schema.rb")
    ok, md5 = self.check('SCHEMA', 'schema.rb', schema_file)
    unless ok
        # TODO: Find a better way to update schema for table changes;
        #       This method throws away all history
        schema.drop_tables
        schema.create_tables
        md5.save
    end
end
for(obj_name, obj_type, digest) click to toggle source

Locate the MD5 record for the object named obj_name whose type is obj_type.

# File lib/batch-kit/database/models.rb, line 18
def self.for(obj_name, obj_type, digest)
    self.where(Sequel.function(:upper, :object_name) => obj_name.upcase,
               Sequel.function(:upper, :object_type) => obj_type.upcase,
               :md5_digest => digest).first
end
new(obj_type, obj_name, string, digest = nil) click to toggle source

Create a new MD5 hash of an object

Calls superclass method
# File lib/batch-kit/database/models.rb, line 59
def initialize(obj_type, obj_name, string, digest = nil)
    obj_ver = self.class.where(Sequel.function(:upper, :object_name) => obj_name.upcase,
                               Sequel.function(:upper, :object_type) => obj_type.upcase).
                         max(:object_version) || 0
    super(object_type: obj_type, object_name: obj_name,
          object_version: obj_ver + 1,
          md5_digest: digest || Digest::MD5.hexdigest(string),
          md5_created_at: model.dataset.current_datetime)
end