Class: PubGrub::Term

Inherits:
Object
  • Object
show all
Defined in:
lib/pub_grub/term.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(constraint, positive) ⇒ Term

Returns a new instance of Term.



7
8
9
10
11
# File 'lib/pub_grub/term.rb', line 7

def initialize(constraint, positive)
  @constraint = constraint
  @package = @constraint.package
  @positive = positive
end

Instance Attribute Details

#constraintObject (readonly)

Returns the value of attribute constraint.



5
6
7
# File 'lib/pub_grub/term.rb', line 5

def constraint
  @constraint
end

#packageObject (readonly)

Returns the value of attribute package.



5
6
7
# File 'lib/pub_grub/term.rb', line 5

def package
  @package
end

#positiveObject (readonly)

Returns the value of attribute positive.



5
6
7
# File 'lib/pub_grub/term.rb', line 5

def positive
  @positive
end

Instance Method Details

#difference(other) ⇒ Object



49
50
51
# File 'lib/pub_grub/term.rb', line 49

def difference(other)
  intersect(other.invert)
end

#empty?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/pub_grub/term.rb', line 99

def empty?
  @empty ||= normalized_constraint.empty?
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/pub_grub/term.rb', line 25

def eql?(other)
  positive == other.positive &&
    constraint.eql?(other.constraint)
end

#hashObject



21
22
23
# File 'lib/pub_grub/term.rb', line 21

def hash
  constraint.hash ^ positive.hash
end

#inspectObject



103
104
105
# File 'lib/pub_grub/term.rb', line 103

def inspect
  "#<#{self.class} #{self}>"
end

#intersect(other) ⇒ Object

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pub_grub/term.rb', line 35

def intersect(other)
  raise ArgumentError, "packages must match" if package != other.package

  if positive? && other.positive?
    self.class.new(constraint.intersect(other.constraint), true)
  elsif negative? && other.negative?
    self.class.new(constraint.union(other.constraint), false)
  else
    positive = positive? ? self : other
    negative = negative? ? self : other
    self.class.new(positive.constraint.intersect(negative.constraint.invert), true)
  end
end

#invertObject Also known as: inverse



30
31
32
# File 'lib/pub_grub/term.rb', line 30

def invert
  self.class.new(@constraint, !@positive)
end

#negative?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/pub_grub/term.rb', line 95

def negative?
  !positive?
end

#normalized_constraintObject



81
82
83
# File 'lib/pub_grub/term.rb', line 81

def normalized_constraint
  @normalized_constraint ||= positive ? constraint : constraint.invert
end

#positive?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/pub_grub/term.rb', line 91

def positive?
  @positive
end

#relation(other) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/pub_grub/term.rb', line 53

def relation(other)
  if positive? && other.positive?
    constraint.relation(other.constraint)
  elsif negative? && other.positive?
    if constraint.allows_all?(other.constraint)
      :disjoint
    else
      :overlap
    end
  elsif positive? && other.negative?
    if !other.constraint.allows_any?(constraint)
      :subset
    elsif other.constraint.allows_all?(constraint)
      :disjoint
    else
      :overlap
    end
  elsif negative? && other.negative?
    if constraint.allows_all?(other.constraint)
      :subset
    else
      :overlap
    end
  else
    raise
  end
end

#satisfies?(other) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


85
86
87
88
89
# File 'lib/pub_grub/term.rb', line 85

def satisfies?(other)
  raise ArgumentError, "packages must match" unless package == other.package

  relation(other) == :subset
end

#to_s(allow_every: false) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/pub_grub/term.rb', line 13

def to_s(allow_every: false)
  if positive
    @constraint.to_s(allow_every: allow_every)
  else
    "not #{@constraint}"
  end
end