Class: Dobby::Database

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/dobby/database.rb

Overview

A set of Defects that knows how to update itself.

Defined Under Namespace

Classes: InitializationError

Instance Method Summary collapse

Constructor Details

#initialize(strategy) ⇒ Database

Returns a new instance of Database.



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

def initialize(strategy)
  @records = Hash.new { |h, k| h[k] = [] }
  @strategy = strategy
  raise InitializationError, 'Strategy did not update at initialize!' unless update

  # TODO: Make the flag path configurable
  file = File.join(File.expand_path(__dir__), 'flags.yml')
  flags = Psych.load_file(file)
  flags.each do |flag, defects|
    defects.each { |d| @records[d].flag = flag if @records.key?(d) }
  end
end

Instance Method Details

#[](key) ⇒ Array<Dobby::Defect>?

Parameters:

  • key (String)

    Package name

Returns:



34
35
36
# File 'lib/dobby/database.rb', line 34

def [](key)
  @records[key]
end

#contains?(key) ⇒ Boolean

Returns true if at least one Dobby::Defect exists for key.

Parameters:

  • key (String)

    Package name

Returns:

  • (Boolean)

    true if at least one Dobby::Defect exists for key



41
42
43
# File 'lib/dobby/database.rb', line 41

def contains?(key)
  @records.key? key
end

#defects_for(package) ⇒ Array<Dobby::Defect>

Parameters:

Returns:



26
27
28
# File 'lib/dobby/database.rb', line 26

def defects_for(package)
  @records[package.name] | @records[package.source]
end

#each {|Iterator| ... } ⇒ Object

Iterate over all Packages in the database and their associated Dobby::Defects

Yields:

  • (Iterator)


48
49
50
# File 'lib/dobby/database.rb', line 48

def each(&block)
  @records.each(&block)
end

#updatetrue

Returns if strategy reports it has new content.

Returns:

  • (true)

    if strategy reports it has new content



53
54
55
56
57
58
59
60
# File 'lib/dobby/database.rb', line 53

def update
  response = @strategy.update
  return false unless response.changed?

  @records.clear
  @records.merge!(response.content)
  true
end