class Gemfilings::Gemfile

Attributes

file[RW]
path[RW]
projects_directory[RW]

Public Class Methods

new(path) click to toggle source
# File lib/gemfilings/gemfile.rb, line 5
def initialize(path)
  self.path               = path
  self.file               = File.read(path)
  self.projects_directory = Pathname.new(ENV['PROJECTS_HOME'])
end

Public Instance Methods

toggle_local_path(gem_names) click to toggle source
# File lib/gemfilings/gemfile.rb, line 11
def toggle_local_path(gem_names)
  gem_names.each do |gem_name|
    gem_pattern      = /^(\s*gem\s+['"]#{gem_name}['"].*?)(,\s*)?(\s*)(#.*)?$/
    gem_path_pattern = /^(\s*gem\s+['"]#{gem_name}['"].*?)(, path: '([^']+)')/

    local_path = discovered_gem_path(gem_name)

    if file =~ gem_path_pattern
      file.gsub!(gem_path_pattern, '\1')
    else
      file.gsub!(gem_pattern, "\\1, path: '#{local_path}'\\2\\3\\4")
    end

    File.write(path, file)
  end
end

Protected Instance Methods

discovered_gem_path(gem_name) click to toggle source
# File lib/gemfilings/gemfile.rb, line 34
def discovered_gem_path(gem_name)
  Pathname.
    glob("#{projects_directory}/{,*/,*/*/}#{gem_name}", File::FNM_EXTGLOB).
    first
end