Sha256: e10cfede49bf404dfaceee06c2fa7a7c92d617983523483ee5deb8f28a6cc0a4

Contents?: true

Size: 1.45 KB

Versions: 21

Compression:

Stored size: 1.45 KB

Contents

require 'hanami/utils/file_list'

module Hanami
  # @api private
  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

21 entries across 21 versions & 1 rubygems

Version Path
hanami-1.3.5 lib/hanami/assets/asset.rb
hanami-1.3.4 lib/hanami/assets/asset.rb
hanami-1.3.3 lib/hanami/assets/asset.rb
hanami-1.3.2 lib/hanami/assets/asset.rb
hanami-1.3.1 lib/hanami/assets/asset.rb
hanami-1.3.0 lib/hanami/assets/asset.rb
hanami-1.3.0.beta1 lib/hanami/assets/asset.rb
hanami-1.2.0 lib/hanami/assets/asset.rb
hanami-1.2.0.rc2 lib/hanami/assets/asset.rb
hanami-1.2.0.rc1 lib/hanami/assets/asset.rb
hanami-1.2.0.beta2 lib/hanami/assets/asset.rb
hanami-1.2.0.beta1 lib/hanami/assets/asset.rb
hanami-1.1.1 lib/hanami/assets/asset.rb
hanami-1.1.0 lib/hanami/assets/asset.rb
hanami-1.1.0.rc1 lib/hanami/assets/asset.rb
hanami-1.1.0.beta3 lib/hanami/assets/asset.rb
hanami-1.1.0.beta2 lib/hanami/assets/asset.rb
hanami-1.1.0.beta1 lib/hanami/assets/asset.rb
hanami-1.0.0 lib/hanami/assets/asset.rb
hanami-1.0.0.rc1 lib/hanami/assets/asset.rb