Class: Dependabot::Bundler::FileUpdater::RequirementReplacer

Inherits:
Object
  • Object
show all
Defined in:
lib/dependabot/bundler/file_updater/requirement_replacer.rb

Defined Under Namespace

Classes: Rewriter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dependency:, file_type:, updated_requirement:, previous_requirement: nil, insert_if_bare: false) ⇒ RequirementReplacer

Returns a new instance of RequirementReplacer.


13
14
15
16
17
18
19
20
# File 'lib/dependabot/bundler/file_updater/requirement_replacer.rb', line 13

def initialize(dependency:, file_type:, updated_requirement:,
               previous_requirement: nil, insert_if_bare: false)
  @dependency           = dependency
  @file_type            = file_type
  @updated_requirement  = updated_requirement
  @previous_requirement = previous_requirement
  @insert_if_bare       = insert_if_bare
end

Instance Attribute Details

#dependencyObject (readonly)

Returns the value of attribute dependency


10
11
12
# File 'lib/dependabot/bundler/file_updater/requirement_replacer.rb', line 10

def dependency
  @dependency
end

#file_typeObject (readonly)

Returns the value of attribute file_type


10
11
12
# File 'lib/dependabot/bundler/file_updater/requirement_replacer.rb', line 10

def file_type
  @file_type
end

#previous_requirementObject (readonly)

Returns the value of attribute previous_requirement


10
11
12
# File 'lib/dependabot/bundler/file_updater/requirement_replacer.rb', line 10

def previous_requirement
  @previous_requirement
end

#updated_requirementObject (readonly)

Returns the value of attribute updated_requirement


10
11
12
# File 'lib/dependabot/bundler/file_updater/requirement_replacer.rb', line 10

def updated_requirement
  @updated_requirement
end

Instance Method Details

#rewrite(content) ⇒ Object


22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dependabot/bundler/file_updater/requirement_replacer.rb', line 22

def rewrite(content)
  buffer = Parser::Source::Buffer.new("(gemfile_content)")
  buffer.source = content
  ast = Parser::CurrentRuby.new.parse(buffer)

  updated_content = Rewriter.new(
    dependency: dependency,
    file_type: file_type,
    updated_requirement: updated_requirement,
    insert_if_bare: insert_if_bare?
  ).rewrite(buffer, ast)

  update_comment_spacing_if_required(content, updated_content)
end