Class: Assetify::Asset
- Inherits:
-
Object
- Object
- Assetify::Asset
- Includes:
- Helpers
- Defined in:
- lib/assetify/asset.rb
Overview
What’s an Asset?
Instance Attribute Summary collapse
-
#as ⇒ Object
Returns the value of attribute as.
-
#ext ⇒ Object
Returns the value of attribute ext.
-
#name ⇒ Object
Returns the value of attribute name.
-
#ns ⇒ Object
Returns the value of attribute ns.
-
#pkg ⇒ Object
Returns the value of attribute pkg.
-
#type ⇒ Object
Returns the value of attribute type.
-
#url ⇒ Object
Returns the value of attribute url.
-
#ver ⇒ Object
Asset version.
Class Method Summary collapse
-
.all ⇒ Object
Simple cache store, read Assetfile and dump it here to use.
- .filter(params) ⇒ Object
Instance Method Summary collapse
- #data ⇒ Object
- #file_exists? ⇒ Boolean
- #filename ⇒ Object
- #find_ext_for(file) ⇒ Object
-
#find_path_for(txt) ⇒ Object
Find correct path to put me.
- #fullpath ⇒ Object
-
#header ⇒ Object
Prints info about the asset (TODO: move this to cli…).
-
#initialize(type, name, url, ver = nil, params = {}) ⇒ Asset
constructor
A new instance of Asset.
-
#install!(_force = false) ⇒ Object
Write down asset to disk.
- #path ⇒ Object
- #print_version ⇒ Object
- #read_data ⇒ Object
Methods included from Helpers
Constructor Details
#initialize(type, name, url, ver = nil, params = {}) ⇒ Asset
Returns a new instance of Asset.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/assetify/asset.rb', line 14 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('.') else @ext = @type == :img ? find_ext_for(url) : @type end @pkg = params[:pkg] @as = params[:as] @ns = params[:ns] @to = params[:to] || '' end |
Instance Attribute Details
#as ⇒ Object
Returns the value of attribute as.
12 13 14 |
# File 'lib/assetify/asset.rb', line 12 def as @as end |
#ext ⇒ Object
Returns the value of attribute ext.
12 13 14 |
# File 'lib/assetify/asset.rb', line 12 def ext @ext end |
#name ⇒ Object
Returns the value of attribute name.
12 13 14 |
# File 'lib/assetify/asset.rb', line 12 def name @name end |
#ns ⇒ Object
Returns the value of attribute ns.
12 13 14 |
# File 'lib/assetify/asset.rb', line 12 def ns @ns end |
#pkg ⇒ Object
Returns the value of attribute pkg.
12 13 14 |
# File 'lib/assetify/asset.rb', line 12 def pkg @pkg end |
#type ⇒ Object
Returns the value of attribute type.
12 13 14 |
# File 'lib/assetify/asset.rb', line 12 def type @type end |
#url ⇒ Object
Returns the value of attribute url.
12 13 14 |
# File 'lib/assetify/asset.rb', line 12 def url @url end |
#ver ⇒ Object
Asset version
99 100 101 |
# File 'lib/assetify/asset.rb', line 99 def ver @ver end |
Class Method Details
.all ⇒ Object
Simple cache store, read Assetfile and dump it here to use.
125 126 127 |
# File 'lib/assetify/asset.rb', line 125 def all @all ||= Assetfile.read end |
.filter(params) ⇒ Object
129 130 131 132 133 134 |
# File 'lib/assetify/asset.rb', line 129 def filter(params) all.select do |a| blob = "#{a.name}#{a.pkg.name if a.pkg}" blob.include? params end end |
Instance Method Details
#data ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/assetify/asset.rb', line 74 def data return @data if @data # Get data, from a pkg or download directly @data = @pkg ? @pkg.get(url, :force).values.first : get_data(url) # Compile/fix paths if asked to @data = Pathfix.new(@data, @as, @ns).fix if @as @data end |
#file_exists? ⇒ Boolean
70 71 72 |
# File 'lib/assetify/asset.rb', line 70 def file_exists? File.exist? fullpath end |
#filename ⇒ Object
33 34 35 36 37 38 |
# File 'lib/assetify/asset.rb', line 33 def filename return @filename if @filename @filename = "#{name}.#{ext}" @filename += ".#{as}" if as @filename end |
#find_ext_for(file) ⇒ Object
40 41 42 |
# File 'lib/assetify/asset.rb', line 40 def find_ext_for(file) file.split('.').last[0, 3] end |
#find_path_for(txt) ⇒ Object
Find correct path to put me
47 48 49 50 51 52 53 |
# File 'lib/assetify/asset.rb', line 47 def find_path_for(txt) case txt when /js/ then :javascripts when /css|style/ then :stylesheets else :images end end |
#fullpath ⇒ Object
66 67 68 |
# File 'lib/assetify/asset.rb', line 66 def fullpath @fullpath ||= File.join(path, filename) end |
#header ⇒ Object
Prints info about the asset (TODO: move this to cli…)
92 93 94 |
# File 'lib/assetify/asset.rb', line 92 def header "-> #{name}.#{type}" end |
#install!(_force = false) ⇒ Object
Write down asset to disk
114 115 116 117 118 119 |
# File 'lib/assetify/asset.rb', line 114 def install!(_force = false) write data rescue => e LINE.f :FAIL, :red p "Fail: #{e} #{e.backtrace}" end |
#path ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/assetify/asset.rb', line 55 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 @path = File.join(args) end |
#print_version ⇒ Object
104 105 106 107 108 109 |
# File 'lib/assetify/asset.rb', line 104 def print_version 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 |
#read_data ⇒ Object
85 86 87 |
# File 'lib/assetify/asset.rb', line 85 def read_data @data = File.read(fullpath) end |