Sha256: 2308b1077c853a38cd30c358f1477da43b5b718cd36378fef5d42d8ab4508f49

Contents?: true

Size: 995 Bytes

Versions: 2

Compression:

Stored size: 995 Bytes

Contents

require 'json'

module RailsExternalAssets
  module AssetFinder
    def external_asset(path)
      external_path = File.join(RailsExternalAssets.config.base_path, asset_path(path))
      block_given? ? yield(external_path) : external_path
    end

    def asset_path(path)
      new_path = asset_manifest[path]
      throw_unknown_path(path, RailsExternalAssets.config.manifest_file) unless new_path
      new_path
    end

    def asset_manifest
      manifest_file = RailsExternalAssets.config.manifest_file
      throw_invalid_manifest(manifest_file) unless File.file? manifest_file
      JSON.parse(File.read manifest_file)
    end


    private

    def throw_unknown_path(path, manifest_file)
        raise Errors::UnknownAssetManifestKey.new("No corresponding file found for \"#{path}\" in \"#{manifest_file}\".")
    end

    def throw_invalid_manifest(manifest_file)
        raise Errors::InvalidManifestFile.new("Manifest file, \"#{manifest_file}\", was not found.")
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rails_external_assets-0.3.1 lib/rails_external_assets/asset_finder.rb
rails_external_assets-0.3.0 lib/rails_external_assets/asset_finder.rb