Class: Assetify::Asset

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/assetify/asset.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#find_version

Constructor Details

#initialize(type, name, url, ver = nil, params = {}) ⇒ Asset

Returns a new instance of Asset.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/assetify/asset.rb', line 12

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, @as, @ns = params[:pkg], params[:as], params[:ns]
  @to  = params[:to] || ""
end

Instance Attribute Details

#asObject

Returns the value of attribute as.



10
11
12
# File 'lib/assetify/asset.rb', line 10

def as
  @as
end

#extObject

Returns the value of attribute ext.



10
11
12
# File 'lib/assetify/asset.rb', line 10

def ext
  @ext
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/assetify/asset.rb', line 10

def name
  @name
end

#nsObject

Returns the value of attribute ns.



10
11
12
# File 'lib/assetify/asset.rb', line 10

def ns
  @ns
end

#pkgObject

Returns the value of attribute pkg.



10
11
12
# File 'lib/assetify/asset.rb', line 10

def pkg
  @pkg
end

#typeObject

Returns the value of attribute type.



10
11
12
# File 'lib/assetify/asset.rb', line 10

def type
  @type
end

#urlObject

Returns the value of attribute url.



10
11
12
# File 'lib/assetify/asset.rb', line 10

def url
  @url
end

#verObject

Asset version



83
84
85
# File 'lib/assetify/asset.rb', line 83

def ver
  @ver
end

Class Method Details

.allObject

Simple cache store, read Assetfile and dump it here to use.



139
140
141
# File 'lib/assetify/asset.rb', line 139

def all
  @all ||= Assetfile.read
end

.filter(params) ⇒ Object



143
144
145
146
147
148
# File 'lib/assetify/asset.rb', line 143

def filter params
  all.select do |a|
    blob = "#{a.name}#{a.pkg.name if a.pkg}"
    blob.include? params
  end
end

Instance Method Details

#check!Object



101
102
103
104
105
106
107
108
109
# File 'lib/assetify/asset.rb', line 101

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

#dataObject



69
70
71
72
73
74
75
76
77
78
# File 'lib/assetify/asset.rb', line 69

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

Returns:

  • (Boolean)


65
66
67
# File 'lib/assetify/asset.rb', line 65

def file_exists?
  File.exists? fullpath
end

#filenameObject



28
29
30
31
32
33
# File 'lib/assetify/asset.rb', line 28

def filename
  return @filename if @filename
  @filename = "#{name}.#{ext}"
  @filename += ".#{as}" if as
  @filename
end

#find_ext_for(file) ⇒ Object



35
36
37
# File 'lib/assetify/asset.rb', line 35

def find_ext_for file
  file.split(".").last[0,3]
end

#find_path_for(txt) ⇒ Object

Find correct path to put me



42
43
44
45
46
47
48
# File 'lib/assetify/asset.rb', line 42

def find_path_for txt
  case txt
  when /js/  then :javascripts
  when /css|style/ then :stylesheets
  else :images
  end
end

#fullpathObject



61
62
63
# File 'lib/assetify/asset.rb', line 61

def fullpath
  @fullpath ||= File.join(path, filename)
end

#headerObject

Prints info about the asset (TODO: move this to cli…)



97
98
99
# File 'lib/assetify/asset.rb', line 97

def header
  LINE.p "-> #{name}.#{type}"
end

#install!(force = false) ⇒ Object

Write down asset to disk



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/assetify/asset.rb', line 114

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

end

#pathObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/assetify/asset.rb', line 50

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


88
89
90
91
92
# File 'lib/assetify/asset.rb', line 88

def print_version
  return "" unless ver
  ver_str = ver.size > 10 ? ver[0..10] : ver[0]
  "v#{ver_str}"
end