github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

pluginaweek / module_creation_helper

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 4
    • 1
  • Source
  • Commits
  • Network (1)
  • Downloads (5)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (5)
    • v0.2.0
    • v0.1.0
    • v0.0.4
    • v0.0.3
    • v0.0.2
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Adds a helper method for creating new modules and classes at runtime — Read more

  cancel

http://www.pluginaweek.org

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Add gemspec 
obrie (author)
Tue Jun 09 20:34:10 -0700 2009
commit  4f2e3c4203906ef720495cbcda0e8b70c14bc185
tree    e4aa82d276b7b98b01c9a70c322132044a7af29e
parent  16f3d458cd88f14ea868ead1feb01c5fc456e31c
module_creation_helper /
name age
history
message
file .gitignore Fri Jul 04 15:49:19 -0700 2008 Ignore test/app_root/script [obrie]
file CHANGELOG.rdoc Sun Dec 14 18:49:50 -0800 2008 Tag 0.2.0 release [obrie]
file LICENSE Tue Apr 14 20:06:37 -0700 2009 Update license [obrie]
file README.rdoc Sat Jul 05 10:10:27 -0700 2008 Reorganize core extensions into a separate modu... [obrie]
file Rakefile Tue Jun 09 20:34:10 -0700 2009 Add gemspec [obrie]
file init.rb Sun Dec 10 11:14:28 -0800 2006 Initial revision. [obrie]
directory lib/ Sun Jan 11 15:57:41 -0800 2009 Tweak docs [obrie]
file module_creation_helper.gemspec Tue Jun 09 20:34:10 -0700 2009 Add gemspec [obrie]
directory test/ Sun Jul 06 19:20:35 -0700 2008 Don't depend on active_support Add support for ... [obrie]
README.rdoc

module_creation_helper

module_creation_helper adds a helper method for creating new modules and classes at runtime.

Resources

API

  • api.pluginaweek.org/module_creation_helper

Bugs

  • pluginaweek.lighthouseapp.com/projects/13281-module_creation_helper

Development

  • github.com/pluginaweek/module_creation_helper

Source

  • git://github.com/pluginaweek/module_creation_helper.git

Description

Creating modules and classes at runtime isn’t the easiest and most intuitive process. Although often used for anonymous classes, there are many times where you will want to associate a runtime class with an actual name.

Traditionally, you would create new classes like so:

  c = Class.new               # => #<Class:0x480e388>
  Object.const_set('Foo', c)  # => Foo

Although this isn’t very hard, there are two problems: (1) It’s a repetitive process that should be DRYed. (2) Callbacks that are invoked while the class is being created do not know the name of the class.

To understand the second problem, consider the following:

  class Foo
    def self.inherited(base)
      puts "inherited class: #{base}, name: #{base.name}"
    end
  end

When a class inherits from Foo, Ruby will invoke the inherited callback. For example,

  c = Class.new(Foo)
  # inherited class: #<Class:0x47fb92c>, name:
  # => #<Class:0x47fb92c>

As you can see from output in this example, since the class has not yet been assigned to a constant, it is anonymous and does not yet have a name.

To address these issues, the functionality is encapsulated into a new method, Module#create. Since the method is defined in Module, it is also available to Class since Class inherits from Module.

Usage

Creating new classes/modules

Using the same example as before,

  c = Class.create('Bar', :superclass => Foo)
  # inherited class: Bar, name: Bar
  # => Bar

As you can see, the name of the class is now available during the inherited callback and is automatically assigned to the ‘Bar’ constant in Object.

Specifying the parent class/module

In addition to specifying the superclass, you can also specify the parent module/class like so:

  c = Class.create('Bar', :superclass => Foo, :parent => MyModule)
  # inherited class: MyModule::Bar, name: MyModule::Bar
  # => MyModule::Bar

Defining class/module methods

As you normally could when creating a new class, you can provide an additional block that defines the body of the class. For example,

  c = Class.create('Bar', :superclass => Foo, :parent => MyModule) do
    def say_hello
      'hello'
    end
  end
  # inherited class: MyModule::Bar, name: MyModule::Bar
  # => Bar
  Bar.new.say_hello
  # => "hello"

Dependencies

None.

Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server