Sha256: 7b749b438fabecae39fba889673087f638368ba11a0e701f57c96fbd9d5039ed

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

module AssetsBooster
  module Package
    class Base
      attr_accessor :name
      attr_accessor :filename
      attr_accessor :assets
      attr_accessor :mtime
      
      def initialize(name, assets)
        self.name = name+"_packaged"
        self.assets = assets
        self.filename = self.class.asset_path(self.name)
      end

      def exists?
        File.exists?(filename)
      end
      
      def mtime
        @mtime ||= self.class.merger.mtime(sources) 
      end
      
      def sources
        assets.each.map{ |asset| self.class.asset_path(asset) }
      end

      def delete
        File.delete(filename) if File.exists?(filename)
      end

      def merge
        AssetsBooster.log("Merging assets using #{self.class.merger.name} to #{relative_filename}...")
        save(self.class.merger.merge(sources, filename))
      end

      def compile
        merged = merge
        AssetsBooster.log("Compiling #{relative_filename} using #{self.class.compiler.name}...")
        code = self.class.compiler.compile(merged)
        AssetsBooster.log("Compilation finished: %5.2f%% saved." % [(1-code.size.to_f/merged.size)*100])
        save(code)
      end

      def view_helper
        sources = AssetsBooster::Configuration.boosted_environment? ? [name] : assets
        [view_helper_method, sources]
      end

      private
      
      def save(code)
        File.open(filename, "w") do |file|
          file.write(code)
        end
        File.utime(mtime, mtime, filename)
        code
      end

      def read
        File.open(filename, "r") do |file| 
          file.read
        end
      end
      
      def relative_filename
        filename.sub(Rails.root, ".")
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
assets_booster-0.0.1 lib/assets_booster/package/base.rb