Sha256: 8e7dfe3d0db853433dc5dcbbfd36f7d0586fecaa9b5e78146211c772e400f755

Contents?: true

Size: 1.78 KB

Versions: 3

Compression:

Stored size: 1.78 KB

Contents

# encoding: utf-8

# stdlib
require 'base64'
require 'oj'
require 'escape_utils'
require 'sinatra/base'
require 'sprockets'

# classy assets components
require 'classy_assets/config'
require 'classy_assets/sprockets'
require 'classy_assets/sass/script/functions'
require 'classy_assets/sprockets/sass_importer'
require 'classy_assets/version'

module ClassyAssets
  # sprockets environment
  def self.sprockets
    if ClassyAssets.config.asset_cache
      ClassyAssets::Sprockets.new.environment.index
    else
      ClassyAssets::Sprockets.new.environment
    end
  end

  # singleton configuration
  def self.config(&block)
    yield ClassyAssets::Config.instance if block_given?
    ClassyAssets::Config.instance
  end

  # utility methods

  def self.asset_manifest
    Oj.load(File.open(config.asset_manifest_path, 'r'))
  end

  def self.asset_url_for(asset)
    if config.asset_digest
      asset = (manifest_exists?) ? asset_manifest['assets'][asset] : sprockets[asset].digest_path
    end
    debug = (config.asset_debug) ? '?body=1' : ''
    "#{config.asset_host}/#{config.asset_prefix}/#{asset}#{debug}"
  end

  def self.asset_data_uri_for(asset)
    asset = sprockets[asset]
    base64 = Base64.encode64(asset.to_s).gsub(/\s+/, "")
    "data:#{asset.content_type};base64,#{EscapeUtils.escape_url(base64)}"
  end

  def self.asset_tag_from(sources, ext)
    sources = [sources] unless sources.is_a? Array
    sources.map do |source|
      source = "#{source}.#{ext}" unless source =~ /\.#{ext}$/
      asset_url = (source =~ /^\/|http/) ? source : asset_url_for(source)
      yield(asset_url)
    end.join('')
  end

  private

  def self.determine_path_type
    (config.asset_digest) ? :digest_path : :logical_path
  end

  def self.manifest_exists?
    ::File.exists? config.asset_manifest_path
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
classy_assets-0.11.4 lib/classy_assets.rb
classy_assets-0.11.3 lib/classy_assets.rb
classy_assets-0.11.2 lib/classy_assets.rb