Class: Plato::Repo
- Inherits:
-
Object
- Object
- Plato::Repo
- Defined in:
- lib/plato/repo.rb
Defined Under Namespace
Modules: HashCodec, RefCodec, Sanitize, StringCodec, TemplateCodec
Constant Summary collapse
- CODEC_MAP =
{}
Instance Attribute Summary collapse
-
#codec ⇒ Object
readonly
Returns the value of attribute codec.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #all ⇒ Object
- #destroy(path) ⇒ Object
-
#initialize(root, codec = nil) ⇒ Repo
constructor
A new instance of Repo.
- #load(path) ⇒ Object
- #save(path, data) ⇒ Object
Constructor Details
#initialize(root, codec = nil) ⇒ Repo
Returns a new instance of Repo.
10 11 12 13 14 15 16 17 |
# File 'lib/plato/repo.rb', line 10 def initialize(root, codec = nil) unless root == File.(root) raise ArgumentError, "root is not an absolute path" end @codec = CODEC_MAP[codec] || StringCodec @root = root.sub(/\/+\Z/, '') #remove trailing slash(es) end |
Instance Attribute Details
#codec ⇒ Object (readonly)
Returns the value of attribute codec.
6 7 8 |
# File 'lib/plato/repo.rb', line 6 def codec @codec end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
6 7 8 |
# File 'lib/plato/repo.rb', line 6 def root @root end |
Instance Method Details
#all ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/plato/repo.rb', line 48 def all paths = Dir[File.join(root, '**/*')].select {|e| File.file? e } if block_given? paths = paths.select {|p| yield relative_path(p) } end paths.inject({}) do |hash, path| hash.update relative_path(path) => load(path) end end |
#destroy(path) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/plato/repo.rb', line 34 def destroy(path) File.unlink (path) until (path = File.dirname(path)) == '.' = (path) if Dir[File.join(, '*')].empty? File.unlink else return end end end |
#load(path) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/plato/repo.rb', line 19 def load(path) path = (path) if File.exist? path @codec.read(path) else raise Filestore::NotFound end end |
#save(path, data) ⇒ Object
28 29 30 31 32 |
# File 'lib/plato/repo.rb', line 28 def save(path, data) path = (path) FileUtils.mkdir_p(File.dirname(path)) @codec.write(path, data) end |