Module: PictureTag::Srcsets::Basics

Included in:
PixelRatio, Width
Defined in:
lib/jekyll-4-picture-tag/srcsets/basics.rb

Overview

Basic functionality for a srcset, which also handles file generation. Classes including this module must implement the to_a method, which accomplishes the following:

  • Return an array of srcset entries.

  • Call generate_file for each entry, giving it the desired width in pixels.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#mediaObject (readonly)

Returns the value of attribute media.



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

def media
  @media
end

#source_imageObject (readonly)

Returns the value of attribute source_image.



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

def source_image
  @source_image
end

Instance Method Details

#check_widths(targets) ⇒ Object

Check our source image size vs requested sizes



40
41
42
43
44
45
46
# File 'lib/jekyll-4-picture-tag/srcsets/basics.rb', line 40

def check_widths(targets)
  if targets.any? { |t| t > @source_image.width }
    handle_small_source(targets, @source_image.width)
  else
    targets
  end
end

#initialize(media:, format:) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/jekyll-4-picture-tag/srcsets/basics.rb', line 15

def initialize(media:, format:)
  @media = media # Associated Media Query, can be nil

  # Output format:
  @format = Utils.process_format(format, media)

  @source_image = PictureTag.source_images[@media]
end

#media_attributeObject

Generates an HTML attribute



49
50
51
# File 'lib/jekyll-4-picture-tag/srcsets/basics.rb', line 49

def media_attribute
  "(#{PictureTag.media_presets[@media]})"
end

#mime_typeObject

Allows us to add a type attribute to whichever element contains this srcset.



30
31
32
# File 'lib/jekyll-4-picture-tag/srcsets/basics.rb', line 30

def mime_type
  MIME::Types.type_for(@format).first.to_s
end

#sizesObject

Some srcsets have them, for those that don’t return nil.



35
36
37
# File 'lib/jekyll-4-picture-tag/srcsets/basics.rb', line 35

def sizes
  nil
end

#to_sObject



24
25
26
# File 'lib/jekyll-4-picture-tag/srcsets/basics.rb', line 24

def to_s
  to_a.join(', ')
end