Class: Rails::Plugin

Inherits:
Object show all
Includes:
Comparable
Defined in:
lib/rails/plugin.rb,
lib/rails/plugin/loader.rb,
lib/rails/plugin/locator.rb

Overview

The Plugin class should be an object which provides the following methods:

  • name - Used during initialisation to order the plugin (based on name and

    the contents of <tt>config.plugins</tt>).
    
  • valid? - Returns true if this plugin can be loaded.

  • load_paths - Each path within the returned array will be added to the $LOAD_PATH.

  • load - Finally ‘load’ the plugin.

These methods are expected by the Rails::Plugin::Locator and Rails::Plugin::Loader classes. The default implementation returns the lib directory as its load_paths, and evaluates init.rb when load is called.

You can also inspect the about.yml data programmatically:

plugin = Rails::Plugin.new(path_to_my_plugin)
plugin.about["author"] # => "James Adam"
plugin.about["url"] # => "http://interblah.net"

Direct Known Subclasses

GemPlugin

Defined Under Namespace

Classes: FileSystemLocator, GemLocator, Loader, Locator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory) ⇒ Plugin

Returns a new instance of Plugin.



24
25
26
27
28
# File 'lib/rails/plugin.rb', line 24

def initialize(directory)
  @directory = directory
  @name      = File.basename(@directory) rescue nil
  @loaded    = false
end

Instance Attribute Details

#directoryObject (readonly)

Returns the value of attribute directory.



22
23
24
# File 'lib/rails/plugin.rb', line 22

def directory
  @directory
end

#nameObject (readonly)

Returns the value of attribute name.



22
23
24
# File 'lib/rails/plugin.rb', line 22

def name
  @name
end

Instance Method Details

#<=>(other_plugin) ⇒ Object



56
57
58
# File 'lib/rails/plugin.rb', line 56

def <=>(other_plugin)
  name <=> other_plugin.name
end

#aboutObject



60
61
62
# File 'lib/rails/plugin.rb', line 60

def about
  @about ||= load_about_information
end

#controller_pathObject



83
84
85
# File 'lib/rails/plugin.rb', line 83

def controller_path
  File.join(directory, 'app', 'controllers')
end

#engine?Boolean

Engines are plugins with an app/ directory.

Returns:

  • (Boolean)


65
66
67
# File 'lib/rails/plugin.rb', line 65

def engine?
  has_app_directory?
end

#load(initializer) ⇒ Object

Evaluates a plugin’s init.rb file.



45
46
47
48
49
50
# File 'lib/rails/plugin.rb', line 45

def load(initializer)
  return if loaded?
  report_nonexistant_or_empty_plugin! unless valid?
  evaluate_init_rb(initializer)
  @loaded = true
end

#load_pathsObject

Returns a list of paths this plugin wishes to make available in $LOAD_PATH.



35
36
37
38
39
40
41
42
# File 'lib/rails/plugin.rb', line 35

def load_paths
  report_nonexistant_or_empty_plugin! unless valid?
  
  [].tap do |load_paths|
    load_paths << lib_path  if has_lib_directory?
    load_paths << app_paths if has_app_directory?
  end.flatten
end

#loaded?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/rails/plugin.rb', line 52

def loaded?
  @loaded
end

#locale_filesObject



99
100
101
# File 'lib/rails/plugin.rb', line 99

def locale_files
  Dir[ File.join(locale_path, '*.{rb,yml}') ]
end

#locale_pathObject



95
96
97
# File 'lib/rails/plugin.rb', line 95

def locale_path
  File.join(directory, 'config', 'locales')
end

#localized?Boolean

Returns true if there is any localization file in locale_path

Returns:

  • (Boolean)


75
76
77
# File 'lib/rails/plugin.rb', line 75

def localized?
  locale_files.any?
end

#metal_pathObject



87
88
89
# File 'lib/rails/plugin.rb', line 87

def metal_path
  File.join(directory, 'app', 'metal')
end

#routed?Boolean

Returns true if the engine ships with a routing file

Returns:

  • (Boolean)


70
71
72
# File 'lib/rails/plugin.rb', line 70

def routed?
  File.exist?(routing_file)
end

#routing_fileObject



91
92
93
# File 'lib/rails/plugin.rb', line 91

def routing_file
  File.join(directory, 'config', 'routes.rb')
end

#valid?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/rails/plugin.rb', line 30

def valid?
  File.directory?(directory) && (has_app_directory? || has_lib_directory? || has_init_file?)
end

#view_pathObject



79
80
81
# File 'lib/rails/plugin.rb', line 79

def view_path
  File.join(directory, 'app', 'views')
end