Sha256: 0d13ba32098c1d212dd87c070c82ddcfdaaa850fe1560a6cd20b7983c5fb8641

Contents?: true

Size: 1.43 KB

Versions: 5

Compression:

Stored size: 1.43 KB

Contents

require 'hanami/utils/file_list'

module Hanami
  module Assets
    # Requested asset
    #
    # @since 0.8.0
    # @api private
    class Asset
      # @since 0.8.0
      # @api private
      PUBLIC_DIRECTORY = Hanami.public_directory.join('**', '*').to_s.freeze

      # @since 0.8.0
      # @api private
      URL_SEPARATOR = '/'.freeze

      # @since 0.8.0
      # @api private
      attr_reader :path

      # @since 0.8.0
      # @api private
      attr_reader :config

      # @since 0.8.0
      # @api private
      attr_reader :original

      # @since 0.8.0
      # @api private
      def initialize(sources, path)
        @path            = path
        @prefix, @config = sources.find { |p, _| path.start_with?(p) }

        if @prefix && @config
          @original = @config.sources.find(@path.sub(@prefix, ''))
        end
      end

      # @since 0.8.0
      # @api private
      def precompile?
        original && config
      end

      # @since 0.8.0
      # @api private
      def exist?
        return true unless original.nil?

        file_path = path.tr(URL_SEPARATOR, ::File::SEPARATOR)
        destination = find_asset do |a|
          a.end_with?(file_path)
        end

        !destination.nil?
      end

      private

      # @since 0.8.0
      # @api private
      def find_asset
        Utils::FileList[PUBLIC_DIRECTORY].find do |asset|
          yield asset unless ::File.directory?(asset)
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
hanami-1.0.0.beta2 lib/hanami/assets/asset.rb
hanami-1.0.0.beta1 lib/hanami/assets/asset.rb
hanami-0.9.2 lib/hanami/assets/asset.rb
hanami-0.9.1 lib/hanami/assets/asset.rb
hanami-0.9.0 lib/hanami/assets/asset.rb