~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/railties/lib/rails/plugin.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
 
  # The Plugin class should be an object which provides the following methods:
3
 
  #
4
 
  # * +name+       - Used during initialisation to order the plugin (based on name and
5
 
  #                  the contents of <tt>config.plugins</tt>).
6
 
  # * +valid?+     - Returns true if this plugin can be loaded.
7
 
  # * +load_paths+ - Each path within the returned array will be added to the <tt>$LOAD_PATH</tt>.
8
 
  # * +load+       - Finally 'load' the plugin.
9
 
  #
10
 
  # These methods are expected by the Rails::Plugin::Locator and Rails::Plugin::Loader classes.
11
 
  # The default implementation returns the <tt>lib</tt> directory as its <tt>load_paths</tt>, 
12
 
  # and evaluates <tt>init.rb</tt> when <tt>load</tt> is called.
13
 
  #
14
 
  # You can also inspect the about.yml data programmatically:
15
 
  #
16
 
  #   plugin = Rails::Plugin.new(path_to_my_plugin)
17
 
  #   plugin.about["author"] # => "James Adam"
18
 
  #   plugin.about["url"] # => "http://interblah.net"
19
 
  class Plugin
20
 
    include Comparable
21
 
    
22
 
    attr_reader :directory, :name
23
 
    
24
 
    def initialize(directory)
25
 
      @directory = directory
26
 
      @name      = File.basename(@directory) rescue nil
27
 
      @loaded    = false
28
 
    end
29
 
    
30
 
    def valid?
31
 
      File.directory?(directory) && (has_app_directory? || has_lib_directory? || has_init_file?)
32
 
    end
33
 
  
34
 
    # Returns a list of paths this plugin wishes to make available in <tt>$LOAD_PATH</tt>.
35
 
    def load_paths
36
 
      report_nonexistant_or_empty_plugin! unless valid?
37
 
      
38
 
      returning [] do |load_paths|
39
 
        load_paths << lib_path  if has_lib_directory?
40
 
        load_paths << app_paths if has_app_directory?
41
 
      end.flatten
42
 
    end
43
 
    
44
 
    # Evaluates a plugin's init.rb file.
45
 
    def load(initializer)
46
 
      return if loaded?
47
 
      report_nonexistant_or_empty_plugin! unless valid?
48
 
      evaluate_init_rb(initializer)
49
 
      @loaded = true
50
 
    end
51
 
    
52
 
    def loaded?
53
 
      @loaded
54
 
    end
55
 
    
56
 
    def <=>(other_plugin)
57
 
      name <=> other_plugin.name
58
 
    end
59
 
 
60
 
    def about
61
 
      @about ||= load_about_information
62
 
    end
63
 
 
64
 
    # Engines are plugins with an app/ directory.
65
 
    def engine?
66
 
      has_app_directory?
67
 
    end
68
 
    
69
 
    # Returns true if the engine ships with a routing file
70
 
    def routed?
71
 
      File.exist?(routing_file)
72
 
    end
73
 
 
74
 
    # Returns true if there is any localization file in locale_path
75
 
    def localized?
76
 
      locale_files.any?
77
 
    end
78
 
 
79
 
    def view_path
80
 
      File.join(directory, 'app', 'views')
81
 
    end
82
 
 
83
 
    def controller_path
84
 
      File.join(directory, 'app', 'controllers')
85
 
    end
86
 
 
87
 
    def metal_path
88
 
      File.join(directory, 'app', 'metal')
89
 
    end
90
 
 
91
 
    def routing_file
92
 
      File.join(directory, 'config', 'routes.rb')
93
 
    end
94
 
 
95
 
    def locale_path
96
 
      File.join(directory, 'config', 'locales')
97
 
    end
98
 
 
99
 
    def locale_files
100
 
      Dir[ File.join(locale_path, '*.{rb,yml}') ]
101
 
    end
102
 
    
103
 
 
104
 
    private
105
 
      def load_about_information
106
 
        about_yml_path = File.join(@directory, "about.yml")
107
 
        parsed_yml = File.exist?(about_yml_path) ? YAML.load(File.read(about_yml_path)) : {}
108
 
        parsed_yml || {}
109
 
      rescue Exception
110
 
        {}
111
 
      end
112
 
 
113
 
      def report_nonexistant_or_empty_plugin!
114
 
        raise LoadError, "Can not find the plugin named: #{name}"
115
 
      end
116
 
 
117
 
      
118
 
      def app_paths
119
 
        [ File.join(directory, 'app', 'models'), File.join(directory, 'app', 'helpers'), controller_path, metal_path ]
120
 
      end
121
 
      
122
 
      def lib_path
123
 
        File.join(directory, 'lib')
124
 
      end
125
 
 
126
 
      def classic_init_path
127
 
        File.join(directory, 'init.rb')
128
 
      end
129
 
 
130
 
      def gem_init_path
131
 
        File.join(directory, 'rails', 'init.rb')
132
 
      end
133
 
 
134
 
      def init_path
135
 
        File.file?(gem_init_path) ? gem_init_path : classic_init_path
136
 
      end
137
 
 
138
 
 
139
 
      def has_app_directory?
140
 
        File.directory?(File.join(directory, 'app'))
141
 
      end
142
 
 
143
 
      def has_lib_directory?
144
 
        File.directory?(lib_path)
145
 
      end
146
 
 
147
 
      def has_init_file?
148
 
        File.file?(init_path)
149
 
      end
150
 
 
151
 
 
152
 
      def evaluate_init_rb(initializer)
153
 
        if has_init_file?
154
 
          silence_warnings do
155
 
            # Allow plugins to reference the current configuration object
156
 
            config = initializer.configuration
157
 
            
158
 
            eval(IO.read(init_path), binding, init_path)
159
 
          end
160
 
        end
161
 
      end               
162
 
  end
163
 
 
164
 
  # This Plugin subclass represents a Gem plugin. Although RubyGems has already
165
 
  # taken care of $LOAD_PATHs, it exposes its load_paths to add them
166
 
  # to Dependencies.load_paths.
167
 
  class GemPlugin < Plugin
168
 
    # Initialize this plugin from a Gem::Specification.
169
 
    def initialize(spec, gem)
170
 
      directory = spec.full_gem_path
171
 
      super(directory)
172
 
      @name = spec.name
173
 
    end
174
 
 
175
 
    def init_path
176
 
      File.join(directory, 'rails', 'init.rb')
177
 
    end
178
 
  end
179
 
end