Sha256: 65180483bada779653daabcd74eb382ba72e2cf9c041b6c1b5b6d57004fe765e

Contents?: true

Size: 1.48 KB

Versions: 3

Compression:

Stored size: 1.48 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
        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

3 entries across 3 versions & 1 rubygems

Version Path
jekyll_picture_tag-1.13.0 lib/jekyll_picture_tag/cache/base.rb
jekyll_picture_tag-1.12.0 lib/jekyll_picture_tag/cache/base.rb
jekyll_picture_tag-1.11.0 lib/jekyll_picture_tag/cache/base.rb