Module: Pallets::Util

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

Instance Method Summary collapse

Instance Method Details

#constantize(str) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pallets/util.rb', line 11

def constantize(str)
  names = str.split('::')

  # Raise the "usual" NameError
  Object.const_get(str) if names.empty?

  # Handle "::Const" cases
  names.shift if names.first.empty?

  names.inject(Object) do |constant, name|
    if constant.const_defined?(name, false)
      constant.const_get(name, false)
    else
      constant.const_missing(name)
    end
  end
end

#generate_id(str) ⇒ Object



5
6
7
8
9
# File 'lib/pallets/util.rb', line 5

def generate_id(str)
  initials = str.gsub(/[^A-Z]+([A-Z])/, '\1')[0,3]
  random = SecureRandom.hex(5)
  "#{initials}#{random}"
end