Class: Middleman::Sprockets::Asset
- Inherits:
-
Object
- Object
- Middleman::Sprockets::Asset
- Defined in:
- lib/middleman-sprockets/asset.rb
Overview
Asset
Instance Attribute Summary collapse
-
#base_name ⇒ Object
readonly
Returns the value of attribute base_name.
-
#destination_directory ⇒ Object
Returns the value of attribute destination_directory.
-
#relative_source_path ⇒ Object
readonly
Returns the value of attribute relative_source_path.
-
#source_directory ⇒ Object
readonly
Returns the value of attribute source_directory.
-
#source_path ⇒ Object
readonly
Returns the value of attribute source_path.
Instance Method Summary collapse
-
#destination_path ⇒ Pathname
Path where the asset should be stored.
-
#destination_path=(path) ⇒ Object
Sets the destination_path.
- #extname ⇒ Object
- #file? ⇒ Boolean
- #has_extname?(*exts) ⇒ Boolean
-
#has_type?(t) ⇒ true, false
Check on file type.
-
#import? ⇒ true, false
Should the asset imported?.
-
#import_it ⇒ Object
Tell asset that it is importable.
- #import_it? ⇒ Boolean
- #in_trusted_source_directory? ⇒ Boolean
-
#initialize(source_path, options) ⇒ Asset
constructor
Create instance.
- #is_font? ⇒ Boolean
- #is_font_by_extension? ⇒ Boolean
- #is_font_by_path? ⇒ Boolean (also: #is_in_fonts_directory?)
- #is_image? ⇒ Boolean
- #is_image_by_extension? ⇒ Boolean
- #is_image_by_path? ⇒ Boolean (also: #is_in_images_directory?)
- #is_script? ⇒ Boolean
- #is_script_by_extension? ⇒ Boolean
- #is_script_by_path? ⇒ Boolean
- #is_stylesheet? ⇒ Boolean
- #is_stylesheet_by_extension? ⇒ Boolean
- #is_stylesheet_by_path? ⇒ Boolean
-
#match?(path) ⇒ true, false
Check if given path matches source_path.
- #partial? ⇒ Boolean
- #type ⇒ Object
-
#valid? ⇒ true, false
Is it a valid asset.
Constructor Details
#initialize(source_path, options) ⇒ Asset
Create instance
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/middleman-sprockets/asset.rb', line 15 def initialize(source_path, ) source_directory = .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_name ⇒ Object (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_directory ⇒ Object
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_path ⇒ Object (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_directory ⇒ Object (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_path ⇒ Object (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_path ⇒ Pathname
Path where the asset should be stored
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
64 65 66 |
# File 'lib/middleman-sprockets/asset.rb', line 64 def destination_path=(path) @destination_path = Pathname.new path end |
#extname ⇒ Object
136 137 138 |
# File 'lib/middleman-sprockets/asset.rb', line 136 def extname source_path.basename.to_s.scan(/(\.[^.]+)/).flatten end |
#file? ⇒ Boolean
113 114 115 |
# File 'lib/middleman-sprockets/asset.rb', line 113 def file? source_path.file? end |
#has_extname?(*exts) ⇒ 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
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?
32 33 34 |
# File 'lib/middleman-sprockets/asset.rb', line 32 def import? valid? && (in_trusted_source_directory? || import_it?) end |
#import_it ⇒ Object
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
128 129 130 |
# File 'lib/middleman-sprockets/asset.rb', line 128 def import_it? @import_it == true # double = end |
#in_trusted_source_directory? ⇒ 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
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
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?
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
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
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?
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
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
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
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
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
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
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
85 86 87 |
# File 'lib/middleman-sprockets/asset.rb', line 85 def match?(path) source_path == Pathname.new(path) end |
#partial? ⇒ Boolean
117 118 119 |
# File 'lib/middleman-sprockets/asset.rb', line 117 def partial? base_name.to_s.start_with? '_' end |
#type ⇒ Object
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
124 125 126 |
# File 'lib/middleman-sprockets/asset.rb', line 124 def valid? file? && !partial? end |