Sha256: 9bfc8aca5ce8bb4eb5030de18f6d205b6c999bcdb212e8e6e59f5336efa9b55c

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

require "digest"

module Linner
  class Asset

    attr_accessor :path, :content

    def initialize(path)
      @path = path
      if File.exist? @path
        @mtime ||= File.mtime(path).to_i
      end
    end

    def mtime
      @mtime
    end

    def extname
      File.extname @path
    end

    def digest
      Digest::MD5.hexdigest content
    end

    def revision!
      revision = @path.chomp(extname) << "-#{digest}" << extname
      File.rename @path, revision
      revision.gsub /#{Linner.env.public_folder}/, ""
    end

    def content
      return @content if @content
      source = begin
        File.exist?(path) ? Tilt.new(path, :default_encoding => "UTF-8").render : ""
      rescue RuntimeError
        File.read(path)
      end
      if wrappable?
        @content = wrap(source)
      else
        @content = source
      end
    end

    def wrap(source)
      Wrapper.wrap(logical_path.chomp(File.extname logical_path), source)
    end

    def javascript?
      Tilt[path] and Tilt[path].default_mime_type == "application/javascript"
    end

    def stylesheet?
      Tilt[path] and Tilt[path].default_mime_type == "text/css"
    end

    def wrappable?
      !!(self.javascript? and !Linner.env.modules_ignored.include?(@path))
    end

    def write
      FileUtils.mkdir_p File.dirname(@path)
      File.open @path, "w" do |file|
        file.write @content
      end
    end

    def compress
      @content = Compressor.compress(self)
    end

    def logical_path
      @logical_path ||= @path.gsub(/#{Linner.env.paths.join("\/|")}/, "")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
linner-0.4.0 lib/linner/asset.rb