Class: Middleman::Sprockets::Asset

Inherits:
Object
  • Object
show all
Defined in:
lib/middleman-sprockets/asset.rb

Overview

Asset

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_path, options) ⇒ Asset

Create instance

Parameters:

  • logical_path (Pathname)

    The logical path to the asset given in config.rb

  • output_dir (proc)

    An individual output directory for that particular asset



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/middleman-sprockets/asset.rb', line 22

def initialize(source_path, options)
  source_directory = options.fetch(:source_directory, nil)

  fail ArgumentError, 'Missing argument source_directory' unless source_directory

  @source_directory     = source_directory

  @source_path          = Pathname.new(source_path)
  @relative_source_path = @source_path.relative_path_from(Pathname.new(source_directory))
  @base_name            = @source_path.basename
  @import_it            = false
end

Instance Attribute Details

#destination_directory=(path) ⇒ Object

Set destination directory

@param [String] path
  The path to the destination directory

@return [Pathname]
  The path as pathname


82
83
84
# File 'lib/middleman-sprockets/asset.rb', line 82

def destination_directory=(path)
  @destination_directory = Pathname.new path
end

#source_pathObject (readonly)

Returns the value of attribute source_path.



13
14
15
# File 'lib/middleman-sprockets/asset.rb', line 13

def source_path
  @source_path
end

Instance Method Details

#destination_pathPathname

Path where the asset should be stored

Returns:

  • (Pathname)

    Returns ‘destination_path` if set, otherwise build result: destination_directory + relative_source_path

Raises:

  • (::Sprockets::FileNotFound)

    Raise error if destination_directory was not set previously from outside



58
59
60
61
62
63
64
# File 'lib/middleman-sprockets/asset.rb', line 58

def destination_path
  return @destination_path if @destination_path

  fail ::Sprockets::FileNotFound, "Couldn't find an appropriate output directory for '#{destination_directory}' - halting because it was explicitly requested via 'import_asset'" unless destination_directory

  destination_directory + relative_source_path
end

#destination_path=(path) ⇒ Object

Sets the destination_path

Parameters:

  • path (String, Pathname)

    The output path for asset as string or pathname. It will be converted to ‘Pathname`.



71
72
73
# File 'lib/middleman-sprockets/asset.rb', line 71

def destination_path=(path)
  @destination_path = Pathname.new path
end

#has_type?(t) ⇒ true, false

Check on file type

Returns:

  • (true, false)

    Is true if has type



47
48
49
# File 'lib/middleman-sprockets/asset.rb', line 47

def has_type?(t)
  type == t
end

#import?true, false

Should the asset imported?

Returns:

  • (true, false)

    Is true if it should be imported



39
40
41
# File 'lib/middleman-sprockets/asset.rb', line 39

def import?
  valid? && (in_trusted_source_directory? || import_it?)
end

#import_itObject

Tell asset that it is importable



97
98
99
# File 'lib/middleman-sprockets/asset.rb', line 97

def import_it
  @import_it = true # single =
end

#match?(path) ⇒ true, false

Check if given path matches source_path

Parameters:

  • path (String)

    The path to be checked

Returns:

  • (true, false)

    The result of check



92
93
94
# File 'lib/middleman-sprockets/asset.rb', line 92

def match?(path)
  source_path == Pathname.new(path)
end