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_from_class_or_symbol(klass, namespace) ⇒ Object
This is the loading logic we use to import drivers of various natures.
-
.deep_copy(obj) ⇒ Object
Copy an object and all of its descendants to form a new tree.
- .index_binds(args, index_map) ⇒ Object
-
.key_hash_as_symbols(hash) ⇒ Object
Rekey a string-keyed hash with equivalent symbols.
- .make_fini_proc(obj, meth, *args) ⇒ Object
-
.naive_require(lib) ⇒ Object
Require and swallow errors.
-
.optional_require(lib) ⇒ Object
Requires with a LoadError check and emits a friendly “please install me” message.
-
.resolve_driver(driver) ⇒ Object
Given a short driver name (e.g., :FauxSQL), return the corresponding class, attempting to load the driver library if needed.
- .upon_finalize!(what, o, meth, *args) ⇒ Object
Class Method Details
.class_from_class_or_symbol(klass, namespace) ⇒ Object
This is the loading logic we use to import drivers of various natures.
154 155 156 157 158 |
# File 'lib/rdbi.rb', line 154 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
172 173 174 |
# File 'lib/rdbi.rb', line 172 def self.deep_copy(obj) Marshal.load(Marshal.dump(obj)) end |
.index_binds(args, index_map) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/rdbi.rb', line 176 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.
163 164 165 166 167 |
# File 'lib/rdbi.rb', line 163 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
204 205 206 |
# File 'lib/rdbi.rb', line 204 def self.make_fini_proc(obj, meth, *args) proc { |object_id| obj.__send__(meth.to_sym, *args) rescue nil } end |
.naive_require(lib) ⇒ Object
Require and swallow errors. Returns true if module loaded (for the first time), false if already loaded or unable to load
131 132 133 134 135 |
# File 'lib/rdbi.rb', line 131 def self.naive_require(lib) require lib rescue LoadError nil end |
.optional_require(lib) ⇒ Object
Requires with a LoadError check and emits a friendly “please install me” message.
123 124 125 126 127 |
# File 'lib/rdbi.rb', line 123 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 |
.resolve_driver(driver) ⇒ Object
Given a short driver name (e.g., :FauxSQL), return the corresponding class, attempting to load the driver library if needed.
139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/rdbi.rb', line 139 def self.resolve_driver(driver) loop do return ::RDBI::Driver.const_get(driver.to_sym) if ::RDBI::Driver.const_defined?(driver.to_sym) # const_defined? will throw a NameError if driver is not a valid # constant name redo if naive_require("rdbi-driver-#{driver.to_s.downcase}") redo if naive_require("rdbi/driver/#{driver.to_s.downcase}") break end raise ArgumentError.new("Unable to qualify driver #{driver}") end |
.upon_finalize!(what, o, meth, *args) ⇒ Object
200 201 202 |
# File 'lib/rdbi.rb', line 200 def self.upon_finalize!(what, o, meth, *args) ObjectSpace.define_finalizer(what, make_fini_proc(o, meth, *args)) end |