Sha256: a7bd424a1c63fa0289959fce01fe5c3581950bfed8d50590b0e799e1612b2e5b

Contents?: true

Size: 935 Bytes

Versions: 10

Compression:

Stored size: 935 Bytes

Contents

module Lucid
  module ObjectFactory #:nodoc:
    def create_object_of(phrase)
      try = 0
      begin
        try += 1
        names = phrase.split('::')
        names.shift if names.empty? || names.first.empty?

        constant = ::Object
        names.each do |name|
          constant = provide_object_name(constant, name)
        end
        constant
      rescue NameError => e
        require underscore(phrase)
        if try < 2
          retry
        else
          raise e
        end
      end
    end

    def underscore(phrase)
      phrase.to_s.gsub(/::/, '/').
        gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
        gsub(/([a-z\d])([A-Z])/,'\1_\2').
        tr("-", "_").
        downcase
    end

    private

    def provide_object_name(constant, name)
      if constant.const_defined?(name, false)
        constant.const_get(name, false)
      else
        constant.const_missing(name)
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
lucid-0.3.0 lib/lucid/factory.rb
lucid-0.2.1 lib/lucid/factory.rb
lucid-0.2.0 lib/lucid/factory.rb
lucid-0.1.1 lib/lucid/factory.rb
lucid-0.1.0 lib/lucid/factory.rb
lucid-0.0.9 lib/lucid/factory.rb
lucid-0.0.8 lib/lucid/factory.rb
lucid-0.0.7 lib/lucid/factory.rb
lucid-0.0.6 lib/lucid/factory.rb
lucid-0.0.5 lib/lucid/factory.rb