Module: Merb::Slices::ModuleMixin
- Defined in:
- lib/merb-slices/module_mixin.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#[](key) ⇒ Object
The configuration value.
- #[]=(key, value) ⇒ Object
-
#activate ⇒ Object
Stub activation hook - runs after AfterAppLoads BootLoader.
-
#app_components ⇒ Array[Symbol]
Return all application path component types.
-
#app_dir_for(type) ⇒ String
Retrieve the absolute path to a app-level directory.
-
#app_glob_for(type) ⇒ String
The pattern with which to match files within the type directory.
-
#app_path_for(type, *segments) ⇒ String
Construct an app-level path.
-
#app_paths ⇒ Hash
The app-level load paths to use when loading the slice.
-
#clone_slice! ⇒ Array[Array]
Clone all files from the slice to their app-level location; this will also copy /lib, causing merb-slices to pick up the slice there.
-
#collected_app_paths ⇒ Array[String]
The app-level load paths that have been used when the slice was loaded.
-
#collected_slice_paths ⇒ Array[String]
The slice-level load paths that have been used when the slice was loaded.
-
#config ⇒ Hash
The configuration for this slice.
-
#deactivate ⇒ Object
Stub deactivation method - not triggered automatically.
-
#dir_for(type) ⇒ String
Retrieve the absolute path to a slice-level directory.
-
#glob_for(type) ⇒ String
The pattern with which to match files within the type directory.
-
#init ⇒ Object
Stub initialization hook - runs before AfterAppLoads BootLoader.
-
#load_slice ⇒ Object
Load slice and it’s classes located in the slice-level load paths.
-
#loadable_files ⇒ Array
Return all *.rb files from valid component paths.
-
#loaded ⇒ Object
Stub classes loaded hook - runs before LoadClasses BootLoader right after a slice’s classes have been loaded internally.
-
#manifest(type = :root) ⇒ Array[Array]
Return all slice files mapped from the source to their relative path.
-
#mirror_all! ⇒ Array[Array]
Copies all files from mirrored_components to their app-level location.
-
#mirror_app! ⇒ Array[Array]
Copies all application files from mirrored_components to their app-level location.
-
#mirror_files_for(*types) ⇒ Array[Array]
Copy files from specified component path types to their app-level location.
-
#mirror_public! ⇒ Array[Array]
Copies all application files from mirrored_components to their app-level location.
-
#mirror_stubs! ⇒ Array[Array]
Copies all files from the (optional) stubs directory to their app-level location.
-
#mirrored_app_components ⇒ Array[Symbol]
Return all application path component types to mirror.
-
#mirrored_components ⇒ Array[Symbol]
Return all path component types to mirror.
-
#mirrored_public_components ⇒ Array[Symbol]
Return all public path component types to mirror.
-
#public_components ⇒ Array[Symbol]
Return all public path component types.
-
#public_dir_for(type) ⇒ String
Retrieve the relative path to a public directory.
-
#public_path_for(type, *segments) ⇒ String
Construct a path relative to the public directory.
-
#push_app_path(type, path, file_glob = "**/*.rb") ⇒ Object
This is the core mechanism for setting up your app-level layout.
-
#push_path(type, path, file_glob = "**/*.rb") ⇒ Object
This is the core mechanism for setting up your slice-level layout.
-
#registered ⇒ Object
Stub that gets triggered when a slice has been registered.
-
#remove_app_paths(*args) ⇒ Object
Removes given types of application components from app-level load path this slice uses for autoloading.
-
#remove_paths(*args) ⇒ Object
Removes given types of application components from slice-level load path this slice uses for autoloading.
-
#root_path(*path) ⇒ String
The full path including the root.
-
#routed? ⇒ Boolean
Check if there have been any routes setup.
-
#setup_default_structure! ⇒ Object
This sets up the default slice-level and app-level structure.
-
#setup_router(scope) ⇒ Object
Stub to setup routes inside the host application.
-
#slice_path_for(type, *segments) ⇒ String
Construct a slice-level path.
-
#slice_paths ⇒ Hash
The slice-level load paths to use when loading the slice.
-
#to_param ⇒ Object
Return a value suitable for routes/urls.
-
#unpack_slice! ⇒ Array[Array]
Unpack a subset of files from the slice to their app-level location; this will also copy /lib, causing merb-slices to pick up the slice there.
-
#url(name, rparams = {}, defaults = {}) ⇒ Object
Generate a url - takes the slice’s :path_prefix into account.
Class Method Details
.extended(slice_module) ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/merb-slices/module_mixin.rb', line 5 def self.extended(slice_module) slice_module..module_eval do attr_accessor :identifier, :identifier_sym, :root, :file attr_accessor :routes, :named_routes attr_accessor :description, :version, :author end end |
Instance Method Details
#[](key) ⇒ Object
Returns The configuration value.
46 47 48 |
# File 'lib/merb-slices/module_mixin.rb', line 46 def [](key) self.config[key] end |
#[]=(key, value) ⇒ Object
52 53 54 |
# File 'lib/merb-slices/module_mixin.rb', line 52 def []=(key, value) self.config[key] = value end |
#activate ⇒ Object
Stub activation hook - runs after AfterAppLoads BootLoader.
26 |
# File 'lib/merb-slices/module_mixin.rb', line 26 def activate; end |
#app_components ⇒ Array[Symbol]
Return all application path component types
269 270 271 |
# File 'lib/merb-slices/module_mixin.rb', line 269 def app_components [:view, :model, :controller, :helper, :mailer, :part] end |
#app_dir_for(type) ⇒ String
Retrieve the absolute path to a app-level directory.
164 |
# File 'lib/merb-slices/module_mixin.rb', line 164 def app_dir_for(type) self.app_paths[type].first end |
#app_glob_for(type) ⇒ String
Returns The pattern with which to match files within the type directory.
169 |
# File 'lib/merb-slices/module_mixin.rb', line 169 def app_glob_for(type) self.app_paths[type][1] end |
#app_path_for(type, *segments) ⇒ String
Construct an app-level path.
200 201 202 203 |
# File 'lib/merb-slices/module_mixin.rb', line 200 def app_path_for(type, *segments) prefix = type.is_a?(Symbol) ? self.app_dir_for(type) : self.app_dir_for(:root) / type File.join(prefix, *segments) end |
#app_paths ⇒ Hash
The app-level load paths to use when loading the slice.
136 137 138 |
# File 'lib/merb-slices/module_mixin.rb', line 136 def app_paths @app_paths ||= Hash.new { [Merb.root] } end |
#clone_slice! ⇒ Array[Array]
Clone all files from the slice to their app-level location; this will also copy /lib, causing merb-slices to pick up the slice there.
326 327 328 329 330 331 332 333 |
# File 'lib/merb-slices/module_mixin.rb', line 326 def clone_slice! app_slice_root = app_dir_for(:root) copied, duplicated = [], [] manifest.each do |source, relative_path| mirror_file(source, app_slice_root / relative_path, copied, duplicated) end [copied, duplicated] end |
#collected_app_paths ⇒ Array[String]
The app-level load paths that have been used when the slice was loaded.
This may be a subset of app_paths, which includes any path to look for.
93 94 95 |
# File 'lib/merb-slices/module_mixin.rb', line 93 def collected_app_paths @collected_app_paths ||= [] end |
#collected_slice_paths ⇒ Array[String]
The slice-level load paths that have been used when the slice was loaded.
This may be a subset of app_paths, which includes any path to look for.
84 85 86 |
# File 'lib/merb-slices/module_mixin.rb', line 84 def collected_slice_paths @collected_slice_paths ||= [] end |
#config ⇒ Hash
Returns The configuration for this slice.
57 58 59 |
# File 'lib/merb-slices/module_mixin.rb', line 57 def config Merb::Slices::config[self.identifier_sym] ||= {} end |
#deactivate ⇒ Object
Stub deactivation method - not triggered automatically.
29 |
# File 'lib/merb-slices/module_mixin.rb', line 29 def deactivate; end |
#dir_for(type) ⇒ String
Retrieve the absolute path to a slice-level directory.
152 |
# File 'lib/merb-slices/module_mixin.rb', line 152 def dir_for(type) self.slice_paths[type].first end |
#glob_for(type) ⇒ String
Returns The pattern with which to match files within the type directory.
157 |
# File 'lib/merb-slices/module_mixin.rb', line 157 def glob_for(type) self.slice_paths[type][1] end |
#init ⇒ Object
Stub initialization hook - runs before AfterAppLoads BootLoader.
23 |
# File 'lib/merb-slices/module_mixin.rb', line 23 def init; end |
#load_slice ⇒ Object
Load slice and it’s classes located in the slice-level load paths.
Assigns collected_slice_paths and collected_app_paths, then loads the collected_slice_paths and triggers the #loaded hook method.
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/merb-slices/module_mixin.rb', line 65 def load_slice # load application.rb (or similar) for thin slices Merb::Slices::Loader.load_file self.dir_for(:application) if File.file?(self.dir_for(:application)) # assign all relevant paths for slice-level and app-level self.collect_load_paths # load all slice-level classes from paths Merb::Slices::Loader.load_classes self.collected_slice_paths # call hook if available self.loaded if self.respond_to?(:loaded) Merb.logger.info!("Loaded slice '#{self}' ...") rescue => e Merb.logger.warn!("Failed loading #{self} (#{e.})") end |
#loadable_files ⇒ Array
Return all *.rb files from valid component paths
258 259 260 261 262 263 264 |
# File 'lib/merb-slices/module_mixin.rb', line 258 def loadable_files app_components.inject([]) do |paths, type| paths += Dir[dir_for(type) / '**/*.rb'] if slice_paths.key?(type) paths += Dir[app_dir_for(type) / '**/*.rb'] if app_paths.key?(type) paths end end |
#loaded ⇒ Object
Stub classes loaded hook - runs before LoadClasses BootLoader right after a slice’s classes have been loaded internally.
20 |
# File 'lib/merb-slices/module_mixin.rb', line 20 def loaded; end |
#manifest(type = :root) ⇒ Array[Array]
Return all slice files mapped from the source to their relative path
308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/merb-slices/module_mixin.rb', line 308 def manifest(type = :root) files = if type == :root Dir.glob(self.root / "**/*") elsif slice_paths.key?(type) glob = ((type == :view) ? view_templates_glob : glob_for(type) || "**/*") Dir.glob(dir_for(type) / glob) else [] end files.map { |source| [source, source.relative_path_from(root)] } end |
#mirror_all! ⇒ Array[Array]
Copies all files from mirrored_components to their app-level location
This includes application and public components.
360 361 362 |
# File 'lib/merb-slices/module_mixin.rb', line 360 def mirror_all! mirror_files_for mirrored_components + mirrored_public_components end |
#mirror_app! ⇒ Array[Array]
Copies all application files from mirrored_components to their app-level location
378 379 380 381 382 |
# File 'lib/merb-slices/module_mixin.rb', line 378 def mirror_app! components = mirrored_app_components components << :application if application_file? mirror_files_for components end |
#mirror_files_for(*types) ⇒ Array[Array]
Only explicitly defined component paths will be taken into account to avoid cluttering the app’s Merb.root by mistake - since undefined paths default to that.
Copy files from specified component path types to their app-level location
App-level overrides are preserved by creating duplicates before writing gem-level files. Because of their _override postfix they will load after their original implementation. In the case of views, this won’t work, but the user override is preserved nonetheless.
405 406 407 408 409 410 411 412 413 414 415 416 417 |
# File 'lib/merb-slices/module_mixin.rb', line 405 def mirror_files_for(*types) seen, copied, duplicated = [], [], [] # keep track of files we copied types.flatten.each do |type| if app_paths.key?(type) && (source_path = dir_for(type)) && (destination_path = app_dir_for(type)) manifest(type).each do |source, relative_path| # this relative path is not what we need here next if seen.include?(source) mirror_file(source, destination_path / source.relative_path_from(source_path), copied, duplicated) seen << source end end end [copied, duplicated] end |
#mirror_public! ⇒ Array[Array]
Copies all application files from mirrored_components to their app-level location
389 390 391 |
# File 'lib/merb-slices/module_mixin.rb', line 389 def mirror_public! mirror_files_for mirrored_public_components end |
#mirror_stubs! ⇒ Array[Array]
Copies all files from the (optional) stubs directory to their app-level location
369 370 371 |
# File 'lib/merb-slices/module_mixin.rb', line 369 def mirror_stubs! mirror_files_for :stub end |
#mirrored_app_components ⇒ Array[Symbol]
Return all application path component types to mirror
293 294 295 |
# File 'lib/merb-slices/module_mixin.rb', line 293 def mirrored_app_components mirrored_components & app_components end |
#mirrored_components ⇒ Array[Symbol]
Return all path component types to mirror
If config option :mirror is set return a subset, otherwise return all types.
285 286 287 288 |
# File 'lib/merb-slices/module_mixin.rb', line 285 def mirrored_components all = slice_paths.keys config[:mirror].is_a?(Array) ? config[:mirror] & all : all end |
#mirrored_public_components ⇒ Array[Symbol]
Return all public path component types to mirror
300 301 302 |
# File 'lib/merb-slices/module_mixin.rb', line 300 def mirrored_public_components mirrored_components & public_components end |
#public_components ⇒ Array[Symbol]
Return all public path component types
276 277 278 |
# File 'lib/merb-slices/module_mixin.rb', line 276 def public_components [:stylesheet, :javascript, :image] end |
#public_dir_for(type) ⇒ String
Retrieve the relative path to a public directory.
176 177 178 179 180 |
# File 'lib/merb-slices/module_mixin.rb', line 176 def public_dir_for(type) dir = type.is_a?(Symbol) ? self.app_dir_for(type) : self.app_dir_for(:public) / type dir = dir.relative_path_from(Merb.dir_for(:public)) rescue '.' dir == '.' ? '/' : "/#{dir}" end |
#public_path_for(type, *segments) ⇒ String
Construct a path relative to the public directory
189 190 191 |
# File 'lib/merb-slices/module_mixin.rb', line 189 def public_path_for(type, *segments) File.join(self.public_dir_for(type), *segments) end |
#push_app_path(type, path, file_glob = "**/*.rb") ⇒ Object
This is the core mechanism for setting up your app-level layout.
242 243 244 245 |
# File 'lib/merb-slices/module_mixin.rb', line 242 def push_app_path(type, path, file_glob = "**/*.rb") enforce!(type => Symbol) app_paths[type] = [path, file_glob] end |
#push_path(type, path, file_glob = "**/*.rb") ⇒ Object
This is the core mechanism for setting up your slice-level layout.
223 224 225 226 |
# File 'lib/merb-slices/module_mixin.rb', line 223 def push_path(type, path, file_glob = "**/*.rb") enforce!(type => Symbol) slice_paths[type] = [path, file_glob] end |
#registered ⇒ Object
This is rarely needed but still provided for edge cases.
Stub that gets triggered when a slice has been registered.
16 |
# File 'lib/merb-slices/module_mixin.rb', line 16 def registered; end |
#remove_app_paths(*args) ⇒ Object
Removes given types of application components from app-level load path this slice uses for autoloading.
251 252 253 |
# File 'lib/merb-slices/module_mixin.rb', line 251 def remove_app_paths(*args) args.each { |arg| self.app_paths.delete(arg) } end |
#remove_paths(*args) ⇒ Object
Removes given types of application components from slice-level load path this slice uses for autoloading.
232 233 234 |
# File 'lib/merb-slices/module_mixin.rb', line 232 def remove_paths(*args) args.each { |arg| self.slice_paths.delete(arg) } end |
#root_path(*path) ⇒ String
Returns The full path including the root.
145 |
# File 'lib/merb-slices/module_mixin.rb', line 145 def root_path(*path) File.join(self.root, *path) end |
#routed? ⇒ Boolean
Check if there have been any routes setup.
35 36 37 |
# File 'lib/merb-slices/module_mixin.rb', line 35 def routed? self.routes && !self.routes.empty? end |
#setup_default_structure! ⇒ Object
This sets up the default slice-level and app-level structure.
You can create your own structure by implementing setup_structure and using the push_path and push_app_paths. By default this setup matches what the merb-gen slice generator creates.
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 |
# File 'lib/merb-slices/module_mixin.rb', line 424 def setup_default_structure! self.push_app_path(:root, Merb.root / 'slices' / self.identifier, nil) self.push_path(:stub, root_path('stubs'), nil) self.push_app_path(:stub, app_dir_for(:root), nil) self.push_path(:application, root_path('app'), nil) self.push_app_path(:application, app_dir_for(:root) / 'app', nil) app_components.each do |component| self.push_path(component, dir_for(:application) / "#{component}s") self.push_app_path(component, app_dir_for(:application) / "#{component}s") end self.push_path(:public, root_path('public'), nil) self.push_app_path(:public, Merb.dir_for(:public) / 'slices' / self.identifier, nil) public_components.each do |component| self.push_path(component, dir_for(:public) / "#{component}s", nil) self.push_app_path(component, app_dir_for(:public) / "#{component}s", nil) end end |
#setup_router(scope) ⇒ Object
Stub to setup routes inside the host application.
32 |
# File 'lib/merb-slices/module_mixin.rb', line 32 def setup_router(scope); end |
#slice_path_for(type, *segments) ⇒ String
Construct a slice-level path.
212 213 214 215 |
# File 'lib/merb-slices/module_mixin.rb', line 212 def slice_path_for(type, *segments) prefix = type.is_a?(Symbol) ? self.dir_for(type) : self.dir_for(:root) / type File.join(prefix, *segments) end |
#slice_paths ⇒ Hash
The slice-level load paths to use when loading the slice.
129 130 131 |
# File 'lib/merb-slices/module_mixin.rb', line 129 def slice_paths @slice_paths ||= Hash.new { [self.root] } end |
#to_param ⇒ Object
Return a value suitable for routes/urls.
40 41 42 |
# File 'lib/merb-slices/module_mixin.rb', line 40 def to_param self.identifier end |
#unpack_slice! ⇒ Array[Array]
Files for the :stub component type are skipped.
Unpack a subset of files from the slice to their app-level location; this will also copy /lib, causing merb-slices to pick up the slice there.
343 344 345 346 347 348 349 350 351 |
# File 'lib/merb-slices/module_mixin.rb', line 343 def unpack_slice! app_slice_root = app_dir_for(:root) copied, duplicated = mirror_public! manifest.each do |source, relative_path| next unless unpack_file?(relative_path) mirror_file(source, app_slice_root / relative_path, copied, duplicated) end [copied, duplicated] end |
#url(name, rparams = {}, defaults = {}) ⇒ Object
Generate a url - takes the slice’s :path_prefix into account.
This is only relevant for default routes, as named routes are handled correctly without any special considerations.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/merb-slices/module_mixin.rb', line 109 def url(name, rparams = {}, defaults = {}) defaults = rparams if name.is_a?(Hash) && defaults.empty? rparams = name if name.is_a?(Hash) if name.is_a?(Symbol) raise "Named route not found: #{name}" unless self.named_routes[name] uri = Merb::Router.generate(name, rparams, defaults) else defaults[:controller] = defaults[:controller].gsub(%r|^#{self.identifier_sym}/|, '') if defaults[:controller] uri = Merb::Router.generate(name, rparams, defaults) uri = self[:path_prefix] / uri unless self[:path_prefix].blank? uri = "/#{uri}" unless uri[0,1] == '/' end Merb::Config[:path_prefix] ? Merb::Config[:path_prefix] + uri : uri end |