Class: Dependabot::Python::FileUpdater::RequirementReplacer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content:, dependency_name:, old_requirement:, new_requirement:) ⇒ RequirementReplacer

Returns a new instance of RequirementReplacer.



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

def initialize(content:, dependency_name:, old_requirement:,
               new_requirement:)
  @content         = content
  @dependency_name = dependency_name
  @old_requirement = old_requirement
  @new_requirement = new_requirement
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



11
12
13
# File 'lib/dependabot/python/file_updater/requirement_replacer.rb', line 11

def content
  @content
end

#dependency_nameObject (readonly)

Returns the value of attribute dependency_name.



11
12
13
# File 'lib/dependabot/python/file_updater/requirement_replacer.rb', line 11

def dependency_name
  @dependency_name
end

#new_requirementObject (readonly)

Returns the value of attribute new_requirement.



11
12
13
# File 'lib/dependabot/python/file_updater/requirement_replacer.rb', line 11

def new_requirement
  @new_requirement
end

#old_requirementObject (readonly)

Returns the value of attribute old_requirement.



11
12
13
# File 'lib/dependabot/python/file_updater/requirement_replacer.rb', line 11

def old_requirement
  @old_requirement
end

Instance Method Details

#updated_contentObject



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

def updated_content
  updated_content =
    content.gsub(original_declaration_replacement_regex) do |mtch|
      # If the "declaration" is setting an option (e.g., no-binary)
      # ignore it, since it isn't actually a declaration
      next mtch if Regexp.last_match.pre_match.match?(/--.*\z/)

      updated_dependency_declaration_string(
        old_requirement,
        new_requirement
      )
    end

  raise "Expected content to change!" if content == updated_content

  updated_content
end