~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/railties/lib/rails_generator/spec.rb

  • Committer: Richard Lee (Canonical)
  • Date: 2010-10-15 15:17:58 UTC
  • mfrom: (190.1.3 use-case-mapper)
  • Revision ID: richard.lee@canonical.com-20101015151758-wcvmfxrexsongf9d
Merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
module Rails
2
 
  module Generator
3
 
    # A spec knows where a generator was found and how to instantiate it.
4
 
    # Metadata include the generator's name, its base path, and the source
5
 
    # which yielded it (PathSource, GemPathSource, etc.)
6
 
    class Spec
7
 
      attr_reader :name, :path, :source
8
 
 
9
 
      def initialize(name, path, source)
10
 
        @name, @path, @source = name, path, source
11
 
      end
12
 
 
13
 
      # Look up the generator class.  Require its class file, find the class
14
 
      # in ObjectSpace, tag it with this spec, and return.
15
 
      def klass
16
 
        unless @klass
17
 
          require class_file
18
 
          @klass = lookup_class
19
 
          @klass.spec = self
20
 
        end
21
 
        @klass
22
 
      end
23
 
 
24
 
      def class_file
25
 
        "#{path}/#{name}_generator.rb"
26
 
      end
27
 
 
28
 
      def class_name
29
 
        "#{name.camelize}Generator"
30
 
      end
31
 
 
32
 
      private
33
 
        # Search for the first Class descending from Rails::Generator::Base
34
 
        # whose name matches the requested class name.
35
 
        def lookup_class
36
 
          ObjectSpace.each_object(Class) do |obj|
37
 
            return obj if obj.ancestors.include?(Rails::Generator::Base) and
38
 
                          obj.name.split('::').last == class_name
39
 
          end
40
 
          raise NameError, "Missing #{class_name} class in #{class_file}"
41
 
        end
42
 
    end
43
 
  end
44
 
end