Sha256: 478a41b5c7b1b6b69d7e190feb3f3b06e2531b3d4e3bceb9afc32016647e5705

Contents?: true

Size: 1.87 KB

Versions: 3

Compression:

Stored size: 1.87 KB

Contents

# Timestamp function for Rhubarb.
#
# Copyright (c) 2009--2010 Red Hat, Inc.
#
# Author:  William Benton (willb@redhat.com)
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0

require 'time'
require 'yaml'
require 'zlib'

module Rhubarb
  module Util
    # A higher-resolution timestamp
    def self.timestamp(tm=nil)
      tm ||= Time.now.utc
      (tm.tv_sec * 1000000) + tm.tv_usec
    end

    # Identity for objects that may be used as foreign keys
    def self.rhubarb_fk_identity(object)
      (object.class.ancestors.include?(Persisting) || object.class.ancestors.include?(Rhubarb::Persisting)) ? object.row_id : object
    end

    def self.truthify_proc
      @truthify_proc ||= Proc.new {|obj| !!(obj) ? "true" : "false"}
    end
    
    def self.blobify_proc
      @blobify_proc ||= Proc.new {|obj| obj.is_a?(SQLite3::Blob) ? obj : SQLite3::Blob.new(obj)}
    end
    
    def self.zblobify_proc
      @zblobify_proc ||= Proc.new {|obj| obj.is_a?(SQLite3::Blob) ? obj : SQLite3::Blob.new(Zlib::Deflate.deflate(obj))}
    end
    
    def self.dezblobify_proc
      @dezblobify_proc ||= Proc.new do |obj| 
        if obj.nil? || obj == ""
          nil
        else
          Zlib::Inflate.inflate(obj)
        end
      end
    end
    
    def self.swizzle_object_proc
      @swizzle_object_proc ||= Proc.new do |obj| 
        yamlrepr = obj.to_yaml
        SQLite3::Blob.new(Zlib::Deflate.deflate(yamlrepr, Zlib::BEST_COMPRESSION))
      end
    end

    def self.deswizzle_object_proc
      @deswizzle_object_proc ||= Proc.new do |zy_obj|
        if zy_obj.nil? || zy_obj == ""
          nil
        else
          obj = YAML.load(Zlib::Inflate.inflate(zy_obj))
          obj.freeze
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rhubarb-0.4.3 lib/rhubarb/util.rb
rhubarb-0.4.2 lib/rhubarb/util.rb
rhubarb-0.4.0 lib/rhubarb/util.rb