class Refinery::ExtensionGeneration::FileMerger

Public Class Methods

new(templater, source, destination, options = {}) click to toggle source
# File lib/refinery/extension_generation.rb, line 353
def initialize(templater, source, destination, options = {})
  @templater = templater
  @source = source
  @destination = destination
  @options = {:to => @destination, :mode => 'a+'}.merge(options)
end

Public Instance Methods

call() click to toggle source
# File lib/refinery/extension_generation.rb, line 360
def call
  if %r{\.erb$} === @source.basename.to_s
    templated_merge!
  else
    merge!
  end
end
contents() click to toggle source
# File lib/refinery/extension_generation.rb, line 368
def contents
  merged_file_contents
end

Private Instance Methods

destination_lines() click to toggle source
# File lib/refinery/extension_generation.rb, line 409
def destination_lines
  @destination_lines ||= read_lines @destination
end
merge!(contents = merged_file_contents) click to toggle source
# File lib/refinery/extension_generation.rb, line 373
def merge!(contents = merged_file_contents)
  @options[:to].open(@options[:mode]) { |file| file.puts contents }
end
merge_rb() click to toggle source
# File lib/refinery/extension_generation.rb, line 396
def merge_rb
  (source_lines[0..-2] + destination_lines[1..-2] + [source_lines.last]).join "\n"
end
merge_yaml() click to toggle source
# File lib/refinery/extension_generation.rb, line 400
def merge_yaml
  YAML::load(@destination.read).deep_merge(YAML::load(@source.read)).
                                to_yaml.gsub(%r{^---\n}, '')
end
merged_file_contents() click to toggle source
# File lib/refinery/extension_generation.rb, line 377
def merged_file_contents
  case @destination.to_s
  # merge translation files together.
  when %r{.yml$} then merge_yaml
  # append any routes from the new file to the current one.
  when %r{/routes.rb$} then merge_rb
  # simply append the file contents
  else @source.read + @destination.read
  end
end
read_lines(file) click to toggle source
# File lib/refinery/extension_generation.rb, line 413
def read_lines(file)
  file.read.to_s.split "\n"
end
source_lines() click to toggle source
# File lib/refinery/extension_generation.rb, line 405
def source_lines
  @source_lines ||= read_lines @source
end
templated_merge!() click to toggle source
# File lib/refinery/extension_generation.rb, line 388
def templated_merge!
  Dir.mktmpdir do |tmp|
    tmp = Pathname.new(tmp)
    @templater.template @source, tmp.join(@source.basename), :verbose => false
    merge! tmp.join(@source.basename).read.to_s
  end
end