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



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/middleman-sprockets/asset.rb', line 15

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

#base_nameObject (readonly)

Returns the value of attribute base_name.



6
7
8
# File 'lib/middleman-sprockets/asset.rb', line 6

def base_name
  @base_name
end

#destination_directoryObject

Returns the value of attribute destination_directory.



6
7
8
# File 'lib/middleman-sprockets/asset.rb', line 6

def destination_directory
  @destination_directory
end

#relative_source_pathObject (readonly)

Returns the value of attribute relative_source_path.



6
7
8
# File 'lib/middleman-sprockets/asset.rb', line 6

def relative_source_path
  @relative_source_path
end

#source_directoryObject (readonly)

Returns the value of attribute source_directory.



6
7
8
# File 'lib/middleman-sprockets/asset.rb', line 6

def source_directory
  @source_directory
end

#source_pathObject (readonly)

Returns the value of attribute source_path.



6
7
8
# File 'lib/middleman-sprockets/asset.rb', line 6

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



51
52
53
54
55
56
57
# File 'lib/middleman-sprockets/asset.rb', line 51

def destination_path
  return @destination_path if @destination_path

  fail ::Sprockets::FileNotFound, "Couldn't find an appropriate output directory for '#{source_path}'. 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`.



64
65
66
# File 'lib/middleman-sprockets/asset.rb', line 64

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

#extnameObject



136
137
138
# File 'lib/middleman-sprockets/asset.rb', line 136

def extname
  source_path.basename.to_s.scan(/(\.[^.]+)/).flatten
end

#file?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/middleman-sprockets/asset.rb', line 113

def file?
  source_path.file?
end

#has_extname?(*exts) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/middleman-sprockets/asset.rb', line 132

def has_extname?(*exts)
  !(extname & exts).empty?
end

#has_type?(t) ⇒ true, false

Check on file type

Returns:

  • (true, false)

    Is true if has type



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

def has_type?(t)
  type == t
end

#import?true, false

Should the asset imported?

Returns:

  • (true, false)

    Is true if it should be imported



32
33
34
# File 'lib/middleman-sprockets/asset.rb', line 32

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

#import_itObject

Tell asset that it is importable



90
91
92
# File 'lib/middleman-sprockets/asset.rb', line 90

def import_it
  @import_it = true # single =
end

#import_it?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/middleman-sprockets/asset.rb', line 128

def import_it?
  @import_it == true # double =
end

#in_trusted_source_directory?Boolean

Returns:

  • (Boolean)


94
95
96
97
# File 'lib/middleman-sprockets/asset.rb', line 94

def in_trusted_source_directory?
  source_directory.end_with?('images') ||
  source_directory.end_with?('fonts')
end

#is_font?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/middleman-sprockets/asset.rb', line 171

def is_font?
  is_font_by_path? || is_font_by_extension?
end

#is_font_by_extension?Boolean

Returns:

  • (Boolean)


181
182
183
# File 'lib/middleman-sprockets/asset.rb', line 181

def is_font_by_extension?
  has_extname?(*%w(.ttf .woff .eot .otf .svg .svgz))
end

#is_font_by_path?Boolean Also known as: is_in_fonts_directory?

Returns:

  • (Boolean)


175
176
177
178
# File 'lib/middleman-sprockets/asset.rb', line 175

def is_font_by_path?
  File.basename(source_directory.to_s) == 'fonts' ||
  source_path.dirname.basename.to_s == 'fonts'
end

#is_image?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/middleman-sprockets/asset.rb', line 140

def is_image?
  is_image_by_path? || (is_image_by_extension? && !is_font_by_path?)
end

#is_image_by_extension?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/middleman-sprockets/asset.rb', line 152

def is_image_by_extension?
  has_extname?(*%w(.gif .png .jpg .jpeg .webp .svg .svgz))
end

#is_image_by_path?Boolean Also known as: is_in_images_directory?

Returns:

  • (Boolean)


144
145
146
147
148
149
# File 'lib/middleman-sprockets/asset.rb', line 144

def is_image_by_path?
  File.basename(source_directory.to_s) == 'images' ||
  File.basename(source_directory.to_s) == 'img' ||
  source_path.dirname.basename.to_s == 'images' ||
  source_path.dirname.basename.to_s == 'img'
end

#is_script?Boolean

Returns:

  • (Boolean)


185
186
187
# File 'lib/middleman-sprockets/asset.rb', line 185

def is_script?
  is_script_by_path? || is_script_by_extension?
end

#is_script_by_extension?Boolean

Returns:

  • (Boolean)


196
197
198
# File 'lib/middleman-sprockets/asset.rb', line 196

def is_script_by_extension?
  has_extname?(*%w(.js .coffee))
end

#is_script_by_path?Boolean

Returns:

  • (Boolean)


189
190
191
192
193
194
# File 'lib/middleman-sprockets/asset.rb', line 189

def is_script_by_path?
  File.basename(source_directory.to_s) == 'javascripts' ||
  File.basename(source_directory.to_s) == 'js' ||
  source_path.dirname.basename.to_s == 'javascripts' ||
  source_path.dirname.basename.to_s == 'js'
end

#is_stylesheet?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/middleman-sprockets/asset.rb', line 156

def is_stylesheet?
  is_stylesheet_by_path? || is_stylesheet_by_extension?
end

#is_stylesheet_by_extension?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/middleman-sprockets/asset.rb', line 160

def is_stylesheet_by_extension?
  has_extname?(*%w(.css .sass .scss .styl .less))
end

#is_stylesheet_by_path?Boolean

Returns:

  • (Boolean)


164
165
166
167
168
169
# File 'lib/middleman-sprockets/asset.rb', line 164

def is_stylesheet_by_path?
  File.basename(source_directory.to_s) == 'stylesheets' ||
  File.basename(source_directory.to_s) == 'css' ||
  source_path.dirname.basename.to_s == 'stylesheets' ||
  source_path.dirname.basename.to_s == 'css'
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



85
86
87
# File 'lib/middleman-sprockets/asset.rb', line 85

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

#partial?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/middleman-sprockets/asset.rb', line 117

def partial?
  base_name.to_s.start_with? '_'
end

#typeObject



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/middleman-sprockets/asset.rb', line 99

def type
  @type ||= if is_image?
              :image
            elsif is_script?
              :script
            elsif is_stylesheet?
              :stylesheet
            elsif is_font?
              :font
            else
              :unknown
            end
end

#valid?true, false

Is it a valid asset

Returns:

  • (true, false)

    If the asset is valid return true



124
125
126
# File 'lib/middleman-sprockets/asset.rb', line 124

def valid?
  file? && !partial?
end