Class: Juicer::Merger::Base

Inherits:
Object
  • Object
show all
Includes:
Chainable
Defined in:
lib/juicer/merger/base.rb

Direct Known Subclasses

JavaScriptMerger, StylesheetMerger

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Chainable

included, #next_in_chain, #next_in_chain=

Constructor Details

#initialize(files = [], options = {}) ⇒ Base

Returns a new instance of Base.



11
12
13
14
15
16
17
# File 'lib/juicer/merger/base.rb', line 11

def initialize(files = [], options = {})
  @files = []
  @root = nil
  @options = options
  @dependency_resolver ||= nil
  self.append files
end

Instance Attribute Details

#dependency_resolverObject

Returns the value of attribute dependency_resolver.



8
9
10
# File 'lib/juicer/merger/base.rb', line 8

def dependency_resolver
  @dependency_resolver
end

#filesObject (readonly)

Returns the value of attribute files.



9
10
11
# File 'lib/juicer/merger/base.rb', line 9

def files
  @files
end

Instance Method Details

#append(file) ⇒ Object Also known as: <<

Append contents to output. Resolves dependencies and adds required files recursively file = A file to add to merged content



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/juicer/merger/base.rb', line 24

def append(file)
  return file.each { |f| self << f } if file.class == Array
  return if @files.include?(file)

  if !@dependency_resolver.nil?
    path = File.expand_path(file)
    resolve_dependencies(path)
  elsif !@files.include?(file)
    @files << file
  end
end

#save(file_or_stream) ⇒ Object

Save the merged contents. If a filename is given the new file is written. If a stream is provided, contents are written to it.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/juicer/merger/base.rb', line 42

def save(file_or_stream)
  output = file_or_stream

  if output.is_a? String
    @root = Pathname.new(File.dirname(File.expand_path(output)))
    output = File.open(output, 'w')
  else
    @root = Pathname.new(File.expand_path("."))
  end

  @files.each do |f|
    output.puts(merge(f))
  end

  output.close if file_or_stream.is_a? String
end