Class: PictureTag::SourceImage

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#nameObject (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

#shortnameObject (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_ratioObject



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_nameObject

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

#digestObject



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

#extObject

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

#sizeObject



13
14
15
# File 'lib/jekyll-4-picture-tag/source_image.rb', line 13

def size
  @size ||= build_size
end

#widthObject



17
18
19
# File 'lib/jekyll-4-picture-tag/source_image.rb', line 17

def width
  size[:width]
end