Class: Richcss::VersionKit::Requirement

Inherits:
Object
  • Object
show all
Defined in:
lib/richcss/vendor/version_kit/lib/version_kit/requirement.rb

Overview

Describes a constraint on the acceptable elements of a list of versions. The only relevant method for this class is the #satisfied_by? method.

The optimistic requirement is deemed optimistic because the user if optimistic about the correct versioning of the software the requirement refers to.

Constant Summary collapse

OPERATORS =

Returns The operators supported by this class associated to the lambda used to evaluate them.

Returns:

  • (Hash {String=>Lambda})

    The operators supported by this class associated to the lambda used to evaluate them.

['=', '!=', '>', '<', '>=', '<=', '~>']

Instance Attribute Summary collapse

Object methods collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ Requirement

Returns a new instance of Requirement.

Parameters:

  • string (String)

    The string representation of the requirement.



25
26
27
28
29
30
31
32
# File 'lib/richcss/vendor/version_kit/lib/version_kit/requirement.rb', line 25

def initialize(string)
  operator, reference_version = parse_string(string)
  check_parsing(string, operator, reference_version)

  @operator = operator
  @reference_version = reference_version
  @reference = Version.new(reference_version)
end

Instance Attribute Details

#operatorString (readonly)

Returns The operator of the constraint.

Returns:

  • (String)

    The operator of the constraint.



12
13
14
# File 'lib/richcss/vendor/version_kit/lib/version_kit/requirement.rb', line 12

def operator
  @operator
end

#reference_versionString (readonly)

Returns The reference version of the operator.

Returns:

  • (String)

    The reference version of the operator.



16
17
18
# File 'lib/richcss/vendor/version_kit/lib/version_kit/requirement.rb', line 16

def reference_version
  @reference_version
end

Instance Method Details

#<=>(other) ⇒ Fixnum

Returns Useful for sorting a list of requirements.

Returns:

  • (Fixnum)

    Useful for sorting a list of requirements.



74
75
76
# File 'lib/richcss/vendor/version_kit/lib/version_kit/requirement.rb', line 74

def <=>(other)
  to_s <=> other.to_s
end

#==(other) ⇒ Object



84
85
86
87
# File 'lib/richcss/vendor/version_kit/lib/version_kit/requirement.rb', line 84

def ==(other)
  operator == other.operator &&
    reference_version == other.reference_version
end

#hashFixnum

Returns The hash of the instance.

Returns:

  • (Fixnum)

    The hash of the instance.



80
81
82
# File 'lib/richcss/vendor/version_kit/lib/version_kit/requirement.rb', line 80

def hash
  to_s.hash
end

#satisfied_by?(candidate_version) ⇒ Bool

rubocop:disable MethodLength, CyclomaticComplexity

Parameters:

  • candidate_version (String)

Returns:

  • (Bool)

    Whether a given version is accepted by the given requirement.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/richcss/vendor/version_kit/lib/version_kit/requirement.rb', line 41

def satisfied_by?(candidate_version)
  candidate = Version.new(candidate_version)
  reference = @reference

  case operator
  when '='  then candidate == reference
  when '!=' then candidate != reference
  when '>'  then candidate >  reference
  when '<'  then candidate <  reference
  when '>=' then candidate >= reference
  when '<=' then candidate <= reference
  when '~>'
    candidate >= reference && candidate < bumped_reference_version
  end
end

#to_sString

Returns the string representation of this class. The string is equivalent, but not strictly equal, to the one used on initialization.

Returns:

  • (String)

    the string representation of this class. The string is equivalent, but not strictly equal, to the one used on initialization.



68
69
70
# File 'lib/richcss/vendor/version_kit/lib/version_kit/requirement.rb', line 68

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