class VitalsImage::Optimizer
This is an abstract base class for optimizers
Public Class Methods
accept?(source)
click to toggle source
Implement this method in a concrete subclass. Have it return true when given a source from which it can calculate dimensions
# File lib/vitals_image/optimizer.rb, line 8 def self.accept?(source) false end
new(source, options = {})
click to toggle source
# 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
Public Instance Methods
html_options()
click to toggle source
# 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?()
click to toggle source
# File lib/vitals_image/optimizer.rb, line 48 def lazy_load? @options["lazy_load"] != false && VitalsImage.lazy_loading end
native_lazy_load?()
click to toggle source
# File lib/vitals_image/optimizer.rb, line 56 def native_lazy_load? lazy_load? && VitalsImage.lazy_loading == :native end
non_native_lazy_load?()
click to toggle source
# File lib/vitals_image/optimizer.rb, line 52 def non_native_lazy_load? lazy_load? && VitalsImage.lazy_loading != :native end
src()
click to toggle source
# File lib/vitals_image/optimizer.rb, line 19 def src if non_native_lazy_load? VitalsImage.lazy_loading_placeholder else source_url end end
Private Instance Methods
analyzed?()
click to toggle source
Override this method in a concrete subclass. Have it return true if width and height are available
# File lib/vitals_image/optimizer.rb, line 110 def analyzed? raise NotImplementedError end
fixed_dimensions?()
click to toggle source
# File lib/vitals_image/optimizer.rb, line 96 def fixed_dimensions? requested_width && requested_height end
height()
click to toggle source
# File lib/vitals_image/optimizer.rb, line 75 def height if !analyzed? || fixed_dimensions? requested_height else (original_height * scale).round end end
original_height()
click to toggle source
Override this method in a concrete subclass. Have it return the height of the image
# File lib/vitals_image/optimizer.rb, line 120 def original_height raise NotImplementedError end
original_width()
click to toggle source
Override this method in a concrete subclass. Have it return the width of the image
# File lib/vitals_image/optimizer.rb, line 115 def original_width raise NotImplementedError end
requested_height()
click to toggle source
# File lib/vitals_image/optimizer.rb, line 104 def requested_height @options["height"].to_f if @options["height"] end
requested_width()
click to toggle source
# File lib/vitals_image/optimizer.rb, line 100 def requested_width @options["width"].to_f if @options["width"] end
scale()
click to toggle source
# File lib/vitals_image/optimizer.rb, line 84 def scale [scale_x, scale_y].min end
scale_x()
click to toggle source
# File lib/vitals_image/optimizer.rb, line 88 def scale_x requested_width ? requested_width / original_width : 1.0 end
scale_y()
click to toggle source
# File lib/vitals_image/optimizer.rb, line 92 def scale_y requested_height ? requested_height / original_height : 1.0 end
source_url()
click to toggle source
Override this method in a concrete subclass. Have it return the url of the image
# File lib/vitals_image/optimizer.rb, line 126 def source_url raise NotImplementedError end
style()
click to toggle source
# File lib/vitals_image/optimizer.rb, line 61 def style if analyzed? && !requested_height "height:auto;" end end
width()
click to toggle source
# File lib/vitals_image/optimizer.rb, line 67 def width if !analyzed? || fixed_dimensions? requested_width else (original_width * scale).round end end