Finding Ruby constants, classes and modules and create new instances - factory style :)
Ruby
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
spec
.document
.gitignore
LICENSE
README.markdown
Rakefile
VERSION
foxy_factory.gemspec

README.markdown

Foxy Factory

Find Constants, including Modules and Classes registered in the Ruby kernel using convenient finder methods. Create new instances factory style. This is a way more advanced utility than 'constantize' and similar more "primitive" factory methods. Foxy Factory caches each successful lookup for faster future retrieval, instead of having to go through the kernel. If a constant is not found in the cache, a kernel lookup will always be performed as a fall-back.

Install

$ gem install foxy_factory

Usage

require 'foxy_factory'

Configuration

Imagine this namespace structure:

    
  class Basic
    def initialize(number, say = "Hello", &block)
      @number = number
      @say = say
      if block
        block.arity < 1 ? obj.instance_eval(&block) : block.call(obj)
      end        
    end
  end

  module Howrah  
    class MyClass < Basic
      def initialize(number, &block)
        super
      end
    end

    module MyModule
      class MyNestedClass < Basic
        def initialize(number, &block)
          super
        end
      end

      module MyNestedModule
        class MyDoubleNestedClass
        end
      end
    end
  end  

Create factory with base namespace 'Howrah' that this factory will use as the root (or base) namespace

factory = Foxy::Factory.new :howrah

You can at any time change the implicit root for a given factory


  factory = Foxy::Factory.new
  ...
  factory.base_namespace = :my_root

Find constant

Find constant Howrah::MyClass

factory.find_constant :my_class

=> Howrah::MyClass

Find constant Howrah::NonExistingClass - should raise error

factory.find_constant :non_existing_class

=> error: ConstantNotFoundError

Find constant Howrah::MyModule

factory.find_constant :my_module
  
=> Howrah::MyModule

Find constant Howrah::MyModule::MyNestedClass

factory.find_constant :my_module, :my_nested_class
  
=> Howrah::MyModule::MyNestedClass

Find constant Howrah::MyModule::MyNestedModule::MyDoubleNestedClass

factory.find_constant :my_module, :my_nested_module, :my_double_nested_class
  
=> Howrah::MyModule::MyNestedModule::MyDoubleNestedClass

Find module

Find module Howrah::MyModule

factory.find_module :my_module
=> Howrah::MyModule  

Find module Howrah::MyModule::MyNestedModule

factory.find_module :my_module, :my_nested_module

=> Howrah::MyModule::MyNestedModule

Find module Howrah::MyModule::MyNestedModule::MyNestedClass but not class!

factory.find_module! :my_module, :my_nested_class
  
=> error: ConstantIsNotModuleError

Find Class

Find class Howrah::MyModule

factory.find_class :my_module

=> error: ConstantIsNotClassError

Find class Howrah::MyModule::MyNestedModule::MyDoubleNestedClass

factory.find_class :my_module, :my_nested_module, :my_nested_class
  
=> Howrah::MyModule::MyNestedModule::MyDoubleNestedClass

Create instance

  factory.create :my_module, :my_nested_class, :args => [2, 'Hello'] do |p|
    puts p.number * p.say
  end 
  
  => Hello Hello

  factory.create :my_class, :args => 2 do |p|
    puts p.number * "Hello "
  end 

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

Copyright

Copyright (c) 2010 Kristian Mandrup. See LICENSE for details.