Class: DevArchive::Repositories::FileSystem
- Inherits:
-
Object
- Object
- DevArchive::Repositories::FileSystem
- Defined in:
- lib/dev_archive/repositories.rb
Constant Summary collapse
- SCHEMA =
"0"
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #add(message) ⇒ Object
- #delete(id) ⇒ Object
-
#initialize(path:) ⇒ FileSystem
constructor
A new instance of FileSystem.
- #list ⇒ Object
- #restore(id) ⇒ Object
- #show(id) ⇒ Object
Constructor Details
#initialize(path:) ⇒ FileSystem
Returns a new instance of FileSystem.
6 7 8 |
# File 'lib/dev_archive/repositories.rb', line 6 def initialize(path:) @path = path end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
5 6 7 |
# File 'lib/dev_archive/repositories.rb', line 5 def path @path end |
Instance Method Details
#add(message) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/dev_archive/repositories.rb', line 10 def add() id = SecureRandom.uuid = Time.now dir = mkdir(id) = Archives::Builder.call(dir: dir) if .fetch(:stores).empty? Cfg.shell.puts("no data to save") else = { __schema__: SCHEMA, id: id, message: , timestamp: .utc.iso8601, dir: dir, archive: } Cfg.shell.puts("writing metadata: #{.to_json}") File.write(File.join(dir, "metadata.json"), JSON.pretty_generate()) end end |
#delete(id) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/dev_archive/repositories.rb', line 57 def delete(id) dir = File.join(path, id) raise ArgumentError.new("unknown snapshot: #{id}") unless File.exist?(dir) FileUtils.rm_rf(dir) end |
#list ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/dev_archive/repositories.rb', line 40 def list Dir .glob(File.join(path, "**/metadata.json")) .map { || JSON.parse(File.read()) } .sort_by { || .fetch("timestamp") } .reverse .each do || Formatters::List.call() end end |
#restore(id) ⇒ Object
34 35 36 37 38 |
# File 'lib/dev_archive/repositories.rb', line 34 def restore(id) = JSON.parse(File.read(File.join(path, id, "metadata.json"))) Archives::Restore.call(.dig("archive")) end |
#show(id) ⇒ Object
51 52 53 54 55 |
# File 'lib/dev_archive/repositories.rb', line 51 def show(id) Formatters::Show.call( JSON.parse(File.read(File.join(path, id, "metadata.json"))) ) end |