Class: DRG::Tasks::Gemfile

Inherits:
Object
  • Object
show all
Defined in:
lib/drg/tasks/gemfile.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = ::Bundler.default_gemfile) ⇒ Gemfile

Returns a new instance of Gemfile.



6
7
8
# File 'lib/drg/tasks/gemfile.rb', line 6

def initialize(file = ::Bundler.default_gemfile)
  @file = file
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



4
5
6
# File 'lib/drg/tasks/gemfile.rb', line 4

def file
  @file
end

Instance Method Details

#find_by_name(name) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/drg/tasks/gemfile.rb', line 25

def find_by_name(name)
  lines.each_with_index.each do |line, index|
    next if line =~ /:?path:?\s*(=>)?\s*/
    next if line =~ /:?git(hub)?:?\s*(=>)?\s*/
    return GemfileLine.new line, index, name if line =~ /gem\s*['"]#{name}["']/
  end
  nil
end

#linesObject



52
53
54
# File 'lib/drg/tasks/gemfile.rb', line 52

def lines
  @lines ||= File.readlines file
end

#remove_version(gem) ⇒ Object



19
20
21
22
23
# File 'lib/drg/tasks/gemfile.rb', line 19

def remove_version(gem)
  saved_lines << lines.clone!
  lines[gem] = gem.remove_version
  write
end

#rollbackObject



42
43
44
45
46
# File 'lib/drg/tasks/gemfile.rb', line 42

def rollback
  return if saved_lines.empty?
  lines.replace saved_lines.pop
  write
end

#saved_linesObject



48
49
50
# File 'lib/drg/tasks/gemfile.rb', line 48

def saved_lines
  @saved_lines ||= []
end

#update(gem, version) ⇒ Object

Saves a copy of @lines before changing it (note that #dup and #clone weren’t working)

Parameters:

  • gem (GemfileLine)
  • version (String)

    to update the gem line with



14
15
16
17
# File 'lib/drg/tasks/gemfile.rb', line 14

def update(gem, version)
  saved_lines << lines.clone!
  lines[gem] = gem.update version
end

#writeObject



34
35
36
37
38
39
40
# File 'lib/drg/tasks/gemfile.rb', line 34

def write
  File.open file, 'wb' do |f|
    lines.each do |line|
      f << line
    end
  end
end