Class: Gemkeeper::ManifestReader

Inherits:
Object
  • Object
show all
Defined in:
lib/gemkeeper/manifest_reader.rb

Overview

Decouples manifest format from sync/setup commands that need the eligible gem list.

Constant Summary collapse

DEFAULT_PATH =
File.expand_path("~/.config/gemkeeper/manifest.yml")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ ManifestReader

Returns a new instance of ManifestReader.



17
18
19
20
21
22
# File 'lib/gemkeeper/manifest_reader.rb', line 17

def initialize(path)
  @path = path
  @gems = []
  @source_url = nil
  parse_manifest if File.exist?(@path)
end

Instance Attribute Details

#gemsObject (readonly)

Returns the value of attribute gems.



11
12
13
# File 'lib/gemkeeper/manifest_reader.rb', line 11

def gems
  @gems
end

#source_urlObject (readonly)

Returns the value of attribute source_url.



11
12
13
# File 'lib/gemkeeper/manifest_reader.rb', line 11

def source_url
  @source_url
end

Class Method Details

.load(path = DEFAULT_PATH) ⇒ Object



13
14
15
# File 'lib/gemkeeper/manifest_reader.rb', line 13

def self.load(path = DEFAULT_PATH)
  new(path)
end

Instance Method Details

#add_mapping(name:, repo:) ⇒ Object

Adds a name→repo mapping. Idempotent for identical entries. Raises ManifestConflictError if the name exists with a different repo.



38
39
40
41
42
43
44
45
46
# File 'lib/gemkeeper/manifest_reader.rb', line 38

def add_mapping(name:, repo:)
  existing = find_by_name(name)
  if existing
    raise ManifestConflictError, conflict_message(name, existing[:repo], repo) if existing[:repo] != repo
  else
    @gems << { name:, repo: }
  end
  self
end

#find_by_name(name) ⇒ Object



28
29
30
# File 'lib/gemkeeper/manifest_reader.rb', line 28

def find_by_name(name)
  @gems.find { |gem_entry| gem_entry[:name] == name }
end

#gem_namesObject



24
25
26
# File 'lib/gemkeeper/manifest_reader.rb', line 24

def gem_names
  @gems.map { |gem_entry| gem_entry[:name] }
end

#repo_for(name) ⇒ Object



32
33
34
# File 'lib/gemkeeper/manifest_reader.rb', line 32

def repo_for(name)
  find_by_name(name)&.fetch(:repo)
end

#save(path = @path) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/gemkeeper/manifest_reader.rb', line 48

def save(path = @path)
  FileUtils.mkdir_p(File.dirname(path))
  data = {}
  data["source_url"] = @source_url if @source_url
  data["gems"] = @gems.map { |g| { "name" => g[:name], "repo" => g[:repo] } }
  File.write(path, data.to_yaml)
end