Class: Plato::Repo
- Inherits:
-
Object
- Object
- Plato::Repo
- Defined in:
- lib/plato/repo.rb
Defined Under Namespace
Classes: NotFound
Instance Attribute Summary collapse
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #all ⇒ Object (also: #to_h, #to_hash)
- #destroy(path) ⇒ Object
- #each(&block) ⇒ Object
- #get(path) ⇒ Object
-
#initialize(root) ⇒ Repo
constructor
A new instance of Repo.
- #set(path, data) ⇒ Object
- #set_all(hash) ⇒ Object
Constructor Details
Instance Attribute Details
#root ⇒ Object (readonly)
Returns the value of attribute root.
34 35 36 |
# File 'lib/plato/repo.rb', line 34 def root @root end |
Instance Method Details
#all ⇒ Object Also known as: to_h, to_hash
58 59 60 61 62 63 64 65 |
# File 'lib/plato/repo.rb', line 58 def all paths = Dir[File.join(root, '**/*')].select {|e| File.file? e } paths.inject({}) do |hash, path| relative = relative_path(path) hash.update relative => get(relative) end end |
#destroy(path) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/plato/repo.rb', line 77 def destroy(path) File.unlink (path) until (path = File.dirname(path)) == '.' = (path) if Dir[File.join(, '*')].empty? File.unlink else return end end end |
#each(&block) ⇒ Object
69 70 71 |
# File 'lib/plato/repo.rb', line 69 def each(&block) all.each(&block) end |
#get(path) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/plato/repo.rb', line 44 def get(path) path = (path) raise NotFound unless File.exist? path FileObject.new(:path => path) end |
#set(path, data) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/plato/repo.rb', line 51 def set(path, data) fo = data.respond_to?(:write_to) ? data : FileObject.new(:data => data) fo.write_to((path)) end |
#set_all(hash) ⇒ Object
73 74 75 |
# File 'lib/plato/repo.rb', line 73 def set_all(hash) hash.each {|path, data| set(path, data) } end |