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



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

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!(%r{(?:_|(/))([a-z\d]*)}i) { "#{Regexp.last_match(1)}#{Regexp.last_match(2).capitalize}" }
  string.gsub!(%r{/}, '::')
  string
end

#classify(table_name) ⇒ Object



47
48
49
50
51
# File 'lib/queue_bus/util.rb', line 47

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



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

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.



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

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.



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

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



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

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