Class: Dobby::FlagManager

Inherits:
Object
  • Object
show all
Defined in:
lib/dobby/flag_manager.rb

Overview

Simple interface for managing whitelist/allowed flags of defects.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, who) ⇒ FlagManager

Returns a new instance of FlagManager.

Parameters:

  • file (String)

    Path to YML file describing current flags, and where flags will be stored on #write

  • who (String)

    User or person associated with a flag entry



11
12
13
14
15
16
17
18
19
20
# File 'lib/dobby/flag_manager.rb', line 11

def initialize(file, who)
  flags = {
    whitelist: {},
    allowed: {}
  }

  @file = file
  @flags = flags.merge!(Psych.load_file(file))
  @who = who
end

Instance Attribute Details

#flagsObject (readonly)

Returns the value of attribute flags.



6
7
8
# File 'lib/dobby/flag_manager.rb', line 6

def flags
  @flags
end

Instance Method Details

#add(flag, id, ticket) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/dobby/flag_manager.rb', line 22

def add(flag, id, ticket)
  return false if @flags[flag].key?(id)

  @flags[flag][id] = {
    by: @who,
    on: Time.now.utc,
    ticket: ticket
  }
end

#builk_remove(flag, ids) ⇒ Object



49
50
51
# File 'lib/dobby/flag_manager.rb', line 49

def builk_remove(flag, ids)
  ids.each { |id| remove(flag, id) }
end

#bulk_add(flag, ids) ⇒ Object



45
46
47
# File 'lib/dobby/flag_manager.rb', line 45

def bulk_add(flag, ids)
  ids.each { |id| remove(flag, id) }
end

#bulk_move(src, dst, ids) ⇒ Object



53
54
55
# File 'lib/dobby/flag_manager.rb', line 53

def bulk_move(src, dst, ids)
  ids.each { |id| move(src, dst, id) }
end

#dumpObject



57
58
59
# File 'lib/dobby/flag_manager.rb', line 57

def dump
  @flags.to_yaml
end

#move(src, dst, id) ⇒ Object



38
39
40
41
42
43
# File 'lib/dobby/flag_manager.rb', line 38

def move(src, dst, id)
  ticket = @flags[src][id][:ticket]
  return false unless remove(src, id)

  add(dst, id, ticket)
end

#remove(flag, id) ⇒ Object



32
33
34
35
36
# File 'lib/dobby/flag_manager.rb', line 32

def remove(flag, id)
  return false unless @flags[flag].key?(id)

  @flags[flag].delete id
end

#writeObject



61
62
63
64
65
# File 'lib/dobby/flag_manager.rb', line 61

def write
  File.open(@file, 'w') do |f|
    f.write(@flags.to_yaml)
  end
end