Sha256: a53ede337b5117eefd95246c01a4eedcdbb28def1808652787417497f8523557

Contents?: true

Size: 1.78 KB

Versions: 4

Compression:

Stored size: 1.78 KB

Contents

module PictureTag
  module Instructions
    # Supervisor class, which manages all input handling and high level decision
    # making which depends on it.
    class Set
      def initialize(raw_tag_params)
        @raw_tag_params = raw_tag_params
      end

      def config
        @config ||= Configuration.new
      end

      def params
        @params ||= TagParser.new @raw_tag_params
      end

      def preset
        @preset ||= Preset.new params.preset_name
      end

      def html_attributes
        # Depends on both the preset and tag params.
        @html_attributes ||= HTMLAttributeSet.new params.leftovers
      end

      # These are our Media Query presets. It's really just a hash, and there
      # are no default values, so extracting this to its own class is overkill.
      def media_presets
        PictureTag.site.data.dig('picture', 'media_presets') || {}
      end

      def source_images
        @source_images ||= build_source_images
      end

      def crop(media = nil)
        params.geometries[media] || preset.crop(media)
      end

      def gravity(media = nil)
        params.gravities[media] || preset.gravity(media)
      end

      # Returns a class constant for the selected output format, which is used
      # to dynamically instantiate it.
      def output_class
        Object.const_get(
          'PictureTag::OutputFormats::' + Utils.titleize(preset['markup'])
        )
      end

      private

      def build_source_images
        source_names = params.source_names
        media_presets = params.media_presets

        sources = [SourceImage.new(source_names.shift)]

        while params.source_names.any?
          sources << SourceImage.new(
            source_names.shift, media_presets.shift
          )
        end

        sources
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
jekyll_picture_tag-1.11.0 lib/jekyll_picture_tag/instructions/set.rb
jekyll_picture_tag-1.10.2 lib/jekyll_picture_tag/instructions/set.rb
jekyll_picture_tag-1.10.1 lib/jekyll_picture_tag/instructions/set.rb
jekyll_picture_tag-1.10.0 lib/jekyll_picture_tag/instructions/set.rb