Class: Semvruler::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/semvruler/rule.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(constraints) ⇒ Rule

Returns a new instance of Rule.



12
13
14
# File 'lib/semvruler/rule.rb', line 12

def initialize(constraints)
  @constraints = Hash[constraints.map { |c| [c.to_s, c] }]
end

Class Method Details

.parse(value) ⇒ Object



6
7
8
9
# File 'lib/semvruler/rule.rb', line 6

def parse(value)
  constraints = Array(Constraint.parse(value, safe: false))
  new(constraints)
end

Instance Method Details

#[](idx) ⇒ Object



16
17
18
# File 'lib/semvruler/rule.rb', line 16

def [](idx)
  constraints[idx]
end

#add(value) ⇒ Object



24
25
26
27
# File 'lib/semvruler/rule.rb', line 24

def add(value)
  constraint = Constraint.parse(value)
  constraints[constraint.to_s] = constraint
end

#match?(version) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/semvruler/rule.rb', line 39

def match?(version)
  constraints.values.all? { |c| c.match?(version) }
end

#merge(other) ⇒ Object



34
35
36
37
# File 'lib/semvruler/rule.rb', line 34

def merge(other)
  new_constraints = [*constraints.values, *other.constraints.values]
  self.class.new(new_constraints)
end

#remove(value) ⇒ Object



29
30
31
32
# File 'lib/semvruler/rule.rb', line 29

def remove(value)
  constraint = Constraint.parse(value)
  constraints.delete(constraint.to_s)
end

#sizeObject



20
21
22
# File 'lib/semvruler/rule.rb', line 20

def size
  constraints.size
end

#to_procObject



43
44
45
# File 'lib/semvruler/rule.rb', line 43

def to_proc
  ->(version) { match?(version) }
end