Class: InvocaGems::Gemfile

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

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Gemfile

Returns a new instance of Gemfile.



2
3
4
5
# File 'lib/invoca_gems/gemfile.rb', line 2

def initialize(path)
  @path = path
  @lines = File.readlines(path).map(&:chomp)
end

Instance Method Details

#append(gem_name) ⇒ Object



16
17
18
# File 'lib/invoca_gems/gemfile.rb', line 16

def append(gem_name)
  InvocaGems::GemfileLine.new(self, "gem '#{gem_name}'", -1)
end

#find(gem_name, allow_missing: false) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/invoca_gems/gemfile.rb', line 7

def find(gem_name, allow_missing: false)
  if index = @lines.find_index { |l| l =~ /\A\s*gem\s+.*[\'\"]#{gem_name}[\'\"]/ }
    InvocaGems::GemfileLine.new(self, @lines[index], index)
  else
    allow_missing or InvocaGems::Presenter.stop_with_message "could not find gem #{gem_name}"
    nil
  end
end

#save!(gemspec) ⇒ Object



20
21
22
23
# File 'lib/invoca_gems/gemfile.rb', line 20

def save!(gemspec)
  update_gemspec(gemspec)
  File.open(@path, "w") { |f| f.write(@lines.join("\n") + "\n") }
end