Class: VitalsImage::Optimizer

Inherits:
Object
  • Object
show all
Defined in:
lib/vitals_image/optimizer.rb

Overview

This is an abstract base class for optimizers

Direct Known Subclasses

Blank, Invariable, Url, Variable

Defined Under Namespace

Classes: Blank, Invariable, Url, Variable

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, options = {}) ⇒ Optimizer

Returns a new instance of Optimizer.

Raises:

  • (ArgumentError)


12
13
14
15
16
17
# File 'lib/vitals_image/optimizer.rb', line 12

def initialize(source, options = {})
  @source = source
  @options = options.deep_stringify_keys

  raise ArgumentError, "You must specify an alt for your image" if @options["alt"].blank? && VitalsImage.require_alt_attribute
end

Class Method Details

.accept?(source) ⇒ Boolean

Implement this method in a concrete subclass. Have it return true when given a source from which it can calculate dimensions

Returns:

  • (Boolean)


8
9
10
# File 'lib/vitals_image/optimizer.rb', line 8

def self.accept?(source)
  false
end

Instance Method Details

#html_optionsObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/vitals_image/optimizer.rb', line 27

def html_options
  @html_options ||= begin
    html_options = @options.dup.except("lazy_load")
    html_options["width"]           = width
    html_options["height"]          = height
    html_options["style"]           = "#{style} #{html_options["style"]}".squish.presence
    html_options["class"]           = "vitals-image #{html_options["class"]}".squish

    if non_native_lazy_load?
      html_options["class"]         = "#{VitalsImage.lazy_loading} #{html_options["class"]}".squish
      html_options["data"]        ||= {}
      html_options["data"]["src"]   = source_url
    elsif lazy_load?
      html_options["loading"]       = "lazy"
      html_options["decoding"]      = "async"
    end

    html_options.compact
  end
end

#lazy_load?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/vitals_image/optimizer.rb', line 48

def lazy_load?
  @options["lazy_load"] != false && VitalsImage.lazy_loading
end

#native_lazy_load?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/vitals_image/optimizer.rb', line 56

def native_lazy_load?
  lazy_load? && VitalsImage.lazy_loading == :native
end

#non_native_lazy_load?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/vitals_image/optimizer.rb', line 52

def non_native_lazy_load?
  lazy_load? && VitalsImage.lazy_loading != :native
end

#srcObject



19
20
21
22
23
24
25
# File 'lib/vitals_image/optimizer.rb', line 19

def src
  if non_native_lazy_load?
    VitalsImage.lazy_loading_placeholder
  else
    source_url
  end
end