Class: Kuby::Utils::SemVer::Constraint

Inherits:
Object
  • Object
show all
Defined in:
lib/kuby/utils/sem_ver/constraint.rb

Constant Summary collapse

OPERATOR_MAP =
{
  '='  => :eq,
  '>'  => :gt,
  '>=' => :gteq,
  '<'  => :lt,
  '<=' => :lteq,
  '~>' => :waka
}
OPERATOR_INVERSE =
OPERATOR_MAP.invert.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operator, version) ⇒ Constraint

Returns a new instance of Constraint.



25
26
27
28
# File 'lib/kuby/utils/sem_ver/constraint.rb', line 25

def initialize(operator, version)
  @operator = operator
  @version = version
end

Instance Attribute Details

#operatorObject (readonly)

Returns the value of attribute operator.



18
19
20
# File 'lib/kuby/utils/sem_ver/constraint.rb', line 18

def operator
  @operator
end

#versionObject (readonly)

Returns the value of attribute version.



18
19
20
# File 'lib/kuby/utils/sem_ver/constraint.rb', line 18

def version
  @version
end

Class Method Details

.parse(str) ⇒ Object



20
21
22
23
# File 'lib/kuby/utils/sem_ver/constraint.rb', line 20

def self.parse(str)
  op, ver = str.split(' ')
  new(OPERATOR_MAP.fetch(op), Version.parse(ver, default: nil))
end

Instance Method Details

#satisfied_by?(other_version) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/kuby/utils/sem_ver/constraint.rb', line 34

def satisfied_by?(other_version)
  case operator
    when :waka
      arr = version.to_a
      other_arr = other_version.to_a

      arr.each_with_index do |digit, idx|
        break unless digit

        next_digit = arr[idx + 1]

        if next_digit
          return false if other_arr[idx] != digit
        else
          return false if other_arr[idx] < digit
        end
      end

      true
    when :eq
      other_version == version
    when :gt
      other_version > version
    when :gteq
      other_version >= version
    when :lt
      other_version < version
    when :lteq
      other_version <= version
  end
end

#to_sObject



30
31
32
# File 'lib/kuby/utils/sem_ver/constraint.rb', line 30

def to_s
  @str ||= "#{OPERATOR_INVERSE[operator]} #{version}"
end