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.


139
140
141
142
143
# File 'lib/rdbi.rb', line 139

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


157
158
159
# File 'lib/rdbi.rb', line 157

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

.format_results(row_count, ary) ⇒ Object

Takes an array and appropriate boxes/deboxes it based on what was requested.

– FIXME this is a really poorly performing way of doing this. ++


168
169
170
171
172
173
174
175
176
177
# File 'lib/rdbi.rb', line 168

def self.format_results(row_count, ary)
  case row_count
  when :first, :last
    ary = ary[0]
    return nil if ary.empty?
    return ary
  else
    return ary
  end
end

.key_hash_as_symbols(hash) ⇒ Object

Rekey a string-keyed hash with equivalent symbols.


148
149
150
151
152
# File 'lib/rdbi.rb', line 148

def self.key_hash_as_symbols(hash)
  return nil unless hash

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

.optional_require(lib) ⇒ Object

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


130
131
132
133
134
# File 'lib/rdbi.rb', line 130

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