Class: TrustyCms::ExtensionPath

Inherits:
Object
  • Object
show all
Defined in:
lib/trusty_cms/extension_path.rb

Constant Summary collapse

@@known_paths =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ExtensionPath

:nodoc



26
27
28
29
# File 'lib/trusty_cms/extension_path.rb', line 26

def initialize(options = {}) #:nodoc
  @name, @path = options[:name].underscore, options[:path]
  @@known_paths[@name.to_sym] = self
end

Instance Attribute Details

#nameObject

This class holds information about extensions that may be loaded. It has two roles: to remember the location of the extension so that we don’t have to search for it again, and to look within that path for significant application subdirectories.

We can’t just retrieve this information from the Extension class because the initializer sets up most of the application load_paths before plugins (including extensions) are loaded. You can think of this as a sort of pre-extension class preparing the way for extension loading.

You can use instances of this class to retrieve information about a particular extension:

ExtensionPath.new(:name, :path)
ExtensionPath.find(:name)               #=> ExtensionPath instance
ExtensionPath.find(:name).plugin_paths  #=> "path/vendor/plugins" if it exists and is a directory
ExtensionPath.for(:name)                #=> "path"

The initializer calls class methods to get overall lists (in configured order) of enabled load paths:

ExtensionPath.enabled                   #=> ["path", "path", "path", "path"]
ExtensionPath.plugin_paths              #=> ["path/vendor/plugins", "path/vendor/plugins"]


23
24
25
# File 'lib/trusty_cms/extension_path.rb', line 23

def name
  @name
end

#pathObject

This class holds information about extensions that may be loaded. It has two roles: to remember the location of the extension so that we don’t have to search for it again, and to look within that path for significant application subdirectories.

We can’t just retrieve this information from the Extension class because the initializer sets up most of the application load_paths before plugins (including extensions) are loaded. You can think of this as a sort of pre-extension class preparing the way for extension loading.

You can use instances of this class to retrieve information about a particular extension:

ExtensionPath.new(:name, :path)
ExtensionPath.find(:name)               #=> ExtensionPath instance
ExtensionPath.find(:name).plugin_paths  #=> "path/vendor/plugins" if it exists and is a directory
ExtensionPath.for(:name)                #=> "path"

The initializer calls class methods to get overall lists (in configured order) of enabled load paths:

ExtensionPath.enabled                   #=> ["path", "path", "path", "path"]
ExtensionPath.plugin_paths              #=> ["path/vendor/plugins", "path/vendor/plugins"]


23
24
25
# File 'lib/trusty_cms/extension_path.rb', line 23

def path
  @path
end

Class Method Details

.clear_paths!Object

Forgets all recorded extension paths. Currently only used in testing.



55
56
57
# File 'lib/trusty_cms/extension_path.rb', line 55

def self.clear_paths!
  @@known_paths = {}
end

.enabledObject

Returns a list of path objects for all the enabled extensions in the configured order. If a configured extension has not been found during initialization, a LoadError will be thrown here.

Note that at this stage, in line with the usage of config.extensions = [], the extension names are being passed around as symbols.



167
168
169
170
# File 'lib/trusty_cms/extension_path.rb', line 167

def enabled
  enabled_extensions = TrustyCms::Application.config.enabled_extensions
  @@known_paths.values_at(*enabled_extensions).compact
end

.enabled_pathsObject

Returns a list of the root paths to all the enabled extensions, in the configured order.



174
175
176
# File 'lib/trusty_cms/extension_path.rb', line 174

def enabled_paths
  enabled.map(&:path)
end

.find(name) ⇒ Object

Returns the ExtensionPath object for the given extension name.

Raises:

  • (LoadError)


150
151
152
153
# File 'lib/trusty_cms/extension_path.rb', line 150

def find(name)
  raise LoadError, "Cannot return path for unknown extension: #{name}" unless @@known_paths[name.to_sym]
  @@known_paths[name.to_sym]
end

.for(name) ⇒ Object

Returns the root path recorded for the given extension name.



157
158
159
# File 'lib/trusty_cms/extension_path.rb', line 157

def for(name)
  find(name).path
end

.from_path(path, name = nil) ⇒ Object

Builds a new ExtensionPath object from the supplied path, working out the name of the extension by stripping the extra bits from radiant-something-extension-1.0.0 to leave just ‘something’. The object is returned, and also remembered here for later use by the initializer (to find load paths) and the ExtensionLoader, to load and activate the extension.

If two arguments are given, the second is taken to be the full extension name.



46
47
48
49
50
# File 'lib/trusty_cms/extension_path.rb', line 46

def self.from_path(path, name=nil)
  name = path if name.blank?
  name = File.basename(name).gsub(/^trusty-|-extension(-[\d\.a-z]+|-[a-z\d]+)*$/, '')
  new(:name => name, :path => path)
end

Instance Method Details

#controller_pathsObject

Returns the app/controllers path if it is found within this extension root. Call the class method ExtensionPath.controller_paths to get a list of the controller paths found in all enabled extensions.



110
111
112
# File 'lib/trusty_cms/extension_path.rb', line 110

def controller_paths
  check_subdirectory("app/controllers")
end

#eager_load_pathsObject

Returns a list of extension subdirectories that should be marked for eager loading. At the moment that includes all the controller, model and helper paths. The main purpose here is to ensure that extension controllers are loaded before running cucumber features, and there may be a better way to achieve that.

Call the class method ExtensionPath.eager_load_paths to get a list for all enabled extensions.



143
144
145
# File 'lib/trusty_cms/extension_path.rb', line 143

def eager_load_paths
  [controller_paths, model_paths, helper_paths].flatten.compact
end

#helper_pathsObject

Returns the app/helpers path if it is found within this extension root. Call the class method ExtensionPath.helper_paths to get a list of the helper paths found in all enabled extensions.



96
97
98
# File 'lib/trusty_cms/extension_path.rb', line 96

def helper_paths
  check_subdirectory("app/helpers")
end

#load_pathsObject

Returns a list of all the likely load paths found within this extension root. It includes all of these that exist and are directories:

  • path

  • path/lib

  • path/app/models

  • path/app/controllers

  • path/app/metal

  • path/app/helpers

  • path/test/helpers

You can call the class method ExtensionPath.load_paths to get a flattened list of all the load paths in all the enabled extensions.



72
73
74
# File 'lib/trusty_cms/extension_path.rb', line 72

def load_paths
  %w(lib app/models app/controllers app/metal app/helpers test/helpers).collect { |d| check_subdirectory(d) }.push(path).flatten.compact
end

#locale_pathsObject

Returns a list of names of all the locale files found within this extension root. Call the class method ExtensionPath.locale_paths to get a list of the locale files found in all enabled extensions in reverse order so that locale definitions override one another correctly.



87
88
89
90
91
# File 'lib/trusty_cms/extension_path.rb', line 87

def locale_paths
  if check_subdirectory("config/locales")
    Dir[File.join("#{path}","config/locales","*.{rb,yml}")]
  end
end

#metal_pathsObject

Returns the app/metal path if it is found within this extension root. Call the class method ExtensionPath.metal_paths to get a list of the metal paths found in all enabled extensions.



125
126
127
# File 'lib/trusty_cms/extension_path.rb', line 125

def metal_paths
  check_subdirectory("app/metal")
end

#model_pathsObject

Returns the app/models path if it is found within this extension root. Call the class method ExtensionPath.model_paths to get a list of the model paths found in all enabled extensions.



103
104
105
# File 'lib/trusty_cms/extension_path.rb', line 103

def model_paths
  check_subdirectory("app/models")
end

#plugin_pathsObject

Returns a list of all the vendor/plugin paths found within this extension root. Call the class method ExtensionPath.plugin_paths to get a list of the plugin paths found in all enabled extensions.



79
80
81
# File 'lib/trusty_cms/extension_path.rb', line 79

def plugin_paths
  check_subdirectory("vendor/plugins")
end

#rake_task_pathsObject

Returns a list of all the rake task files found within this extension root.



131
132
133
134
135
# File 'lib/trusty_cms/extension_path.rb', line 131

def rake_task_paths
  if check_subdirectory("lib/tasks")
    Dir[File.join("#{path}","lib/tasks/**","*.rake")]
  end
end

#requiredObject



31
32
33
# File 'lib/trusty_cms/extension_path.rb', line 31

def required
  File.join(path, "#{name}_extension")
end

#to_sObject



35
36
37
# File 'lib/trusty_cms/extension_path.rb', line 35

def to_s
  path
end

#view_pathsObject

Returns the app/views path if it is found within this extension root. Call the class method ExtensionPath.view_paths to get a list of the view paths found in all enabled extensions in reverse order so that views override one another correctly.



118
119
120
# File 'lib/trusty_cms/extension_path.rb', line 118

def view_paths
  check_subdirectory("app/views")
end