Module: RDBI::Util

Defined in:
lib/rdbi.rb

Overview

RDBI::Util is a set of utility methods used internally. It is not geared for public consumption.

Class Method Summary collapse

Class Method Details

.class_from_class_or_symbol(klass, namespace) ⇒ Object

This is the loading logic we use to import drivers of various natures.



127
128
129
130
131
# File 'lib/rdbi.rb', line 127

def self.class_from_class_or_symbol(klass, namespace)
  klass.kind_of?(Class) ? klass : namespace.const_get(klass.to_s)
rescue
  raise ArgumentError, "Invalid argument for driver name; must be Class, or a Symbol or String identifying the Class, and the driver Class must have been loaded"
end

.deep_copy(obj) ⇒ Object

Copy an object and all of its descendants to form a new tree



145
146
147
# File 'lib/rdbi.rb', line 145

def self.deep_copy(obj)
  Marshal.load(Marshal.dump(obj))
end

.index_binds(args, index_map) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/rdbi.rb', line 149

def self.index_binds(args, index_map)
  # FIXME exception if mixed hash/indexed binds
  
  if args.empty? or !args.find { |x| x.kind_of?(Hash) }
    return args
  end

  if args.kind_of?(Hash)
    binds = []
    hash = args
  else
    hashes, binds = args.partition { |x| x.kind_of?(Hash) }
    hash = hashes.inject({ }, :merge)
  end

  hash.each do |key, value| 
    # XXX yes, we want to *assign* here.
    if index = index_map.index(key)
      binds.insert(index, value)
    end
  end
  return binds
end

.key_hash_as_symbols(hash) ⇒ Object

Rekey a string-keyed hash with equivalent symbols.



136
137
138
139
140
# File 'lib/rdbi.rb', line 136

def self.key_hash_as_symbols(hash)
  return nil unless hash

  Hash[hash.map { |k,v| [k.to_sym, v] }]
end

.make_fini_proc(obj, meth, *args) ⇒ Object



177
178
179
# File 'lib/rdbi.rb', line 177

def self.make_fini_proc(obj, meth, *args)
  proc { |object_id| obj.__send__(meth.to_sym, *args) rescue nil }
end

.optional_require(lib) ⇒ Object

Requires with a LoadError check and emits a friendly “please install me” message.



118
119
120
121
122
# File 'lib/rdbi.rb', line 118

def self.optional_require(lib)
  require lib
rescue LoadError => e
  raise LoadError, "The '#{lib}' gem is required to use this driver. Please install it."
end

.upon_finalize!(what, o, meth, *args) ⇒ Object



173
174
175
# File 'lib/rdbi.rb', line 173

def self.upon_finalize!(what, o, meth, *args)
  ObjectSpace.define_finalizer(what, make_fini_proc(o, meth, *args))
end