lib/assetify/asset.rb in assetify-2.0.1 vs lib/assetify/asset.rb in assetify-3.0.0

- old
+ new

@@ -1,71 +1,76 @@ -require "net/http" -require "fileutils" -require "assetify/asset/pkg" -require "assetify/asset/pathfix" +require 'net/http' +require 'fileutils' +require 'assetify/asset/pkg' +require 'assetify/asset/pathfix' module Assetify - + # + # What's an Asset? + # class Asset include Helpers attr_accessor :type, :name, :url, :ns, :pkg, :ver, :ext, :as - 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 + def initialize(type, name, url, ver = nil, params = {}) + raise 'NoType' unless type + raise 'NoName' unless name + raise 'NoURL' unless url + @type = type + @name = name.to_s @url = (@ver = ver) ? url.gsub(/{VERSION}/, @ver) : url if @name =~ /\./ - @name, @ext = name.split(".") + @name, @ext = name.split('.') else @ext = @type == :img ? find_ext_for(url) : @type end - @pkg, @as, @ns = params[:pkg], params[:as], params[:ns] - @to = params[:to] || "" + @pkg = params[:pkg] + @as = params[:as] + @ns = params[:ns] + @to = params[:to] || '' end def filename return @filename if @filename @filename = "#{name}.#{ext}" @filename += ".#{as}" if as @filename end - def find_ext_for file - file.split(".").last[0,3] + def find_ext_for(file) + file.split('.').last[0, 3] end # # Find correct path to put me # - def find_path_for txt + def find_path_for(txt) case txt when /js/ then :javascripts when /css|style/ then :stylesheets else :images end 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 : ""] - else - [Dir.pwd, @to] - end + tpath = Opt[find_path_for(type)] + raise "Don`t know where to put #{type} files..." unless tpath + [tpath, @ns ? @ns.to_s : ''] + else + [Dir.pwd, @to] + end @path = File.join(args) end def fullpath @fullpath ||= File.join(path, filename) end def file_exists? - File.exists? fullpath + File.exist? fullpath end def data return @data if @data # Get data, from a pkg or download directly @@ -75,80 +80,58 @@ @data = Pathfix.new(@data, @as, @ns).fix if @as @data end + def read_data + @data = File.read(fullpath) + end + # + # Prints info about the asset (TODO: move this to cli...) + # + def header + "-> #{name}.#{type}" + end + + # # Asset version # def ver return nil unless @data @ver ||= find_version(@data) end def print_version - return "" unless ver + return '' unless ver + # chop to only first 10 chars if it's big hash ver_str = ver.size > 10 ? ver[0..10] : ver[0] "v#{ver_str}" end # - # Prints info about the asset (TODO: move this to cli...) - # - def header - LINE.p "-> #{name}.#{type}" - 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 - - # # Write down asset to disk # - 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 } - - write data - LINE.f "#{print_version}ok" - rescue => e - LINE.f :FAIL, :red - p "Fail: #{e} #{e.backtrace}" - ensure - points.kill - end - + def install!(_force = false) + write data + rescue => e + LINE.f :FAIL, :red + p "Fail: #{e} #{e.backtrace}" end class << self # # Simple cache store, read Assetfile and dump it here to use. # def all @all ||= Assetfile.read end - def filter params + def filter(params) all.select do |a| blob = "#{a.name}#{a.pkg.name if a.pkg}" blob.include? params end end - end - end - end