module XapianDb::Utilities

Public Instance Methods

assert_valid_keys(hash, *valid_keys) click to toggle source

Taken from Rails

   # File lib/xapian_db/utilities.rb
45 def assert_valid_keys(hash, *valid_keys)
46   unknown_keys = hash.keys - [valid_keys].flatten
47   raise(ArgumentError, "Unsupported option(s) detected: #{unknown_keys.join(", ")}") unless unknown_keys.empty?
48 end
camelize(string) click to toggle source

Convert a string to camel case @param [String] The string to camelize @return [String] The camelized string

   # File lib/xapian_db/utilities.rb
13 def camelize(string)
14   string.split(/[^a-z0-9]/i).map{|w| w.capitalize}.join
15 end
constantize(camel_cased_word) click to toggle source

Taken from Rails

   # File lib/xapian_db/utilities.rb
18 def constantize(camel_cased_word)
19   names = camel_cased_word.split('::')
20   names.shift if names.empty? || names.first.empty?
21 
22   names.inject(Object) do |constant, name|
23     if constant == Object
24       constant.const_get(name)
25     else
26       candidate = constant.const_get(name)
27       next candidate if constant.const_defined?(name, false)
28       next candidate unless Object.const_defined?(name)
29 
30       # Go down the ancestors to check it it's owned
31       # directly before we reach Object or the end of ancestors.
32       constant = constant.ancestors.inject do |const, ancestor|
33         break const    if ancestor == Object
34         break ancestor if ancestor.const_defined?(name, false)
35         const
36       end
37 
38       # owner is in Object, so raise
39       constant.const_get(name, false)
40     end
41   end
42 end