Class: EasyCreds::Projects::Registry
- Inherits:
-
Object
- Object
- EasyCreds::Projects::Registry
- Defined in:
- lib/easy_creds/projects/registry.rb
Instance Attribute Summary collapse
-
#projects ⇒ Object
readonly
Returns the value of attribute projects.
Class Method Summary collapse
Instance Method Summary collapse
- #find_by_path(path) ⇒ Object
- #find_containing(cwd) ⇒ Object
-
#initialize(data) ⇒ Registry
constructor
A new instance of Registry.
- #prune_missing! ⇒ Object
- #register(name:, path:, kind:, github_url: nil, template: nil) ⇒ Object
- #remove(project) ⇒ Object
- #save! ⇒ Object
- #touch(project) ⇒ Object
Constructor Details
#initialize(data) ⇒ Registry
Returns a new instance of Registry.
41 42 43 44 |
# File 'lib/easy_creds/projects/registry.rb', line 41 def initialize(data) @data = data @projects = (data['projects'] || []).map { |h| build(h) } end |
Instance Attribute Details
#projects ⇒ Object (readonly)
Returns the value of attribute projects.
46 47 48 |
# File 'lib/easy_creds/projects/registry.rb', line 46 def projects @projects end |
Class Method Details
.load ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/easy_creds/projects/registry.rb', line 30 def self.load path = registry_path unless File.exist?(path) return new({ 'schema_version' => 1, 'projects' => [] }) end data = YAML.safe_load_file(path) || { 'schema_version' => 1, 'projects' => [] } new(data) end |
Instance Method Details
#find_by_path(path) ⇒ Object
48 49 50 51 |
# File 'lib/easy_creds/projects/registry.rb', line 48 def find_by_path(path) norm = File.(path.to_s) @projects.find { |p| File.(p.path) == norm } end |
#find_containing(cwd) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/easy_creds/projects/registry.rb', line 53 def find_containing(cwd) norm = File.(cwd.to_s) @projects .select { |p| norm == File.(p.path) || norm.start_with?(File.(p.path) + '/') } .max_by { |p| p.path.length } end |
#prune_missing! ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/easy_creds/projects/registry.rb', line 93 def prune_missing! gone = @projects.reject(&:exists_on_disk?) return [] if gone.empty? gone_paths = gone.map(&:path).to_set @projects.reject! { |p| gone_paths.include?(p.path) } save! gone end |
#register(name:, path:, kind:, github_url: nil, template: nil) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/easy_creds/projects/registry.rb', line 60 def register(name:, path:, kind:, github_url: nil, template: nil) norm = File.(path.to_s) existing = find_by_path(norm) if existing touch(existing) return existing end now = Time.now.utc proj = Project.new( name: name, path: norm, kind: kind.to_sym, github_url: github_url.presence, template: template&.to_sym, last_seen_at: now, registered_at: now ) @projects << proj save! proj end |
#remove(project) ⇒ Object
88 89 90 91 |
# File 'lib/easy_creds/projects/registry.rb', line 88 def remove(project) @projects.reject! { |p| p.path == project.path } save! end |
#save! ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/easy_creds/projects/registry.rb', line 103 def save! path = self.class.registry_path FileUtils.mkdir_p(File.dirname(path)) out = { 'schema_version' => 1, 'projects' => @projects.map(&:to_h_yaml) } File.write(path, out.to_yaml) FileUtils.chmod(0o600, path) end |
#touch(project) ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'lib/easy_creds/projects/registry.rb', line 79 def touch(project) idx = @projects.index { |p| p.path == project.path } return unless idx @projects[idx] = project.with(last_seen_at: Time.now.utc) save! @projects[idx] end |