Class: FlatHash::Directory

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/flat_hash/directory.rb

Direct Known Subclasses

Repository

Instance Method Summary collapse

Constructor Details

#initialize(serialiser, path) ⇒ Directory

Returns a new instance of Directory.



18
19
20
# File 'lib/flat_hash/directory.rb', line 18

def initialize serialiser, path
  @serialiser, @path = serialiser, path
end

Instance Method Details

#<<(entry) ⇒ Object



34
35
36
# File 'lib/flat_hash/directory.rb', line 34

def << entry
  write entry.name, entry.content
end

#destroy(key) ⇒ Object



30
31
32
# File 'lib/flat_hash/directory.rb', line 30

def destroy key
  FileUtils.rm(File.join(@path,key))
end

#eachObject



22
23
24
25
26
27
28
# File 'lib/flat_hash/directory.rb', line 22

def each
  return unless File.exist?(@path)
  Dir.foreach(@path) do |path|
    basename = File.basename(path)
    yield self[basename] unless ['.','..'].include?(basename)
  end
end

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/flat_hash/directory.rb', line 38

def exist? key
  File.exist?(File.join(@path,key))
end

#read(key) ⇒ Object Also known as: []



42
43
44
# File 'lib/flat_hash/directory.rb', line 42

def read key
  File.open(File.join(@path,key)) {|io| add_key(@serialiser.read(io), key) }
end

#write(key, hash) ⇒ Object Also known as: []=



48
49
50
51
# File 'lib/flat_hash/directory.rb', line 48

def write key, hash
  FileUtils.mkdir_p @path
  File.open(File.join(@path,key),'w') {|io| @serialiser.write io, hash }
end