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(key = nil) ⇒ Object
Get the content.
-
#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.
-
#p? ⇒ Boolean
Production mode?.
-
#print ⇒ Object
Print data.
-
#sources ⇒ Object
Get the sources for this item.
-
#src ⇒ Object
Get the full path.
-
#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 = 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
46 47 48 |
# File 'lib/assets/item.rb', line 46 def cache_path @cache_path ||= File.join(::Asset.cache, kext) end |
#cached ⇒ Object
The cached content
35 36 37 38 |
# File 'lib/assets/item.rb', line 35 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
56 57 58 59 60 61 62 63 |
# File 'lib/assets/item.rb', line 56 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.
30 31 32 |
# File 'lib/assets/item.rb', line 30 def content(key = nil) !key ? (@joined ||= joined) : (@cached ||= cached) 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
66 67 68 |
# File 'lib/assets/item.rb', line 66 def joined @joined ||= files.map{|f| File.read(File.join(::Asset.path, @type, f))}.join end |
#kext ⇒ Object
Key and extension
51 52 53 |
# File 'lib/assets/item.rb', line 51 def kext @kext ||= %{#{@key}.#{@type}} end |
#p? ⇒ Boolean
Production mode?
71 72 73 |
# File 'lib/assets/item.rb', line 71 def p? %w[staging production].include?(::Asset.mode) end |
#print ⇒ Object
Print data
76 77 78 |
# File 'lib/assets/item.rb', line 76 def print [:path, :type, :key, :name, :modified, :files, :content].map{|r| "#{r.upcase}: #{send(r).inspect}"}.join("\n") 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(r) ⇒ Object
Store in cache
41 42 43 |
# File 'lib/assets/item.rb', line 41 def write_cache(r) File.open(cache_path, 'w'){|f| f.write(r)} end |