Sha256: 5a970e0d2cefaf1801f3ed7ba4e921409173f7aa0529ee27bb00708f52a67f92

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

module Asset
  class Item

    attr_accessor :path, :type, :key, :modified, :app, :name, :kpath

    # Init
    def initialize(*args)
      @path, @type, @key, @modified = args
      @app = !!(@path =~ /^bundle\.(js|css)$/)
      @name = @path.rpartition('.')[0]
      @kpath = "#{@name}-#{kext}"
    end

    # Get the files for this item
    def files(bundle = true)
      (@app and bundle) ? ::Asset.bundle[@type] : [@path]
    end

    # Get the sources for this item
    def sources
      files(!p?)
    end

    # Get the full path
    def src
      File.join('/assets', @type, (p? ? @kpath : @path))
    end

    # Get the content. Pass cache = false to fetch from disk instead of the cache.
    def content(key = nil)
      !key ? (@joined ||= joined) : (@cached ||= cached)
    end

    # The cached content
    def cached
      File.read(cache_path).tap{|f| return f if f} rescue nil
      compressed.tap{|r| write_cache(r)}
    end

    # Store in cache
    def write_cache(r)
      File.open(cache_path, 'w'){|f| f.write(r)}
    end

    # Cache path
    def cache_path
      @cache_path ||= File.join(::Asset.cache, kext)
    end

    # Key and extension
    def kext
      @kext ||= %{#{@key}.#{@type}}
    end

    # Compressed joined files
    def compressed
      @compressed ||= case @type
      when 'css'
        Tilt.new('scss', :style => :compressed){ joined }.render rescue joined
      when 'js'
        Uglifier.compile(joined, {}) rescue joined
      end
    end

    # All files joined
    def joined
      @joined ||= files.map{|f| File.read(File.join(::Asset.path, @type, f))}.join
    end

    # Production mode?
    def p?
      ::Asset.mode == 'production'
    end

    # Print data
    def print
      [:path, :type, :key, :name, :modified, :files, :content].map{|r| "#{r.upcase}: #{send(r).inspect}"}.join("\n")
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
asset-0.1.6 lib/assets/item.rb
asset-0.1.5 lib/assets/item.rb