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.



115
116
117
118
119
# File 'lib/rdbi.rb', line 115

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



133
134
135
# File 'lib/rdbi.rb', line 133

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. ++



144
145
146
147
148
149
150
151
152
153
# File 'lib/rdbi.rb', line 144

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

.index_binds(args, index_map) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/rdbi.rb', line 155

def self.index_binds(args, index_map)
  # FIXME exception if mixed hash/indexed binds
  if args[0].kind_of?(Hash)
    binds = []
    args[0].keys.each do |key| 
      if index = index_map.index(key)
        binds.insert(index, args[0][key])
      end
    end
    return binds
  else
    return args
  end
end

.key_hash_as_symbols(hash) ⇒ Object

Rekey a string-keyed hash with equivalent symbols.



124
125
126
127
128
# File 'lib/rdbi.rb', line 124

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.



106
107
108
109
110
# File 'lib/rdbi.rb', line 106

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