Class: Bundler::Interactive::GemfileEditor
- Inherits:
-
Object
- Object
- Bundler::Interactive::GemfileEditor
- Defined in:
- lib/bundler/interactive/gemfile_editor.rb
Overview
Rewrites a gem's version requirement in the Gemfile when the user picks a version beyond the declared constraint.
Bundler's own Injector can only append or remove entries, so this performs
a targeted, in-place edit of the existing gem "name", ... line, keeping
any groups, options, and trailing comments intact. When no editable line
is found it warns and returns false rather than risk corrupting the file.
Instance Method Summary collapse
-
#initialize(name, target_version, gemfile_path: Bundler.default_gemfile) ⇒ GemfileEditor
constructor
A new instance of GemfileEditor.
-
#new_requirement ⇒ Object
A pessimistic requirement that admits the chosen version, pinned to its major.minor (e.g. 8.0.1 -> "~> 8.0").
- #rewrite! ⇒ Object
Constructor Details
#initialize(name, target_version, gemfile_path: Bundler.default_gemfile) ⇒ GemfileEditor
Returns a new instance of GemfileEditor.
13 14 15 16 17 |
# File 'lib/bundler/interactive/gemfile_editor.rb', line 13 def initialize(name, target_version, gemfile_path: Bundler.default_gemfile) @name = name @target = Gem::Version.new(target_version.to_s) @gemfile_path = gemfile_path end |
Instance Method Details
#new_requirement ⇒ Object
A pessimistic requirement that admits the chosen version, pinned to its major.minor (e.g. 8.0.1 -> "~> 8.0").
37 38 39 |
# File 'lib/bundler/interactive/gemfile_editor.rb', line 37 def new_requirement "~> #{@target.segments.first(2).join('.')}" end |
#rewrite! ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/bundler/interactive/gemfile_editor.rb', line 19 def rewrite! content = File.read(@gemfile_path) updated = replace_requirement(content) if updated.nil? warn_unfound return false end File.write(@gemfile_path, updated) true rescue StandardError => e warn_failure(e) false end |