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



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

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)

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

Since:

  • 0.1.0



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

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



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

def name
  @name
end

#remote_sourceObject (readonly)

Returns instance that implements RemoteSources::Common

Returns:

Since:

  • 0.1.0



31
32
33
# File 'lib/torba/package.rb', line 31

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



80
81
82
83
84
85
86
87
# File 'lib/torba/package.rb', line 80

def build
  return if built?
  process_stylesheets
  process_other_assets
rescue
  remove
  raise
end

#import_listImportList

Returns:

Since:

  • 0.1.0



96
97
98
# File 'lib/torba/package.rb', line 96

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



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

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

#removevoid

This method returns an undefined value.

Remove self from filesystem.

Since:

  • 0.1.0



102
103
104
# File 'lib/torba/package.rb', line 102

def remove
  FileUtils.rm_rf(load_path)
end

#verifyObject

Raises:

Since:

  • 0.1.0



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

def verify
  raise Errors::UnbuiltPackage.new(name) unless built?
end