Class: CSVStore::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_store/database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_path) ⇒ Database

Returns a new instance of Database.



6
7
8
# File 'lib/csv_store/database.rb', line 6

def initialize(base_path)
  @base_path = base_path
end

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



4
5
6
# File 'lib/csv_store/database.rb', line 4

def base_path
  @base_path
end

Instance Method Details

#append(path, row = nil, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/csv_store/database.rb', line 34

def append(path, row=nil, &block)
  file_path = path_with_existing_dir(path)
  if block_given?
    ::CSVStore::File.new(file_path, "ab").file(&block)
  elsif row
    ::CSVStore::File.new(file_path, "ab").file do |f|
      f << row
    end
  end
end

#each_row(path) ⇒ Object



27
28
29
30
31
32
# File 'lib/csv_store/database.rb', line 27

def each_row(path)
  file_path = path_for_file(path)
  ::CSVStore::File.new(file_path, "rb").each do |row|
    yield row
  end
end

#file(path, &block) ⇒ Object



45
46
47
# File 'lib/csv_store/database.rb', line 45

def file(path, &block)
  ::CSVStore::File.new(file_path, "rb").file(&block)
end

#glob(glob_string, &block) ⇒ Object



20
21
22
23
24
25
# File 'lib/csv_store/database.rb', line 20

def glob(glob_string, &block)
  current_glob = path_for_file(glob_string)
  Dir.glob(current_glob) do |path|
    yield File.new(path, "rb")
  end
end

#path_for_file(path) ⇒ Object



10
11
12
# File 'lib/csv_store/database.rb', line 10

def path_for_file(path)
  ::File.join(base_path, path)
end

#path_with_existing_dir(path) ⇒ Object



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

def path_with_existing_dir(path)
  current_path = path_for_file(path)
  FileUtils.mkdir_p(::File.dirname(current_path))
  current_path
end

#truncate_file(path, &block) ⇒ Object



49
50
51
52
# File 'lib/csv_store/database.rb', line 49

def truncate_file(path, &block)
  file_path = path_with_existing_dir(path)
  ::CSVStore::File.new(file_path, "wb").file(&block)
end