Sha256: d48d92849f663854065f9fc636b74e899302ec49d9413e608b2d755bd081c96d

Contents?: true

Size: 1.76 KB

Versions: 215

Compression:

Stored size: 1.76 KB

Contents

require 'active_support/core_ext/string/inflections'

#--
# Allows code reuse in the methods below without polluting Module.
#++
module QualifiedConstUtils
  def self.raise_if_absolute(path)
    raise NameError, "wrong constant name #$&" if path =~ /\A::[^:]+/
  end

  def self.names(path)
    path.split('::')
  end
end

##
# Extends the API for constants to be able to deal with qualified names. Arguments
# are assumed to be relative to the receiver.
#
#--
# Qualified names are required to be relative because we are extending existing
# methods that expect constant names, ie, relative paths of length 1. For example,
# Object.const_get("::String") raises NameError and so does qualified_const_get.
#++
class Module
  if method(:const_defined?).arity == 1
    def qualified_const_defined?(path)
      QualifiedConstUtils.raise_if_absolute(path)

      QualifiedConstUtils.names(path).inject(self) do |mod, name|
        return unless mod.const_defined?(name)
        mod.const_get(name)
      end
      return true
    end
  else
    def qualified_const_defined?(path, search_parents=true)
      QualifiedConstUtils.raise_if_absolute(path)

      QualifiedConstUtils.names(path).inject(self) do |mod, name|
        return unless mod.const_defined?(name, search_parents)
        mod.const_get(name)
      end
      return true
    end
  end

  def qualified_const_get(path)
    QualifiedConstUtils.raise_if_absolute(path)

    QualifiedConstUtils.names(path).inject(self) do |mod, name|
      mod.const_get(name)
    end
  end

  def qualified_const_set(path, value)
    QualifiedConstUtils.raise_if_absolute(path)

    const_name = path.demodulize
    mod_name = path.deconstantize
    mod = mod_name.empty? ? self : qualified_const_get(mod_name)
    mod.const_set(const_name, value)
  end
end

Version data entries

215 entries across 171 versions & 24 rubygems

Version Path
font-awesome-rails-3.1.1.2 vendor/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/core_ext/module/qualified_const.rb
font-awesome-rails-3.1.1.2 vendor/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/core_ext/module/qualified_const.rb
font-awesome-rails-3.1.1.1 vendor/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/core_ext/module/qualified_const.rb
font-awesome-rails-3.1.1.1 vendor/ruby/2.0.0/gems/activesupport-3.2.12/lib/active_support/core_ext/module/qualified_const.rb
font-awesome-rails-3.1.1.1 vendor/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/core_ext/module/qualified_const.rb
challah-1.0.0.beta3 vendor/bundle/gems/activesupport-3.2.13/lib/active_support/core_ext/module/qualified_const.rb
sidekiq-statsd-0.1.1 vendor/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/core_ext/module/qualified_const.rb
sidekiq-statsd-0.1.0 vendor/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/core_ext/module/qualified_const.rb
fc-webicons-0.0.4 vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/core_ext/module/qualified_const.rb
challah-1.0.0.beta2 vendor/bundle/gems/activesupport-3.2.13/lib/active_support/core_ext/module/qualified_const.rb
challah-1.0.0.beta vendor/bundle/gems/activesupport-3.2.13/lib/active_support/core_ext/module/qualified_const.rb
challah-1.0.0.beta vendor/bundle/gems/activesupport-3.2.11/lib/active_support/core_ext/module/qualified_const.rb
fc-webicons-0.0.3 vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/core_ext/module/qualified_const.rb
fc-webicons-0.0.2 vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/core_ext/module/qualified_const.rb
fc-webicons-0.0.1 vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/core_ext/module/qualified_const.rb
activesupport-3.2.13 lib/active_support/core_ext/module/qualified_const.rb
activesupport-3.2.13.rc2 lib/active_support/core_ext/module/qualified_const.rb
activesupport-3.2.13.rc1 lib/active_support/core_ext/module/qualified_const.rb
active_mailer-0.0.5 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/core_ext/module/qualified_const.rb
active_mailer-0.0.4 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/core_ext/module/qualified_const.rb