Sha256: b2af8aee97260d9e7b7d6b37894e476ff79233fceda1836051ffe43ac1cae651

Contents?: true

Size: 1.26 KB

Versions: 3

Compression:

Stored size: 1.26 KB

Contents

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 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

3 entries across 3 versions & 1 rubygems

Version Path
linner-0.3.2 lib/linner/asset.rb
linner-0.3.1 lib/linner/asset.rb
linner-0.3.0 lib/linner/asset.rb