Sha256: ea9e6c82e90c9f8f8a1c35f42190c1be5d13c3b9d365ae638bf79ea8fec712c2

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

require_relative 'consty/version'

class Consty
  
  class << self

    def get(name, namespace=Object)
      current_namespace = namespace
      while current_namespace do
        begin
          return secuential_get name, current_namespace
        rescue NameError
          namespace_name = current_namespace.name ? current_namespace.name.split('::')[0..-2].join('::') : ''
          if !namespace_name.empty?
            current_namespace = secuential_get namespace_name
          elsif current_namespace != Object
            current_namespace = Object
          else
            current_namespace = nil
          end
        end
      end
      namespace.const_missing name
    end

    private

    def secuential_get(name, namespace=Object)
      name_sections = name.to_s.split('::')
      
      if name_sections.first.empty?
        namespace = Object 
        name_sections = name_sections[1..-1]
      end
      
      name_sections.inject(namespace) do |scope, section|
        if scope.constants.include?(section.to_sym)
          scope.const_get section
        else
          scope.const_missing section
        end
      end

    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
consty-1.0.3 lib/consty.rb