Module: MicroQ::Util

Defined in:
lib/micro_q/util.rb

Class Method Summary collapse

Class Method Details

.constantize(word) ⇒ Object

Stolen from active_support/inflector/inflections with a rescue to nil.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/micro_q/util.rb', line 6

def self.constantize(word)
  names = word.split('::')
  names.shift if names.empty? || names.first.empty?

  constant = Object
  names.each do |name| # Compatible with Ruby 1.9 and above. Before 1.9 the arity of #const_defined? was 1.
    constant = constant.const_defined?(name, false) ? constant.const_get(name) : constant.const_missing(name)
  end
  constant
rescue
end

.json_parseObject



18
19
20
# File 'lib/micro_q/util.rb', line 18

def self.json_parse
  @@json_parse ||= proc {|entry| JSON.parse(entry) }
end

.safe_require(lib) ⇒ Object

Attempt to load a library but return nil if it cannot be loaded



45
46
47
48
# File 'lib/micro_q/util.rb', line 45

def self.safe_require(lib)
  require lib
rescue LoadError
end

.stringify(*args) ⇒ Object



22
23
24
25
26
# File 'lib/micro_q/util.rb', line 22

def self.stringify(*args)
  args.collect do |a|
    stringify_keys(a)
  end
end

.stringify_keys(hash) ⇒ Object

Copy a hash and convert all keys to strings. Stringifies to infinite hash depth



32
33
34
35
36
37
38
39
40
# File 'lib/micro_q/util.rb', line 32

def self.stringify_keys(hash)
  {}.tap do |result|
    hash.keys.each do |key|
      value = hash[key]

      result[key.to_s] = value.is_a?(Hash) ? stringify_keys(value) : value
    end
  end
end