Class: Torba::Package

Inherits:
Object
  • Object
show all
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.

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, remote_source, options = {}) ⇒ Package

Returns a new instance of Package.

Parameters:

  • name (String)

    see #name

  • remote_source (#[])
  • options (Hash) (defaults to: {})

Options Hash (options):

Since:

  • 0.1.0



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/torba/package.rb', line 47

def initialize(name, remote_source, options = {})
  @name = name
  @remote_source = remote_source
  @import_paths = (options[:import] || ["**/*"]).sort.map do |path|
    if path.end_with?("/")
      File.join(path, "**/*")
    else
      path
    end
  end
end

Instance Attribute Details

#import_pathsArray<String> (readonly)

Note:

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

Examples:

Direct path to a file

["build/underscore.js"]

Dir.glob pattern

["build/*.js", "**/*.css"]

Any file within directory (including subdirectories)

["build/"] # same as ["build/**/*"]

Returns:

  • (Array<String>)

    list of file paths to import (relative to remote source root).

See Also:

Since:

  • 0.1.0



41
42
43
# File 'lib/torba/package.rb', line 41

def import_paths
  @import_paths
end

#nameString (readonly)

Returns short package name, acts as as namespace within Sprockets’ load path. Doesn’t need to be equal to remote package name.

Returns:

  • (String)

    short package name, acts as as namespace within Sprockets’ load path. Doesn’t need to be equal to remote package name.

Since:

  • 0.1.0



26
27
28
# File 'lib/torba/package.rb', line 26

def name
  @name
end

#remote_sourceObject (readonly)

Returns instance that implements RemoteSources::Common.

Returns:

Since:

  • 0.1.0



29
30
31
# File 'lib/torba/package.rb', line 29

def remote_source
  @remote_source
end

Instance Method Details

#buildvoid

Note:

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.

Since:

  • 0.1.0



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_listImportList

Returns:

Since:

  • 0.1.0



111
112
113
# File 'lib/torba/package.rb', line 111

def import_list
  @import_list ||= build_import_list
end

#load_pathString

Returns path where processed files of the package reside. It’s located within Torba.home_path directory.

Returns:

  • (String)

    path where processed files of the package reside. It’s located within Torba.home_path directory.

Since:

  • 0.1.0



92
93
94
# File 'lib/torba/package.rb', line 92

def load_path
  @load_path ||= File.join(Torba.home_path, folder_name)
end

#logical_pathsArray<String>

Returns logical paths that the package contains.

Returns:

Since:

  • 0.3.0



99
100
101
# File 'lib/torba/package.rb', line 99

def logical_paths
  import_list.assets.map(&:logical_path)
end

#non_js_css_logical_pathsArray<String>

Returns logical paths that the package contains except JS ans CSS.

Returns:

  • (Array<String>)

    logical paths that the package contains except JS ans CSS.

Since:

  • 0.3.0



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

#removevoid

This method returns an undefined value.

Remove self from filesystem.

Since:

  • 0.1.0



117
118
119
# File 'lib/torba/package.rb', line 117

def remove
  FileUtils.rm_rf(load_path)
end

#verifyfalse

Returns if package is not build.

Returns:

  • (false)

    if package is not build.

Since:

  • 0.1.0



60
61
62
# File 'lib/torba/package.rb', line 60

def verify
  built?
end