Module: Autocreate
- Defined in:
- lib/autocreate.rb
Overview
Extending a module with Autocreate allows you to specify autocreate rules for that module.
Autocreate allows you to automatically created classes or modules based on a given exemplar at the time they are referenced.
To use Autocreate, mix it via extend
into the class or module within which you want to autocreate referenced constants.
See the README for an example of how to use Autocreate.
Class Method Summary collapse
-
.extended(mod) ⇒ Object
:nodoc:.
Class Method Details
.extended(mod) ⇒ Object
:nodoc:
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/autocreate.rb', line 14 def self.extended( mod ) #:nodoc: old = mod.method( :const_missing ) mod..class_eval do # Specifies that the constant specified by key should be autocreated using the exemplar. If a block is given, the block is further used to initialize the block once it has been cloned. def autocreate( key, exemplar, &block ) ( @autocreate ||= [] ) << [ key, exemplar, block ] return self end define_method :const_missing do | cname | #:nodoc: # first, find an applicable exemplar key, exemplar, block = ( @autocreate ||= [] ).find do |k,v| case k when true then true when String, Symbol then k.to_s == cname.to_s when Array then k.find { |k| k.to_s == cname.to_s } else false end end # if we found it, set the const and return it if key object = exemplar.clone ( @reloadable ||= [] ) << cname; const_set( cname, object ) object.instance_eval( &block ) if block return object else old.call(cname) end end end end |