Class: Boxing::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/boxing/database.rb

Overview

The package and dependency mapping database

Since:

  • 0.1.0

Defined Under Namespace

Classes: DownloadFailed, UpdateFailed

Constant Summary collapse

URL =

Git URL of the ruby-boxing-db

Since:

  • 0.3.0

'https://github.com/elct9620/ruby-boxing-db.git'
USER_PATH =

Path to the user’s copy of ruby-boxing-db

Since:

  • 0.3.0

Pathname.new(Gem.user_home).join('.local/share/ruby-boxing-db')
DEFAULT_PATH =

Since:

  • 0.3.0

ENV.fetch('BOXING_DB', USER_PATH)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = DEFAULT_PATH) ⇒ Database

Initialize Database

Since:

  • 0.3.0



61
62
63
# File 'lib/boxing/database.rb', line 61

def initialize(path = DEFAULT_PATH)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Since:

  • 0.3.0



56
57
58
# File 'lib/boxing/database.rb', line 56

def path
  @path
end

Class Method Details

.download!(path = DEFAULT_PATH) ⇒ Object

Download Database

Raises:

Since:

  • 0.3.0



26
27
28
29
30
31
32
33
# File 'lib/boxing/database.rb', line 26

def download!(path = DEFAULT_PATH)
  command = %w[git clone --quiet]
  command << URL << path.to_s

  raise DownloadFailed, "failed to download #{URL} to #{path}" unless system(*command)

  new(path)
end

.exist?(path = DEFAULT_PATH) ⇒ TrueClass\FalseClass

Check for the database exists

Parameters:

  • path (String) (defaults to: DEFAULT_PATH)

Returns:

  • (TrueClass\FalseClass)

Since:

  • 0.3.0



19
20
21
# File 'lib/boxing/database.rb', line 19

def exist?(path = DEFAULT_PATH)
  File.directory?(path) && !(Dir.entries(path) - %w[. ..]).empty?
end

Instance Method Details

#each_package_path_for(name) {|path| ... } ⇒ Object

Enumerates over gems

Parameters:

  • name (String)

    the gem name

Yields:

  • (path)

    The given block will be passed each package path

Yield Parameters:

  • A (String)

    path to package ‘.yml` file

Since:

  • 0.1.0



111
112
113
# File 'lib/boxing/database.rb', line 111

def each_package_path_for(name, &block)
  Dir.glob(File.join(@path, 'gems', name, '*.yml'), &block)
end

#git?TrueClass|FalseClass

The Database is Git Repoistory

Returns:

  • (TrueClass|FalseClass)

Since:

  • 0.3.0



70
71
72
# File 'lib/boxing/database.rb', line 70

def git?
  File.directory?(File.join(@path, '.git'))
end

#package_for(name) {|path| ... } ⇒ Object

Find packages for rubygems

Parameters:

  • name (String)

    the gem name

Yields:

  • (path)

    The given block will be passed each package path

Yield Parameters:

Since:

  • 0.1.0



96
97
98
99
100
101
102
# File 'lib/boxing/database.rb', line 96

def package_for(name)
  return enum_for(__method__, name) unless block_given?

  each_package_path_for(name.to_s) do |path|
    yield Package.load(path)
  end
end

#update!Object

Update the database

Since:

  • 0.3.0



77
78
79
80
81
82
83
84
85
86
# File 'lib/boxing/database.rb', line 77

def update!
  return unless git?

  Dir.chdir(@path) do
    command = %w[git pull --quiet origin main]
    raise UpdateFailed, "failed to update #{@path}" unless system(*command)
  end

  true
end