Sha256: 6b5bcb2774bc0f92516248428c4c10b7ceeb8ddb0e464b0363fb239c64dace3e

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

require 'thread'

module Lotus
  module Assets
    # Store assets references when compile mode is on.
    #
    # This is expecially useful in development mode, where we want to compile
    # only the assets that were changed from last browser refresh.
    #
    # @since 0.1.0
    # @api private
    class Cache
      # Return a new instance
      #
      # @return [Lotus::Assets::Cache] a new instance
      def initialize
        @data  = Hash.new{|h,k| h[k] = 0 }
        @mutex = Mutex.new
      end

      # Check if the given file is fresh or changed from last check.
      #
      # @param file [String,Pathname] the file path
      #
      # @return [TrueClass,FalseClass] the result of the check
      #
      # @since 0.1.0
      # @api private
      def fresh?(file)
        @mutex.synchronize do
          @data[file.to_s] < mtime(file)
        end
      end

      # Store the given file reference
      #
      # @param file [String,Pathname] the file path
      #
      # @return [TrueClass,FalseClass] the result of the check
      #
      # @since 0.1.0
      # @api private
      def store(file)
        @mutex.synchronize do
          @data[file.to_s] = mtime(file)
        end
      end

      private

      # @since 0.1.0
      # @api private
      def mtime(file)
        file.mtime.utc.to_i
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lotus-assets-0.1.0 lib/lotus/assets/cache.rb