Class: Asset::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/assets/item.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#appObject

Returns the value of attribute app.



4
5
6
# File 'lib/assets/item.rb', line 4

def app
  @app
end

#bundleObject

Returns the value of attribute bundle.



4
5
6
# File 'lib/assets/item.rb', line 4

def bundle
  @bundle
end

#compressObject

Returns the value of attribute compress.



4
5
6
# File 'lib/assets/item.rb', line 4

def compress
  @compress
end

#keyObject

Returns the value of attribute key.



4
5
6
# File 'lib/assets/item.rb', line 4

def key
  @key
end

#kpathObject

Returns the value of attribute kpath.



4
5
6
# File 'lib/assets/item.rb', line 4

def kpath
  @kpath
end

#modifiedObject

Returns the value of attribute modified.



4
5
6
# File 'lib/assets/item.rb', line 4

def modified
  @modified
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/assets/item.rb', line 4

def name
  @name
end

#pathObject

Returns the value of attribute path.



4
5
6
# File 'lib/assets/item.rb', line 4

def path
  @path
end

#typeObject

Returns the value of attribute type.



4
5
6
# File 'lib/assets/item.rb', line 4

def type
  @type
end

Instance Method Details

#bundle_filesObject

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_pathObject

Cache path



41
42
43
# File 'lib/assets/item.rb', line 41

def cache_path
  @cache_path ||= File.join(::Asset.cache, kext)
end

#cachedObject

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

#compressedObject

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

#filesObject

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

#joinedObject

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

#kextObject

Key and extension



46
47
48
# File 'lib/assets/item.rb', line 46

def kext
  @kext ||= %{#{@key}.#{@type}}
end

#p?Boolean

Production mode?

Returns:

  • (Boolean)


66
67
68
# File 'lib/assets/item.rb', line 66

def p?
  ::Asset.mode == 'production'
end

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