Module: AutoCode

Defined in:
lib/autocode.rb

Class Method Summary collapse

Class Method Details

.camel_case(cname) ⇒ Object



14
15
16
17
18
# File 'lib/autocode.rb', line 14

def AutoCode.camel_case( cname )  
  cname.to_s.split('_').map do |word|
    "#{word.slice(/^\w/).upcase}#{word.slice(/^\w(\w+)/, 1)}"
  end.join
end

.extended(mod) ⇒ Object



24
# File 'lib/autocode.rb', line 24

def self.extended( mod ) ; included( mod ) ; end

.included(mod) ⇒ Object



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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/autocode.rb', line 26

def self.included( mod )
  
  mod.instance_eval do
    
    AutoCode.monitor.enter
    begin
      # First make sure we haven't already done this once
      return unless @autocode.nil?
    
      # Initialize bookkeeping variables needed by AutoCode
      @autocode = { 
        :constructors => Hash.new { |h,k| h[k] = [] },
        :initializers => Hash.new { |h,k| h[k] = [] },
        :loaded => []
      }
    ensure
      AutoCode.monitor.exit
    end
    
    def auto_constructor( key = true, &block)
      @autocode[:constructors][ AutoCode.normalize( key ) ] << block
    end
    
    def auto_create( key = true, options = {}, &block )
      auto_constructor( key ) do | cname |
        exemplar = options[:exemplar] || Module.new
        new_constant = exemplar.clone
        new_constant.send(:include, AutoCode)
        ret = const_set( cname, new_constant )
        new_constant.module_eval( &block ) if block
        ret
      end
    end
    
    # Convenience method for auto_creating classes.
    def auto_create_class( key = true, parent = Object, &block )
      auto_constructor( key ) do | cname |
        parent = const_get(parent) unless parent.is_a? Class
        new_constant = Class.new( parent )
        new_constant.send(:include, AutoCode)
        ret = const_set( cname, new_constant )
        new_constant.module_eval( &block ) if block
        ret
      end
    end

    # Convenience method for auto_creating modules.
    def auto_create_module( key = true, &block )
      auto_constructor( key ) do | cname |
        new_constant = Module.new
        new_constant.send(:include, AutoCode)
        ret = const_set( cname, new_constant )
        new_constant.module_eval( &block ) if block
        ret
      end
    end

    # Adds an auto_load block for the given key and directories
    def auto_load( key = true, options = {} )
      @autocode[:constructors][ AutoCode.normalize( key ) ] << lambda do | cname |
        filename = AutoCode.snake_case( cname ) << '.rb'
        options[:directories] ||= '.'
        path = if target = options[:target]
          target.call(cname)
        else
          options[:directories].
          map { |dir| File.join( dir.to_s, filename ) }.
          find { |path| File.exist?( path ) }
        end
        Kernel.load( path ) unless path.nil?
      end
    end

    # Adds an arbitrary initializer block for the given key
    def auto_eval( key, &block )
      if key.is_a?( Symbol) && const_defined?( AutoCode.normalize( key ) )
        const_get( key ).module_eval( &block)
      else
        @autocode[:initializers][ AutoCode.normalize( key ) ] << lambda do | mod |
          mod.module_eval( &block )
        end
      end
    end
    
    def auto_const?(cname)
      true unless @autocode[:constructors][ AutoCode.normalize( cname ) ].empty?
    end

    # Returns the list of constants that would be reloaded upon a call to reload.
    def reloadable ; @autocode[:loaded] ; end

    # Reloads (via #remove_const) all the constants that were loaded via auto_code.
    def reload
      Autocode.monitor.synchronize do
        @autocode[:loaded].each { |name| remove_const( name ) } ; @autocode[:loaded] = []
      end
    end

    private

    old = method( :const_missing )
    (class << self ; self ; end ).instance_eval do
      define_method( :const_missing ) do | cname |

        Autocode.monitor.synchronize do
          # check to see if some other thread loaded the constant before
          # we entered the lock.
          return const_get( cname ) if const_defined?( cname )
          actions = []
          @autocode[:constructors].each do |matcher,action|
            actions.concat action if (matcher == true || matcher == cname || matcher === cname.to_s )
          end
          actions.reverse!.find { | c | c.call( cname ) and const_defined?( cname ) }
          return old.call( cname ) unless const_defined?( cname )          
          initializers = @autocode[:initializers][true] + @autocode[:initializers][cname]
          mod = const_get( cname ); initializers.each { |init| init.call( mod ) }
          @autocode[:loaded] << cname
          mod
        end
      end
    end
  end
end

.monitorObject



3
4
5
6
# File 'lib/autocode.rb', line 3

def self.monitor
  require 'monitor'
  @monitor ||= Monitor.new
end

.normalize(cname) ⇒ Object

always make sure we have a camel-cased symbol



9
10
11
12
# File 'lib/autocode.rb', line 9

def AutoCode.normalize( cname )
  return cname  unless cname.is_a? Symbol or cname.is_a? String
  camel_case( cname ).intern
end

.snake_case(cname) ⇒ Object



20
21
22
# File 'lib/autocode.rb', line 20

def AutoCode.snake_case( cname )
  cname.to_s.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').gsub(/([a-z\d])([A-Z])/,'\1_\2').downcase
end