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.



14
15
16
17
18
19
# File 'lib/gemkeeper/manifest_reader.rb', line 14

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

Instance Attribute Details

#gemsObject (readonly)

Returns the value of attribute gems.



8
9
10
# File 'lib/gemkeeper/manifest_reader.rb', line 8

def gems
  @gems
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/gemkeeper/manifest_reader.rb', line 8

def path
  @path
end

#source_urlObject (readonly)

Returns the value of attribute source_url.



8
9
10
# File 'lib/gemkeeper/manifest_reader.rb', line 8

def source_url
  @source_url
end

Class Method Details

.load(path = DEFAULT_PATH) ⇒ Object



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

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.



41
42
43
44
45
46
47
48
49
# File 'lib/gemkeeper/manifest_reader.rb', line 41

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

#clear!Object



21
22
23
24
25
# File 'lib/gemkeeper/manifest_reader.rb', line 21

def clear!
  @gems = []
  @source_url = nil
  self
end

#find_by_name(name) ⇒ Object



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

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

#gem_namesObject



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

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

#repo_for(name) ⇒ Object



35
36
37
# File 'lib/gemkeeper/manifest_reader.rb', line 35

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

#save(path = @path) ⇒ Object



51
52
53
# File 'lib/gemkeeper/manifest_reader.rb', line 51

def save(path = @path)
  ManifestSerializer.save(path, gems: @gems, source_url: @source_url)
end