Class: Torba::Package
- Inherits:
-
Object
- Object
- Torba::Package
- Defined in:
- lib/torba/package.rb
Overview
Represents remote source with explicit paths/files to be imported (i.e. copied from an archive, git repository etc). Stylesheets (if any) are treated specially because static “url(…)” definitions should be replaced with Sprockets-aware “asset_path” helpers.
Instance Attribute Summary collapse
-
#import_paths ⇒ Array<String>
readonly
List of file paths to import (relative to remote source root).
-
#name ⇒ String
readonly
Short package name, acts as as namespace within Sprockets’ load path.
-
#remote_source ⇒ Object
readonly
Instance that implements RemoteSources::Common.
Instance Method Summary collapse
-
#build ⇒ void
Cache remote source and import specified assets to #load_path.
- #import_list ⇒ ImportList
-
#initialize(name, remote_source, options = {}) ⇒ Package
constructor
A new instance of Package.
-
#load_path ⇒ String
Path where processed files of the package reside.
-
#logical_paths ⇒ Array<String>
logical paths that the package contains.
-
#non_js_css_logical_paths ⇒ Array<String>
logical paths that the package contains except JS ans CSS.
-
#remove ⇒ void
Remove self from filesystem.
-
#verify ⇒ false
If package is not build.
Constructor Details
#initialize(name, remote_source, options = {}) ⇒ Package
Returns a new instance of Package.
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/torba/package.rb', line 47 def initialize(name, remote_source, = {}) @name = name @remote_source = remote_source @import_paths = ([:import] || ["**/*"]).sort.map do |path| if path.end_with?("/") File.join(path, "**/*") else path end end end |
Instance Attribute Details
#import_paths ⇒ Array<String> (readonly)
Put in this list only files that are absolutely necessary to you as Manifest#non_js_css_logical_paths depends on it.
Returns list of file paths to import (relative to remote source root).
41 42 43 |
# File 'lib/torba/package.rb', line 41 def import_paths @import_paths end |
#name ⇒ String (readonly)
Returns short package name, acts as as namespace within Sprockets’ load path. Doesn’t need to be equal to remote package name.
26 27 28 |
# File 'lib/torba/package.rb', line 26 def name @name end |
#remote_source ⇒ Object (readonly)
Returns instance that implements RemoteSources::Common.
29 30 31 |
# File 'lib/torba/package.rb', line 29 def remote_source @remote_source end |
Instance Method Details
#build ⇒ void
Directories explicitly specified in #import_paths are not preserved after importing, i.e. resulted file tree becomes flatten. This way you can omit build specific directories when requiring assets in your project. If you want to preserve remote source file tree, use glob patterns without mentioning subdirectories in them.
In addition #name is used as a namespace folder within #load_path to protect file names clashing across packages.
package.name #=> "datepicker"
package.import_paths #=> ["css/stylesheet.css", "js/*.js"]
Dir[package.load_path + "/**/*"] #=> ["datepicker/stylesheet.css", "datepicker/script.js"]
package.name #=> "datepicker"
package.import_paths #=> ["**/*.{js,css}"]
Dir[package.load_path + "/**/*"] #=> ["datepicker/css/stylesheet.css", "datepicker/js/script.js"]
This method returns an undefined value.
Cache remote source and import specified assets to #load_path.
81 82 83 84 85 86 87 88 |
# File 'lib/torba/package.rb', line 81 def build return if built? process_stylesheets process_other_assets rescue remove raise end |
#import_list ⇒ ImportList
111 112 113 |
# File 'lib/torba/package.rb', line 111 def import_list @import_list ||= build_import_list end |
#load_path ⇒ String
Returns path where processed files of the package reside. It’s located within Torba.home_path directory.
92 93 94 |
# File 'lib/torba/package.rb', line 92 def load_path @load_path ||= File.join(Torba.home_path, folder_name) end |
#logical_paths ⇒ Array<String>
Returns logical paths that the package contains.
99 100 101 |
# File 'lib/torba/package.rb', line 99 def logical_paths import_list.assets.map(&:logical_path) end |
#non_js_css_logical_paths ⇒ Array<String>
Returns logical paths that the package contains except JS ans CSS.
106 107 108 |
# File 'lib/torba/package.rb', line 106 def non_js_css_logical_paths import_list.non_js_css_assets.map(&:logical_path) end |
#remove ⇒ void
This method returns an undefined value.
Remove self from filesystem.
117 118 119 |
# File 'lib/torba/package.rb', line 117 def remove FileUtils.rm_rf(load_path) end |
#verify ⇒ false
Returns if package is not build.
60 61 62 |
# File 'lib/torba/package.rb', line 60 def verify built? end |