Class: Juicer::Collection

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Memoizable
Defined in:
lib/juicer-rails/collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, type, options = {}) ⇒ Collection

Returns a new instance of Collection.



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/juicer-rails/collection.rb', line 57

def initialize(key, type, options = {})
  @type = type
  @key = key
  @paths = JuicerRails.config[type.to_s][key.to_s]
  unless @paths.is_a?(Array)
    JuicerRails.warn("Wrong configuration for '#{key}'. Check you config file")
    @paths = []
  end
  @destination_dir = self.class.compiled_directory
  @destination_path = self.class.compiled_path(key, type)
  ensure_destination_directory_exists
end

Instance Attribute Details

#pathsObject (readonly)

Returns the value of attribute paths.



55
56
57
# File 'lib/juicer-rails/collection.rb', line 55

def paths
  @paths
end

Class Method Details

.compile(key, type) ⇒ Object



17
18
19
20
# File 'lib/juicer-rails/collection.rb', line 17

def compile(key, type)
  self.new(key, type).compile 
  destination_compiled_path(key, type).to_s
end

.compiled_directoryObject



33
34
35
# File 'lib/juicer-rails/collection.rb', line 33

def compiled_directory
  public_path.join(JuicerRails.compiled_assets_directory)
end

.compiled_path(key, type) ⇒ Object



37
38
39
# File 'lib/juicer-rails/collection.rb', line 37

def compiled_path(key, type)
  compiled_directory.join("#{key}.#{type}")
end

.destination_compiled_path(key, type) ⇒ Object



45
46
47
# File 'lib/juicer-rails/collection.rb', line 45

def destination_compiled_path(key, type)
  "/#{relative_compiled_path(key, type)}"
end

.dispatch(key, type) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/juicer-rails/collection.rb', line 9

def dispatch(key, type)
  if JuicerRails.perform_compilation?
    compile(key, type)
  else
    embed_standalone(key, type)
  end
end

.embed_standalone(key, type) ⇒ Object

Embeds each asset as standalone file. The best mode for development purposes. No merging occurs



25
26
27
28
29
30
31
# File 'lib/juicer-rails/collection.rb', line 25

def embed_standalone(key, type)
  dir = case type.to_s
        when 'js' then 'javascripts'
        when 'css' then 'stylesheets'
        end
  new(key, type).paths.collect { |p| Rails.root.join(p).relative_path_from(public_path.join(dir)).to_s }
end

.public_pathObject



49
50
51
# File 'lib/juicer-rails/collection.rb', line 49

def public_path
  @public_path ||= Rails.root.join('public')
end

.relative_compiled_path(key, type) ⇒ Object



41
42
43
# File 'lib/juicer-rails/collection.rb', line 41

def relative_compiled_path(key, type)
  compiled_path(key, type).relative_path_from(public_path)
end

Instance Method Details

#bust_cacheObject



103
104
105
106
# File 'lib/juicer-rails/collection.rb', line 103

def bust_cache
  Juicer::CssCacheBuster.new.save(@destination_path.to_s)
  self
end

#compileObject

Produces production ready assets. Merges, appends timestamps for images, embeds images into stylesheets and tries to do everything what juicer can. TODO: only merging and cache buster are implemented at the moment



74
75
76
77
# File 'lib/juicer-rails/collection.rb', line 74

def compile
  merge
  bust_cache if @type == 'css'
end

#ensure_destination_directory_existsObject



108
109
110
111
112
# File 'lib/juicer-rails/collection.rb', line 108

def ensure_destination_directory_exists
  unless @destination_dir.exist?
    FileUtils.mkdir_p(@destination_dir.to_s)
  end
end

#mergeObject

Depending on type merges and saves compiled asset



80
81
82
83
84
85
86
87
88
# File 'lib/juicer-rails/collection.rb', line 80

def merge
  if @type == 'js'
    merger = merge_javascripts
  elsif @type == 'css'
    merger = merge_stylesheets
  end
  save(merger)
  self
end

#merge_javascriptsObject



90
91
92
# File 'lib/juicer-rails/collection.rb', line 90

def merge_javascripts
  Juicer::Merger::JavaScriptMerger.new(@paths)
end

#merge_stylesheetsObject



94
95
96
# File 'lib/juicer-rails/collection.rb', line 94

def merge_stylesheets
  Juicer::Merger::StylesheetMerger.new(@paths, :document_root => Rails.root.join('public/stylesheets'))
end

#save(compiler) ⇒ Object



98
99
100
101
# File 'lib/juicer-rails/collection.rb', line 98

def save(compiler)
  compiler.save(@destination_path.to_s)
  self
end