Module: QueueBus::Util

Extended by:
Util
Included in:
Util
Defined in:
lib/queue_bus/util.rb

Defined Under Namespace

Classes: DecodeException

Instance Method Summary collapse

Instance Method Details

#camelize(term) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/queue_bus/util.rb', line 51

def camelize(term)
  string = term.to_s
  # string = string.sub(/^[a-z\d]*/) { inflections.acronyms[$&] || $&.capitalize }
  string = string.sub(/^[a-z\d]*/) { $&.capitalize }
  # string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{inflections.acronyms[$2] || $2.capitalize}" }
  string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" }
  string.gsub!(/\//, '::')
  string
end

#classify(table_name) ⇒ Object



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

def classify(table_name)
  # strip out any leading schema name
  # camelize(singularize(table_name.to_s.sub(/.*\./, '')))
  camelize(table_name.to_s.sub(/.*\./, ''))
end

#constantize(camel_cased_word) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/queue_bus/util.rb', line 61

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

  names.inject(Object) do |constant, name|
    if constant == Object
      constant.const_get(name)
    else
      candidate = constant.const_get(name)
      next candidate if constant.const_defined?(name, false)
      next candidate unless Object.const_defined?(name)

      # Go down the ancestors to check it it's owned
      # directly before we reach Object or the end of ancestors.
      constant = constant.ancestors.inject do |const, ancestor|
        break const    if ancestor == Object
        break ancestor if ancestor.const_defined?(name, false)
        const
      end

      # owner is in Object, so raise
      constant.const_get(name, false)
    end
  end
end

#decode(object) ⇒ Object

Given a string, returns a Ruby object.



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/queue_bus/util.rb', line 20

def decode(object)
  return unless object

  begin
    if MultiJson.respond_to?(:dump) && MultiJson.respond_to?(:load)
      MultiJson.load object
    else
      MultiJson.decode object
    end
  rescue ::MultiJson::DecodeError => e
    raise DecodeException, e.message, e.backtrace
  end
end

#encode(object) ⇒ Object

Given a Ruby object, returns a string suitable for storage in a queue.



11
12
13
14
15
16
17
# File 'lib/queue_bus/util.rb', line 11

def encode(object)
  if MultiJson.respond_to?(:dump) && MultiJson.respond_to?(:load)
    MultiJson.dump object
  else
    MultiJson.encode object
  end
end

#underscore(camel_cased_word) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/queue_bus/util.rb', line 34

def underscore(camel_cased_word)
  word = camel_cased_word.to_s.dup
  word.gsub!('::', '/')
  # word.gsub!(/(?:([A-Za-z\d])|^)(#{inflections.acronym_regex})(?=\b|[^a-z])/) { "#{$1}#{$1 && '_'}#{$2.downcase}" }
  word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
  word.tr!("-", "_")
  word.downcase!
  word
end