Sha256: ca8c71967a7bd472df01464a12c8770215fb03c5a30923dc54a9dab46aa53a03

Contents?: true

Size: 812 Bytes

Versions: 2

Compression:

Stored size: 812 Bytes

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.split('::')[0..-2].join('::')
         current_namespace = namespace_name.empty? ? nil : secuential_get(namespace_name)
        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) { |c,n| c.const_get n }
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
consty-1.0.0 lib/consty.rb
consty-0.1.0 lib/consty.rb