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

#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

#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

#compressedObject

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

#fileObject

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?

Returns:

  • (Boolean)


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

#joinedObject

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