class Guard::Entangle::Entangler

Attributes

options[R]

Public Class Methods

new(options={}) click to toggle source

Initialize entangler

@param [Hash] options The options passed in

# File lib/guard/entangle/entangler.rb, line 10
def initialize(options={})
  @options = options
end

Public Instance Methods

convert(path) click to toggle source

Convert the file

@param [String] path The path of file @return [String] The entangled content

# File lib/guard/entangle/entangler.rb, line 19
def convert(path)
  if not File.exists?(path) or not File.readable?(path)
    return false
  end
  pn = Pathname.new(path)
  file = File.open(path, 'rb')
  contents = file.read
  contents = convert_file(contents, pn.dirname)
end

Private Instance Methods

convert_file(contents, base) click to toggle source

Convert the file

@param [String] contents The content to replace in @param [String] base The base path @return [String] The replaced content

# File lib/guard/entangle/entangler.rb, line 37
def convert_file(contents, base)
  matches = Set.new search(contents)
  if not matches.empty?
    matches.each do |entry|
      contents = replace(contents, entry, base)
    end
  else
    return contents
  end
  contents
end
replace(content, file, path) click to toggle source

Replace the file hook with the contents of the file

@param [String] content The content of the file @param [String] file The file hook @param [String] path The base path of the file @return [String] The replaced content

# File lib/guard/entangle/entangler.rb, line 65
def replace(content, file, path)
  name = file.sub '//=', ''
  stripped = name.strip
  file = "#{path}/#{stripped}"

  if File.exists?(file) && File.readable?(file)
    insert = File.open(file, 'rb')
    insert_content = insert.read
    pn = Pathname.new(insert)
    insert = convert_file(insert_content, pn.dirname)
    content.gsub! "//=#{name}", insert_content
  else
    content.gsub! "// #{name}: Does not exist or isn't readable!"
  end
  content
end