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 = 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

#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

#cache_pathObject

Cache path



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

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

#cachedObject

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

#compressedObject

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

#joinedObject

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

#kextObject

Key and extension



51
52
53
# File 'lib/assets/item.rb', line 51

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

#p?Boolean

Production mode?

Returns:

  • (Boolean)


71
72
73
# File 'lib/assets/item.rb', line 71

def p?
  %w[staging production].include?(::Asset.mode)
end

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

#sourcesObject

Get the sources for this item



20
21
22
# File 'lib/assets/item.rb', line 20

def sources
  files(!p?)
end

#srcObject

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