Class: PictureTag::SourceImage
- Inherits:
-
Object
- Object
- PictureTag::SourceImage
- Defined in:
- lib/jekyll-4-picture-tag/source_image.rb
Overview
Handles a given source image file and its properties. Provides a speed advantage by storing expensive file reads and writes in instance variables, to be reused by many different source images.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#shortname ⇒ Object
readonly
Returns the value of attribute shortname.
Instance Method Summary collapse
- #aspect_ratio ⇒ Object
-
#base_name ⇒ Object
Includes path relative to default sorce folder, and the original filename.
- #digest ⇒ Object
-
#ext ⇒ Object
File exention.
-
#initialize(relative_filename) ⇒ SourceImage
constructor
A new instance of SourceImage.
- #size ⇒ Object
- #width ⇒ Object
Constructor Details
#initialize(relative_filename) ⇒ SourceImage
Returns a new instance of SourceImage.
8 9 10 11 |
# File 'lib/jekyll-4-picture-tag/source_image.rb', line 8 def initialize(relative_filename) @shortname = relative_filename @name = grab_file relative_filename end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/jekyll-4-picture-tag/source_image.rb', line 6 def name @name end |
#shortname ⇒ Object (readonly)
Returns the value of attribute shortname.
6 7 8 |
# File 'lib/jekyll-4-picture-tag/source_image.rb', line 6 def shortname @shortname end |
Instance Method Details
#aspect_ratio ⇒ Object
21 22 23 |
# File 'lib/jekyll-4-picture-tag/source_image.rb', line 21 def aspect_ratio @aspect_ratio ||= size[:width].to_f / size[:height].to_f end |
#base_name ⇒ Object
Includes path relative to default sorce folder, and the original filename.
30 31 32 |
# File 'lib/jekyll-4-picture-tag/source_image.rb', line 30 def base_name @shortname.delete_suffix File.extname(@shortname) end |
#digest ⇒ Object
25 26 27 |
# File 'lib/jekyll-4-picture-tag/source_image.rb', line 25 def digest @digest ||= Digest::MD5.hexdigest(File.read(@name))[0..5] end |
#ext ⇒ Object
File exention
35 36 37 38 |
# File 'lib/jekyll-4-picture-tag/source_image.rb', line 35 def ext # [1..-1] will strip the leading period. @ext ||= File.extname(@name)[1..-1].downcase end |
#size ⇒ Object
13 14 15 |
# File 'lib/jekyll-4-picture-tag/source_image.rb', line 13 def size @size ||= build_size end |
#width ⇒ Object
17 18 19 |
# File 'lib/jekyll-4-picture-tag/source_image.rb', line 17 def width size[:width] end |