Class: Asset::Item
- Inherits:
-
Object
- Object
- Asset::Item
- Defined in:
- lib/assets/item.rb
Instance Attribute Summary collapse
-
#bundle ⇒ Object
Returns the value of attribute bundle.
-
#compress ⇒ Object
Returns the value of attribute compress.
-
#key ⇒ Object
Returns the value of attribute key.
-
#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
-
#compressed ⇒ Object
Compressed joined files.
-
#content(cache = (::Asset.mode = 'production')) ⇒ Object
Get the content.
-
#file ⇒ Object
Get the file, meaning the full path with key.
-
#file? ⇒ Boolean
File? Or bundle?.
-
#files(keyed = true) ⇒ Object
Get the files.
-
#initialize(*args) ⇒ Item
constructor
Init.
-
#joined ⇒ Object
All files joined.
-
#print ⇒ Object
Print data.
-
#write_cache(r) ⇒ Object
Store in cache.
Constructor Details
#initialize(*args) ⇒ Item
Init
7 8 9 10 |
# File 'lib/assets/item.rb', line 7 def initialize(*args) @path, @type, @key, @modified, @compress, @bundle = args @name = @path.rpartition('.')[0] end |
Instance Attribute Details
#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 |
#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
#compressed ⇒ Object
Compressed joined files
47 48 49 50 51 52 53 54 |
# File 'lib/assets/item.rb', line 47 def compressed case @type when 'css' Tilt.new('scss', :style => :compressed){ joined }.render rescue joined when 'js' Uglifier.compile(joined, {}) rescue joined end end |
#content(cache = (::Asset.mode = 'production')) ⇒ Object
Get the content. Pass cache = false to fetch from disk instead of the cache.
32 33 34 35 36 37 38 39 |
# File 'lib/assets/item.rb', line 32 def content(cache = (::Asset.mode = 'production')) return joined unless cache File.read(File.join(::Asset.cache, %{#{@key}.#{@type}})).tap{|f| return f if f} rescue nil # Compress the files compressed.tap{|r| write_cache(r)} end |
#file ⇒ Object
Get the file, meaning the full path with key
27 28 29 |
# File 'lib/assets/item.rb', line 27 def file ::Asset.mode == 'development' ? @path : %{#{file? ? @name : 'application'}-#{@key}.#{@type}} end |
#file? ⇒ Boolean
File? Or bundle?
13 14 15 |
# File 'lib/assets/item.rb', line 13 def file? @path !~ /^application\.(js|css)$/ end |
#files(keyed = true) ⇒ Object
Get the files. Pass keyed = false to get the path instead of the file
18 19 20 21 22 23 24 |
# File 'lib/assets/item.rb', line 18 def files(keyed = true) if file? or (keyed and ::Asset.mode == 'production') [keyed ? file : path] else ::Asset.manifest.select{|i| i.type == @type and i.file?}.map{|i| keyed ? i.file : i.path} end end |
#joined ⇒ Object
All files joined
57 58 59 |
# File 'lib/assets/item.rb', line 57 def joined @joined ||= files(false).map{|f| File.read(File.join(::Asset.path, @type, f))}.join end |
#print ⇒ Object
Print data
62 63 64 |
# File 'lib/assets/item.rb', line 62 def print [:path, :type, :key, :name, :modified, :files, :content].each{|r| puts "#{r.upcase}: #{send(r).inspect}"} end |
#write_cache(r) ⇒ Object
Store in cache
42 43 44 |
# File 'lib/assets/item.rb', line 42 def write_cache(r) File.open(File.join(::Asset.cache, %{#{@key}.#{@type}}), 'w'){|f| f.write(r)} end |