Class: FlatHash::Directory
- Includes:
- Enumerable
- Defined in:
- lib/flat_hash/directory.rb
Direct Known Subclasses
Instance Method Summary collapse
- #<<(entry) ⇒ Object
- #destroy(key) ⇒ Object
- #each ⇒ Object
- #exist?(key) ⇒ Boolean
-
#initialize(serialiser, path) ⇒ Directory
constructor
A new instance of Directory.
- #read(key) ⇒ Object (also: #[])
- #write(key, hash) ⇒ Object (also: #[]=)
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 |
#each ⇒ Object
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
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 |