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.

Parameters:

  • fb (Factbase) (defaults to: Fbe.fb)

    The factbase to use (defaults to Fbe.fb)



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.

Parameters:

  • where (String)

    The place, e.g. “github”

  • repo (Integer)

    ID of repository

  • issue (Integer)

    ID of issue (or array of them)



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fbe/tombstone.rb', line 29

def bury!(where, repo, issue)
  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?

Parameters:

  • where (String)

    The place, e.g. “github”

  • repo (Integer)

    ID of repository

  • issue (Integer)

    ID of issue (or array of them)

Returns:

  • (Boolean)

    True if it’s there



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/fbe/tombstone.rb', line 60

def has?(where, repo, issue)
  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