Class: Asset::Item
- Inherits:
-
Object
- Object
- Asset::Item
- Defined in:
- lib/assets/item.rb
Instance Attribute Summary collapse
-
#app ⇒ Object
Returns the value of attribute app.
-
#bundle ⇒ Object
Returns the value of attribute bundle.
-
#compress ⇒ Object
Returns the value of attribute compress.
-
#key ⇒ Object
Returns the value of attribute key.
-
#kpath ⇒ Object
Returns the value of attribute kpath.
-
#modified ⇒ Object
Returns the value of attribute modified.
-
#name ⇒ Object
Returns the value of attribute name.
-
#path ⇒ Object
Returns the value of attribute path.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
-
#bundle_files ⇒ Object
Get the files for the bundle.
-
#cache_path ⇒ Object
Cache path.
-
#cached ⇒ Object
The cached content.
-
#compressed ⇒ Object
Compressed joined files.
-
#content(key = nil) ⇒ Object
Get the content.
-
#files ⇒ Object
Get the files for this item.
-
#initialize(*args) ⇒ Item
constructor
Init.
-
#joined ⇒ Object
All files joined.
-
#kext ⇒ Object
Key and extension.
-
#p? ⇒ Boolean
Production mode?.
-
#print ⇒ Object
Print data.
-
#write_cache(r) ⇒ Object
Store in cache.
Constructor Details
#initialize(*args) ⇒ Item
Init
7 8 9 10 11 12 |
# File 'lib/assets/item.rb', line 7 def initialize(*args) @path, @type, @key, @modified, @compress, @bundle = args @app = !!(@path =~ /^bundle\.(js|css)$/) @name = @path.rpartition('.')[0] @kpath = "#{@name}-#{kext}" end |
Instance Attribute Details
#app ⇒ Object
Returns the value of attribute app.
4 5 6 |
# File 'lib/assets/item.rb', line 4 def app @app end |
#bundle ⇒ Object
Returns the value of attribute bundle.
4 5 6 |
# File 'lib/assets/item.rb', line 4 def bundle @bundle end |
#compress ⇒ Object
Returns the value of attribute compress.
4 5 6 |
# File 'lib/assets/item.rb', line 4 def compress @compress end |
#key ⇒ Object
Returns the value of attribute key.
4 5 6 |
# File 'lib/assets/item.rb', line 4 def key @key end |
#kpath ⇒ Object
Returns the value of attribute kpath.
4 5 6 |
# File 'lib/assets/item.rb', line 4 def kpath @kpath end |
#modified ⇒ Object
Returns the value of attribute modified.
4 5 6 |
# File 'lib/assets/item.rb', line 4 def modified @modified end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/assets/item.rb', line 4 def name @name end |
#path ⇒ Object
Returns the value of attribute path.
4 5 6 |
# File 'lib/assets/item.rb', line 4 def path @path end |
#type ⇒ Object
Returns the value of attribute type.
4 5 6 |
# File 'lib/assets/item.rb', line 4 def type @type end |
Instance Method Details
#bundle_files ⇒ Object
Get the files for the bundle
20 21 22 |
# File 'lib/assets/item.rb', line 20 def bundle_files @bundle_files ||= ::Asset.manifest.select{|i| i.type == @type and i.bundle and !i.app}.map{|i| p? ? i.kpath : i.path} end |
#cache_path ⇒ Object
Cache path
41 42 43 |
# File 'lib/assets/item.rb', line 41 def cache_path @cache_path ||= File.join(::Asset.cache, kext) end |
#cached ⇒ Object
The cached content
30 31 32 33 |
# File 'lib/assets/item.rb', line 30 def cached File.read(cache_path).tap{|f| return f if f} rescue nil compressed.tap{|r| write_cache(r)} end |
#compressed ⇒ Object
Compressed joined files
51 52 53 54 55 56 57 58 |
# File 'lib/assets/item.rb', line 51 def compressed @compressed ||= case @type when 'css' Tilt.new('scss', :style => :compressed){ joined }.render rescue joined when 'js' Uglifier.compile(joined, {}) rescue joined end end |
#content(key = nil) ⇒ Object
Get the content. Pass cache = false to fetch from disk instead of the cache.
25 26 27 |
# File 'lib/assets/item.rb', line 25 def content(key = nil) (!key or !@compress) ? (@joined ||= joined) : (@cached ||= cached) end |
#files ⇒ Object
Get the files for this item
15 16 17 |
# File 'lib/assets/item.rb', line 15 def files (@app and !p?) ? bundle_files : [p? ? @kpath : @path] end |
#joined ⇒ Object
All files joined
61 62 63 |
# File 'lib/assets/item.rb', line 61 def joined @joined ||= files.map{|f| File.read(File.join(::Asset.path, @type, f))}.join end |
#kext ⇒ Object
Key and extension
46 47 48 |
# File 'lib/assets/item.rb', line 46 def kext @kext ||= %{#{@key}.#{@type}} end |
#p? ⇒ Boolean
Production mode?
66 67 68 |
# File 'lib/assets/item.rb', line 66 def p? ::Asset.mode == 'production' end |
#print ⇒ Object
Print data
71 72 73 |
# File 'lib/assets/item.rb', line 71 def print [:path, :type, :key, :name, :modified, :files, :content].map{|r| "#{r.upcase}: #{send(r).inspect}"}.join("\n") end |
#write_cache(r) ⇒ Object
Store in cache
36 37 38 |
# File 'lib/assets/item.rb', line 36 def write_cache(r) File.open(cache_path, 'w'){|f| f.write(r)} end |