Class: Bmg::Database::DataFolder

Inherits:
Bmg::Database show all
Defined in:
lib/bmg/database/data_folder.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  data_extensions: ['json', 'yml', 'yaml', 'csv']
}

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Bmg::Database

data_folder, sequel, #to_data_folder, #to_xlsx, xlsx

Constructor Details

#initialize(folder, options = {}) ⇒ DataFolder

Returns a new instance of DataFolder.



9
10
11
12
# File 'lib/bmg/database/data_folder.rb', line 9

def initialize(folder, options = {})
  @folder = Path(folder)
  @options = DEFAULT_OPTIONS.merge(options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &bl) ⇒ Object



14
15
16
17
18
# File 'lib/bmg/database/data_folder.rb', line 14

def method_missing(name, *args, &bl)
  return super(name, *args, &bl) unless args.empty? && bl.nil?
  raise NotSuchRelationError(name.to_s) unless file = find_file(name)
  read_file(file)
end

Class Method Details

.dump(database, path, ext = :json) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/bmg/database/data_folder.rb', line 32

def self.dump(database, path, ext = :json)
  path = Path(path)
  path.mkdir_p
  database.each_relation_pair do |name, rel|
    (path/"#{name}.#{ext}").write(rel.public_send(:"to_#{ext}"))
  end
  path
end

Instance Method Details

#each_relation_pairObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bmg/database/data_folder.rb', line 20

def each_relation_pair
  return to_enum(:each_relation_pair) unless block_given?

  @folder.glob('*') do |path|
    next unless path.file?
    next unless @options[:data_extensions].find {|ext|
      path.ext == ".#{ext}" || path.ext == ext
    }
    yield(path.basename.rm_ext.to_sym, read_file(path))
  end
end