Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/qmin/core_ext/string.rb
Instance Method Summary collapse
-
#constantize ⇒ Object
shamelessly stolen from resque.
- #to_queue_name ⇒ Object
- #underscore ⇒ Object
Instance Method Details
#constantize ⇒ Object
shamelessly stolen from resque
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/qmin/core_ext/string.rb', line 12 def constantize names = self.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) args = Module.method(:const_defined?).arity != 1 ? [false] : [] next candidate if constant.const_defined?(name, *args) 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, *args) const end # owner is in Object, so raise constant.const_get(name, false) end end end |
#to_queue_name ⇒ Object
39 40 41 |
# File 'lib/qmin/core_ext/string.rb', line 39 def to_queue_name gsub('::','').underscore end |
#underscore ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/qmin/core_ext/string.rb', line 3 def underscore self.to_s.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr('-', '_'). downcase end |