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.
-
#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
-
#cache_path ⇒ Object
Cache path.
-
#cached ⇒ Object
The cached content.
-
#compressed ⇒ Object
Compressed joined files.
-
#content(compress = p?) ) ⇒ Object
Get the content, will be compressed in production typically.
-
#files(bundle = true) ⇒ Object
Get the files for this item.
-
#initialize(*args) ⇒ Item
constructor
Init.
-
#joined ⇒ Object
All files joined.
-
#kext ⇒ Object
Key and extension.
-
#print ⇒ Object
Print data.
-
#read_cache ⇒ Object
Read cache.
-
#sources ⇒ Object
Get the sources for this item.
-
#src ⇒ Object
Get the full path.
-
#write_cache ⇒ 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 = 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 |
#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
#cache_path ⇒ Object
Cache path
50 51 52 |
# File 'lib/assets/item.rb', line 50 def cache_path @cache_path ||= File.join(::Asset.cache, kext) end |
#cached ⇒ Object
The cached content
35 36 37 |
# File 'lib/assets/item.rb', line 35 def cached @cached ||= (read_cache || write_cache) end |
#compressed ⇒ Object
Compressed joined files
60 61 62 63 64 65 66 67 |
# File 'lib/assets/item.rb', line 60 def compressed @compressed ||= case @type when 'css' Sass::Engine.new(joined, :syntax => :scss, :cache => false, :style => :compressed).render rescue joined when 'js' Uglifier.compile(joined, :mangle => false, :comments => :none) rescue joined end end |
#content(compress = p?) ) ⇒ Object
Get the content, will be compressed in production typically.
30 31 32 |
# File 'lib/assets/item.rb', line 30 def content(compress = p?) compress ? cached : joined end |
#files(bundle = true) ⇒ Object
Get the files for this item
15 16 17 |
# File 'lib/assets/item.rb', line 15 def files(bundle = true) (@app and bundle) ? ::Asset.bundle[@type] : [@path] end |
#joined ⇒ Object
All files joined
70 71 72 |
# File 'lib/assets/item.rb', line 70 def joined @joined ||= files.map{|f| File.read(File.join(::Asset.path, @type, f))}.join end |
#kext ⇒ Object
Key and extension
55 56 57 |
# File 'lib/assets/item.rb', line 55 def kext @kext ||= %{#{@key}.#{@type}} end |
#print ⇒ Object
Print data
75 76 77 |
# File 'lib/assets/item.rb', line 75 def print [:path, :type, :key, :name, :modified, :files, :content].map{|r| "#{r.upcase}: #{send(r).inspect}"}.join("\n") end |
#read_cache ⇒ Object
Read cache
40 41 42 |
# File 'lib/assets/item.rb', line 40 def read_cache File.read(cache_path) rescue nil end |
#sources ⇒ Object
Get the sources for this item
20 21 22 |
# File 'lib/assets/item.rb', line 20 def sources files(!p?) end |
#src ⇒ Object
Get the full path
25 26 27 |
# File 'lib/assets/item.rb', line 25 def src File.join('/assets', @type, (p? ? @kpath : @path)) end |
#write_cache ⇒ Object
Store in cache
45 46 47 |
# File 'lib/assets/item.rb', line 45 def write_cache compressed.tap{|c| File.atomic_write(cache_path){|f| f.write(c)}} end |