class LockfilePreserver::RubyVersion

Constants

NEW_LINE
REGEXP
RUBY_VERSION
VERSION_PATTERN

Attributes

original[R]
updated[R]

Public Class Methods

new(original, updated) click to toggle source
# File lib/lockfile_preserver/ruby_version.rb, line 3
def initialize(original, updated)
  @original = original
  @updated = updated
end

Public Instance Methods

keep() click to toggle source
# File lib/lockfile_preserver/ruby_version.rb, line 8
def keep
  if original.include? RUBY_VERSION
    keep_ruby_version
  else
    remove_ruby_version
  end
end

Private Instance Methods

add_ruby_version() click to toggle source

How RUBY VERSION added to lockfile: git.io/voWNN

# File lib/lockfile_preserver/ruby_version.rb, line 42
def add_ruby_version
  updated_lines = updated.lines

  # Find BUNDLED WITH line
  bundled_with_index = updated.lines.index { |line| line.include? "BUNDLED WITH" }

  # RUBY VERSION should be added before BUNDLED WITH
  add_ruby_version_index = bundled_with_index-1

  # Find where to add RUBY VERSION section
  while updated_lines[add_ruby_version_index] != "\n"
    add_ruby_version_index -= 1
  end

  # Add RUBY VERSION section
  updated_lines.insert(
    add_ruby_version_index, "\nRUBY VERSION\n", "   #{ruby_version}\n"
  )

  # Reconstruct lockfile
  updated_lines.join
end
keep_ruby_version() click to toggle source
# File lib/lockfile_preserver/ruby_version.rb, line 29
def keep_ruby_version
  if updated.include? RUBY_VERSION
    retains_ruby_version
  else
    add_ruby_version
  end
end
remove_ruby_version() click to toggle source
# File lib/lockfile_preserver/ruby_version.rb, line 65
def remove_ruby_version
  updated.sub(REGEXP, NEW_LINE)
end
retains_ruby_version() click to toggle source
# File lib/lockfile_preserver/ruby_version.rb, line 37
def retains_ruby_version
  updated.sub(REGEXP, ruby_version_section)
end
ruby_version() click to toggle source
# File lib/lockfile_preserver/ruby_version.rb, line 74
def ruby_version
  @_ruby_version ||= original.match(REGEXP)[:version]
end
ruby_version_section() click to toggle source
# File lib/lockfile_preserver/ruby_version.rb, line 69
def ruby_version_section
  "\n\nRUBY VERSION\n" \
  "   #{ruby_version}\n"
end