Class: Etcdist::Reader
- Inherits:
-
Object
- Object
- Etcdist::Reader
- Defined in:
- lib/etcdist/reader.rb
Overview
Reads data from file system into directories, keys and values.
Instance Method Summary collapse
- #all_dirs ⇒ Object
-
#initialize(dir) ⇒ Reader
constructor
A new instance of Reader.
-
#read ⇒ Object
Returns a hash of type { directory => { key => val } }.
Constructor Details
#initialize(dir) ⇒ Reader
Returns a new instance of Reader.
8 9 10 |
# File 'lib/etcdist/reader.rb', line 8 def initialize(dir) @dir = dir end |
Instance Method Details
#all_dirs ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/etcdist/reader.rb', line 27 def all_dirs @dir = Pathname.new File.(@dir) root = Pathname.new '/' descendant_dirs = Dir[File.join(@dir, '**', '*')].select { |p| File.directory? p } relative_descendant_dirs = descendant_dirs.map { |d| Pathname.new(d).relative_path_from @dir } dirs = relative_descendant_dirs.map { |d| root.join(d).to_s } dirs.push '/' end |
#read ⇒ Object
Returns a hash of type { directory => { key => val } }
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/etcdist/reader.rb', line 13 def read @dir = File.(@dir) files = Dir[File.join(@dir, '**', '*')].reject { |p| File.directory? p } Log.info("found #{files.length} files in #{@dir}") files.reduce(Hash.new { |h, k| h[k] = {} }) do |h, f| directory = File.dirname(f).gsub(@dir, '') entries = Hash[read_non_blank_lines(f).map { |e| e.chomp.split('=', 2) }.select { |k, _| valid_key?(k) }] Log.debug("found #{entries.length} entries in #{f.gsub(@dir, '')}: #{entries}") h[directory].merge!(entries) h end end |