Sha256: 68aa2af9d2787d506cd60b155363f9751eeea018d57b840d673fba2a82a9f050

Contents?: true

Size: 1.66 KB

Versions: 5

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

gem("bundler")
require "bundler"

module SmartTodo
  module Events
    # An event that compare the version of a gem specified in your Gemfile.lock
    # with the expected version specifiers.
    class GemBump
      # @param gem_name [String]
      # @param requirements [Array] a list of version specifiers.
      #   The specifiers are the same as the one used in Gemfiles or Gemspecs
      #
      # @example Expecting a specific version
      #   GemBump.new('rails', ['6.0'])
      #
      # @example Expecting a version in the 5.x.x series
      #   GemBump.new('rails', ['> 5.2', '< 6'])
      def initialize(gem_name, requirements)
        @gem_name = gem_name
        @requirements = Gem::Requirement.new(requirements)
      end

      # @return [String, false]
      def met?
        return error_message if spec_set[@gem_name].empty?

        installed_version = spec_set[@gem_name].first.version
        if @requirements.satisfied_by?(installed_version)
          message(installed_version)
        else
          false
        end
      end

      # Error message send to Slack in case a gem couldn't be found
      #
      # @return [String]
      def error_message
        "The gem *#{@gem_name}* is not in your dependencies, I can't determine if your TODO is ready to be addressed."
      end

      # @return [String]
      def message(version_number)
        "The gem *#{@gem_name}* was updated to version *#{version_number}* and your TODO is now ready to be addressed."
      end

      private

      # @return [Bundler::SpecSet] an instance of Bundler::SpecSet
      def spec_set
        @spec_set ||= Bundler.load.specs
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
smart_todo-1.6.0 lib/smart_todo/events/gem_bump.rb
smart_todo-1.5.0 lib/smart_todo/events/gem_bump.rb
smart_todo-1.4.3 lib/smart_todo/events/gem_bump.rb
smart_todo-1.3.1 lib/smart_todo/events/gem_bump.rb
smart_todo-1.3.0 lib/smart_todo/events/gem_bump.rb