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
30
# File 'lib/trusty_cms/extension_path.rb', line 26

def initialize(options = {}) #:nodoc
  @name = options[:name].underscore
  @path = 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.



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

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.



169
170
171
172
# File 'lib/trusty_cms/extension_path.rb', line 169

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.



176
177
178
# File 'lib/trusty_cms/extension_path.rb', line 176

def enabled_paths
  enabled.map(&:path)
end

.find(name) ⇒ Object

Returns the ExtensionPath object for the given extension name.

Raises:

  • (LoadError)


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

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.



159
160
161
# File 'lib/trusty_cms/extension_path.rb', line 159

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 trusty-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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

def locale_paths
  if check_subdirectory('config/locales')
    Dir[File.join(path.to_s, '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.



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

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.



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

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.



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

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.



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

def rake_task_paths
  if check_subdirectory('lib/tasks')
    Dir[File.join(path.to_s, 'lib/tasks/**', '*.rake')]
  end
end

#requiredObject



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

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

#to_sObject



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

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.



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

def view_paths
  check_subdirectory('app/views')
end