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 ⇒ Object
Constructor Details
#initialize(name, remote_source, options = {}) ⇒ Package
Returns a new instance of Package.
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/torba/package.rb', line 49 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).
43 44 45 |
# File 'lib/torba/package.rb', line 43 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.
28 29 30 |
# File 'lib/torba/package.rb', line 28 def name @name end |
#remote_source ⇒ Object (readonly)
Returns instance that implements RemoteSources::Common.
31 32 33 |
# File 'lib/torba/package.rb', line 31 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.
83 84 85 86 87 88 89 90 |
# File 'lib/torba/package.rb', line 83 def build return if built? process_stylesheets process_other_assets rescue remove raise end |
#import_list ⇒ ImportList
113 114 115 |
# File 'lib/torba/package.rb', line 113 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.
94 95 96 |
# File 'lib/torba/package.rb', line 94 def load_path @load_path ||= File.join(Torba.home_path, folder_name) end |
#logical_paths ⇒ Array<String>
Returns logical paths that the package contains.
101 102 103 |
# File 'lib/torba/package.rb', line 101 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.
108 109 110 |
# File 'lib/torba/package.rb', line 108 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.
119 120 121 |
# File 'lib/torba/package.rb', line 119 def remove FileUtils.rm_rf(load_path) end |
#verify ⇒ Object
62 63 64 |
# File 'lib/torba/package.rb', line 62 def verify raise Errors::UnbuiltPackage.new(name) unless built? end |