csv_store 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/csv_store.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'csv_store/version'
2
+ require 'csv_store/constants'
2
3
  require 'csv_store/database'
3
4
  require 'csv_store/file'
4
5
  require 'fastercsv'
@@ -0,0 +1,6 @@
1
+ module CSVStore
2
+ DEFAULT_OPTIONS = {
3
+ :col_sep => "\t",
4
+ :row_sep => "\n"
5
+ }
6
+ end
@@ -0,0 +1,55 @@
1
+ module CSVStore
2
+ class Database
3
+
4
+ attr_reader :base_path
5
+
6
+ def initialize(base_path)
7
+ @base_path = base_path
8
+ end
9
+
10
+ def path_for_file(path)
11
+ ::File.join(base_path, path)
12
+ end
13
+
14
+ def path_with_existing_dir(path)
15
+ current_path = path_for_file(path)
16
+ FileUtils.mkdir_p(::File.dirname(current_path))
17
+ current_path
18
+ end
19
+
20
+ def glob(glob_string, &block)
21
+ current_glob = path_for_file(glob_string)
22
+ Dir.glob(current_glob) do |path|
23
+ yield File.new(path, "rb")
24
+ end
25
+ end
26
+
27
+ def each_row(path)
28
+ file_path = path_for_file(path)
29
+ ::CSVStore::File.new(file_path, "rb").each do |row|
30
+ yield row
31
+ end
32
+ end
33
+
34
+ def append(path, row=nil, &block)
35
+ file_path = path_with_existing_dir(path)
36
+ if block_given?
37
+ ::CSVStore::File.new(file_path, "ab").file(&block)
38
+ elsif row
39
+ ::CSVStore::File.new(file_path, "ab").file do |f|
40
+ f << row
41
+ end
42
+ end
43
+ end
44
+
45
+ def file(path, &block)
46
+ ::CSVStore::File.new(file_path, "rb").file(&block)
47
+ end
48
+
49
+ def truncate_file(path, &block)
50
+ file_path = path_with_existing_dir(path)
51
+ ::CSVStore::File.new(file_path, "wb").file(&block)
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,50 @@
1
+ module CSVStore
2
+ class File
3
+
4
+ attr_reader :path, :mode
5
+
6
+ def initialize(path, mode)
7
+ @path = path
8
+ @mode = mode
9
+ end
10
+
11
+ def file(&block)
12
+ if block_given?
13
+ FasterCSV.open(path, mode, DEFAULT_OPTIONS) do |f|
14
+ yield f
15
+ end
16
+ else
17
+ FasterCSV.open(path, mode, DEFAULT_OPTIONS)
18
+ end
19
+ end
20
+
21
+ def each(&block)
22
+ file do |f|
23
+ f.each do |row|
24
+ yield row
25
+ end
26
+ end
27
+ end
28
+
29
+
30
+ def each_reverse_unique(&block)
31
+ each_unique(true, &block)
32
+ end
33
+
34
+ def each_unique(reverse = false, &block)
35
+
36
+ read_identifiers = []
37
+
38
+ file do |f|
39
+ (reverse ? f.read.reverse : f).each do |row|
40
+ row_identifier = row[0]
41
+ unless read_identifiers.include?(row_identifier)
42
+ yield row
43
+ end
44
+ read_identifiers << row_identifier
45
+ end
46
+ end
47
+ end
48
+
49
+ end
50
+ end
@@ -1,3 +1,3 @@
1
1
  module CSVStore
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
8
+ - 1
9
+ version: 0.1.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Karl Ravn
@@ -41,6 +41,7 @@ extra_rdoc_files: []
41
41
  files:
42
42
  - README.markdown
43
43
  - Rakefile
44
+ - lib/csv_store/constants.rb
44
45
  - lib/csv_store/database.rb
45
46
  - lib/csv_store/file.rb
46
47
  - lib/csv_store/version.rb