Module: RequireReloader::ActionPackInfectorMethods

Included in:
Helper
Defined in:
lib/require_reloader/helper.rb

Overview

Methods copied from latest ActiveSupport::Inflector to support older Rails versions (e.g. 3.0) without these methods.

Instance Method Summary collapse

Instance Method Details

#const_regexp(camel_cased_word) ⇒ Object

:nodoc:



20
21
22
23
24
25
26
27
# File 'lib/require_reloader/helper.rb', line 20

def const_regexp(camel_cased_word) #:nodoc:
  parts = camel_cased_word.split("::")
  last  = parts.pop

  parts.reverse.inject(last) do |acc, part|
    part.empty? ? acc : "#{part}(::#{acc})?"
  end
end

#constantize(camel_cased_word) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/require_reloader/helper.rb', line 9

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

  constant = Object
  names.each do |name|
    constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
  end
  constant
end

#deconstantize(path) ⇒ Object



5
6
7
# File 'lib/require_reloader/helper.rb', line 5

def deconstantize(path)
  path.to_s[0...(path.rindex('::') || 0)] # implementation based on the one in facets' Module#spacename
end

#demodulize(path) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/require_reloader/helper.rb', line 29

def demodulize(path)
  path = path.to_s
  if i = path.rindex('::')
    path[(i+2)..-1]
  else
    path
  end
end

#safe_constantize(camel_cased_word) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/require_reloader/helper.rb', line 38

def safe_constantize(camel_cased_word)
  begin
    constantize(camel_cased_word)
  rescue NameError => e
    raise unless e.message =~ /(uninitialized constant|wrong constant name) #{const_regexp(camel_cased_word)}$/ ||
    e.name.to_s == camel_cased_word.to_s
  rescue ArgumentError => e
    raise unless e.message =~ /not missing constant #{const_regexp(camel_cased_word)}\!$/
  end
end