Sha256: ff9f24e21c96a98c049453add4d1048e37d4b9c864ed33c35f7b11f23a72f609
Contents?: true
Size: 1.12 KB
Versions: 3
Compression:
Stored size: 1.12 KB
Contents
# Be sure to restart your server when you modify this file. # Add new inflection rules using the following format # (all these examples are active by default): ActiveSupport::Inflector.inflections do |inflect| end # # From Rails 3.2 # module ActiveSupport::Inflector 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 # Mount a regular expression that will match part by part of the constant. # For instance, Foo::Bar::Baz will generate Foo(::Bar(::Baz)?)? 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 end class String def safe_constantize ActiveSupport::Inflector.safe_constantize(self) end end
Version data entries
3 entries across 3 versions & 1 rubygems