Class: Plato::Repo

Inherits:
Object
  • Object
show all
Defined in:
lib/plato/repo.rb

Defined Under Namespace

Modules: HashCodec, RefCodec, Sanitize, StringCodec, TemplateCodec

Constant Summary collapse

CODEC_MAP =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

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.expand_path(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

#codecObject (readonly)

Returns the value of attribute codec.



6
7
8
# File 'lib/plato/repo.rb', line 6

def codec
  @codec
end

#rootObject (readonly)

Returns the value of attribute root.



6
7
8
# File 'lib/plato/repo.rb', line 6

def root
  @root
end

Instance Method Details

#allObject



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 expanded_path(path)

  until (path = File.dirname(path)) == '.'
    expanded = expanded_path(path)

    if Dir[File.join(expanded, '*')].empty?
      File.unlink expanded
    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 = expanded_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 = expanded_path(path)
  FileUtils.mkdir_p(File.dirname(path))
  @codec.write(path, data)
end