lib/assetify/asset.rb in assetify-0.7.0.rc1 vs lib/assetify/asset.rb in assetify-1.0.0.rc1
- old
+ new
@@ -1,40 +1,50 @@
-require 'net/http'
-require 'fileutils'
+require "net/http"
+require "fileutils"
+require "assetify/asset/pkg"
+require "assetify/asset/pathfix"
module Assetify
+
class Asset
include Helpers
- attr_accessor :type, :name, :url, :ns, :pkg, :ver
- alias :ext :type
+ attr_accessor :type, :name, :url, :ns, :pkg, :ver, :ext
def initialize(type, name, url, ver = nil, params={})
raise "NoType" unless type
raise "NoName" unless name
raise "NoURL" unless url
@type, @name = type, name.to_s
@url = (@ver = ver) ? url.gsub(/{VERSION}/, @ver) : url
+ if @name =~ /\./
+ @name, @ext = name.split(".")
+ else
+ @ext = @type == :img ? find_ext_for(url) : @type
+ end
+
+ @pkg = params[:pkg]
@ns = params[:ns] || ""
@to = params[:to] || ""
- @pkg = params[:pkg]
end
def filename
return @filename if @filename
- @filename = Opt[:newname] ? name : url.split("/").last
- @filename += ".#{type}" unless @filename =~ /\.\w{1,6}$/
- @filename
+ @filename = "#{name}.#{ext}"
end
def find_path_for txt
case txt
when /js/ then :javascripts
when /css|style/ then :stylesheets
else :images
end
end
+ def find_ext_for file
+ file.split(".").last[0,3]
+ end
+
def path
args = if @to.empty?
tpath = Opt[find_path_for(type)]
raise "Don`t know where to put #{type} files..." unless tpath
[tpath, @ns ? @ns.to_s : ""]
@@ -46,11 +56,11 @@
def fullpath
@fullpath ||= File.join(path, filename)
end
- def check?
+ def file_exists?
File.exists? fullpath
end
def print_version
return "" unless ver
@@ -66,14 +76,28 @@
def ver
return nil unless @data
@ver ||= find_version(@data)
end
- def install!(force = false)
+ def header
LINE.p "-> #{name}.#{type}"
- if !force && check?
+ end
+
+ def check!
+ header
+ if file_exists? # Return if file is on path
@data = File.read(fullpath)
+ LINE.f "#{print_version}Installed"
+ else
+ LINE.f "Not Found", :red
+ end
+ end
+
+ def install!(force = false)
+ header
+ if !force && file_exists? # Return if file is on path
+ @data = File.read(fullpath)
return LINE.f "#{print_version}Installed"
end
begin
# Creates a thread to insert dots while downloading
points = Thread.new { loop do; LINE.p "."; sleep 1; end }
@@ -85,9 +109,19 @@
p "Fail: #{e} #{e.backtrace}"
ensure
points.kill
end
+ end
+
+ class << self
+ # Simple cache for Jsfile
+ def self.set_all v
+ @all = v
+ end
+ def self.all
+ @all
+ end
end
end
end