Class: Rack::ScriptStackerUtils::Stacker

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/scriptstacker.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Stacker

Returns a new instance of Stacker.



176
177
178
179
180
181
182
183
# File 'lib/rack/scriptstacker.rb', line 176

def initialize config
  @template = config[:template]
  @glob = config[:glob]
  @slot = config[:slot]
  @inject_before_tag = config[:inject_before_tag]

  @files = []
end

Instance Method Details

#find_files(source_path, serve_path) ⇒ Object



185
186
187
188
189
# File 'lib/rack/scriptstacker.rb', line 185

def find_files source_path, serve_path
  @files = @files + files_for(source_path).map do |filename|
    sprintf @template, serve_path + filename
  end
end

#replace_slot(chunk) ⇒ Object



203
204
205
206
207
# File 'lib/rack/scriptstacker.rb', line 203

def replace_slot chunk
  chunk.gsub /^(\s*)#{slot}/ do
    file_list($1).join "\n"
  end
end

#tag_inject(chunk) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/rack/scriptstacker.rb', line 191

def tag_inject chunk
  lines = chunk.split "\n", -1 # this preserves any trailing newlines
  index = lines.find_index { |line| line.match @inject_before_tag }
  return if index.nil?
  indent = lines[index].match(/^\s*/).to_s
  (
    lines[0...index] +
    file_list(indent + '  ') +
    lines[index..-1]
  ).join "\n"
end