Class: Fbe::Tombstone

Inherits:
Object
  • Object
show all
Defined in:
lib/fbe/tombstone.rb

Overview

Checks whether an issue is already under a tombstone.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024-2025 Zerocracy

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(fb: Fbe.fb, fid: '_id') ⇒ Tombstone

Ctor.



20
21
22
23
# File 'lib/fbe/tombstone.rb', line 20

def initialize(fb: Fbe.fb, fid: '_id')
  @fb = fb
  @fid = fid
end

Instance Method Details

#bury!(where, repo, issue) ⇒ Object

Put it there.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fbe/tombstone.rb', line 47

def bury!(where, repo, issue)
  raise 'The type of "where" is not String' unless where.is_a?(String)
  raise 'The type of "repo" is not Integer' unless repo.is_a?(Integer)
  raise 'The type of "issue" is neither Integer nor Array' unless issue.is_a?(Integer) || issue.is_a?(Array)
  f =
    Fbe.if_absent(fb: @fb, always: true) do |n|
      n.what = 'tombstone'
      n.where = where
      n.repository = repo
    end
  f.send(:"#{@fid}=", SecureRandom.random_number(99_999)) if f[@fid].nil?
  nn = f['issues']&.map { |ii| ii.split('-').map(&:to_i).then { |ii| ii.size == 1 ? ii << ii[0] : ii } } || []
  issue = [issue] unless issue.is_a?(Array)
  issue.each do |i|
    nn << [i, i]
  end
  merged =
    nn.sort.each_with_object([]) do |(a, b), merged|
      if !merged.empty? && merged[-1][0] <= a && a <= merged[-1][1] + 1
        merged[-1][1] = b if b > merged[-1][1]
      else
        merged << [a, b]
      end
    end
  Fbe.overwrite(
    f, 'issues', merged.map { |ii| ii[0] == ii[1] ? ii[0].to_s : "#{ii[0]}-#{ii[1]}" },
    fb: @fb, fid: @fid
  )
end

#has?(where, repo, issue) ⇒ Boolean

Is it there?



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/fbe/tombstone.rb', line 82

def has?(where, repo, issue)
  raise 'The type of "where" is not String' unless where.is_a?(String)
  raise 'The type of "repo" is not Integer' unless repo.is_a?(Integer)
  raise 'The type of "issue" is neither Integer nor Array' unless issue.is_a?(Integer) || issue.is_a?(Array)
  f = @fb.query(
    "(and (eq where '#{where}') (eq what 'tombstone') (eq repository #{repo}) (exists issues))"
  ).each.first
  return false if f.nil?
  issue = [issue] unless issue.is_a?(Array)
  issue.all? do |i|
    f['issues'].any? do |ii|
      a, b = ii.split('-').map(&:to_i)
      b.nil? ? a == i : (a..b).cover?(i)
    end
  end
end

#issues(where, repo) ⇒ Array<Integer>

See all issues in the tombstone, as array of numbers.



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fbe/tombstone.rb', line 29

def issues(where, repo)
  raise 'The type of "where" is not String' unless where.is_a?(String)
  raise 'The type of "repo" is not Integer' unless repo.is_a?(Integer)
  f = @fb.query(
    "(and (eq where '#{where}') (eq what 'tombstone') (eq repository #{repo}) (exists issues))"
  ).each.first
  return [] if f.nil?
  f['issues'].map do |ii|
    a, b = ii.split('-').map(&:to_i)
    b = a if b.nil?
    (a..b).map { |i| i }
  end.flatten
end