Sha256: d102a77feea975da72bad92257ba05d7a0fc69c1a56bac9d177f17850f877bab

Contents?: true

Size: 1.44 KB

Versions: 15

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

require "versionaire"

module Gemsmith
  module Gem
    # Defines a gem requirement. This is a partial, cleaner implementation of the RubyGems
    # `Gem::Requirement` object.
    # :reek:MissingSafeMethod
    class Requirement
      def self.operators
        [">", ">=", "=", "!=", "<", "<=", "~>"]
      end

      def self.version_segments version
        String(version).split(Versionaire::DELIMITER).map(&:to_i)
      end

      def self.for object
        case object
          when String
            operator, version = object.split " "
            new operator: operator, raw_version: version
          else
            fail Errors::RequirementConversion,
                 %(Invalid string conversion. Use: "<operator> <version>".)
        end
      end

      attr_reader :operator, :raw_version, :version_segments, :version

      def initialize operator: ">=", raw_version: "0"
        @operator = operator
        @raw_version = raw_version
        @version_segments = self.class.version_segments raw_version
        @version = Versionaire::Version version_segments
        validate!
      end

      def to_s
        "#{operator} #{version}"
      end

      private

      def validate!
        operators = self.class.operators
        return true if operators.include? operator

        fail Errors::RequirementOperator,
             %(Invalid gem requirement operator. Use: #{operators.join ", "}.)
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
gemsmith-14.9.0 lib/gemsmith/gem/requirement.rb
gemsmith-14.8.0 lib/gemsmith/gem/requirement.rb
gemsmith-14.7.0 lib/gemsmith/gem/requirement.rb
gemsmith-14.6.0 lib/gemsmith/gem/requirement.rb
gemsmith-14.5.0 lib/gemsmith/gem/requirement.rb
gemsmith-14.4.0 lib/gemsmith/gem/requirement.rb
gemsmith-14.3.0 lib/gemsmith/gem/requirement.rb
gemsmith-14.2.0 lib/gemsmith/gem/requirement.rb
gemsmith-14.1.3 lib/gemsmith/gem/requirement.rb
gemsmith-14.1.2 lib/gemsmith/gem/requirement.rb
gemsmith-14.1.1 lib/gemsmith/gem/requirement.rb
gemsmith-14.1.0 lib/gemsmith/gem/requirement.rb
gemsmith-14.0.2 lib/gemsmith/gem/requirement.rb
gemsmith-14.0.1 lib/gemsmith/gem/requirement.rb
gemsmith-14.0.0 lib/gemsmith/gem/requirement.rb