Sha256: dd675d64864363a4e4aff92a88c964811ff179172e060159bc4943b7bcfeb86f

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

require 'json'

module PictureTag
  module Cache
    # Basic image information cache functionality
    module Base
      def initialize(base_name)
        @base_name = base_name
      end

      def [](key)
        data[key]
      end

      def []=(key, value)
        raise ArgumentError unless template.keys.include? key

        data[key] = value
      end

      # Call after updating data.
      def write
        return if PictureTag.site.config['disable_disk_cache']

        FileUtils.mkdir_p(File.join(base_directory, sub_directory))

        File.open(filename, 'w+') do |f|
          f.write JSON.generate(data)
        end
      end

      private

      def data
        @data ||= if File.exist?(filename)
                    JSON.parse(File.read(filename)).transform_keys(&:to_sym)
                  else
                    template
                  end
      end

      # /home/dave/my_blog/.jekyll-cache/jpt/(cache_dir)/assets/myimage.jpg.json
      # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      def base_directory
        File.join(PictureTag.site.cache_dir, 'jpt', cache_dir)
      end

      # /home/dave/my_blog/.jekyll-cache/jpt/(cache_dir)/assets/myimage.jpg.json
      #                                                 ^^^^^^^^
      def sub_directory
        File.dirname(@base_name)
      end

      # /home/dave/my_blog/.jekyll-cache/jpt/somefolder/myimage.jpg.json
      # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      def filename
        File.join(base_directory, @base_name + '.json')
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jekyll_picture_tag-1.14.0 lib/jekyll_picture_tag/cache/base.rb