Class: Plato::Repo

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

Defined Under Namespace

Classes: NotFound

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ Repo

Returns a new instance of Repo.



36
37
38
39
40
41
42
# File 'lib/plato/repo.rb', line 36

def initialize(root)
  unless root == File.expand_path(root)
    raise ArgumentError, "root is not an absolute path"
  end

  @root = root.sub(/\/+\Z/, '') #remove trailing slash(es)
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



34
35
36
# File 'lib/plato/repo.rb', line 34

def root
  @root
end

Instance Method Details

#allObject 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 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

#each(&block) ⇒ Object



69
70
71
# File 'lib/plato/repo.rb', line 69

def each(&block)
  all.each(&block)
end

#get(path) ⇒ Object

Raises:



44
45
46
47
48
49
# File 'lib/plato/repo.rb', line 44

def get(path)
  path = expanded_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(expanded_path(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