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



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

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

#cachedObject

The cached content



35
36
37
# File 'lib/assets/item.rb', line 35

def cached
  @cached ||= (read_cache || write_cache)
end

#compressedObject

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

#joinedObject

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

#kextObject

Key and extension



55
56
57
# File 'lib/assets/item.rb', line 55

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

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_cacheObject

Read cache



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

def read_cache
  File.read(cache_path) rescue nil
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_cacheObject

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