xcsv 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4ff9165c5564192166cd4dba6c141e7cbec377d7e89b1e83aefeeed16a5f14e1
4
- data.tar.gz: a8d607ec64710a73488358e07481f99a62d12f2fb09fe9747b44893ebc6c6860
3
+ metadata.gz: 42eb140ee193e61a54d51f784eb2e793bcb4811892344b1edb3e0a0146eb92b1
4
+ data.tar.gz: cdd5a40fa9e4faf7586bae3c8d42689cadfc6f969cbfe6e384c65c687a1228a8
5
5
  SHA512:
6
- metadata.gz: 78a1b77ae1f099e37966c93fca1669feb1c02bdb0f8e760bd1b8db36b9484eb3180041f2b704bf7dd5ba948bf6f25683b7924c27bb358911a8bf534463711a93
7
- data.tar.gz: f3bb8a94f289faf706a65576f7d51b36f2c13504c76dd874813e34ee1572217ab3f5c9be2ba8db9e7381108dfdad95734d93960632dd08c6f4a82f1d39dce5ca
6
+ metadata.gz: 21f528621774e3da62632ab9b5669764270cce4ef6aed37d81b0d6630b5b56a1569a919da2755f19e246718e92840747b3e1b4032cc918d6d029fa08ab6b9575
7
+ data.tar.gz: 70a4c13f561575d12d48e0a40e2691cd6485f5b0d5d8152c2e6e6ab6b33eed9343f80f4736b0529dbc8c72f1b27dd422783c82072b069241bcb0dd84528ab003
data/.gitignore CHANGED
@@ -1,2 +1,5 @@
1
1
  /target
2
2
  *.so
3
+ /*.gem
4
+ /.bundle
5
+ /vendor/bundle
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- xcsv (0.1.0)
4
+ xcsv (0.2.0)
5
5
  helix_runtime (= 0.7.5)
6
6
 
7
7
  GEM
@@ -34,9 +34,9 @@ PLATFORMS
34
34
 
35
35
  DEPENDENCIES
36
36
  bundler (~> 1.17)
37
- xcsv!
38
37
  rake (~> 12.3)
39
38
  rspec (~> 3.8)
39
+ xcsv!
40
40
 
41
41
  BUNDLED WITH
42
42
  1.17.1
data/README.md CHANGED
@@ -8,15 +8,14 @@ Fast CSV reader based on [Rust CSV crate](https://docs.rs/csv/1.0.2/csv/)
8
8
 
9
9
  Don't miss this message:
10
10
 
11
- ```
12
- Rust is installed now. Great!
11
+ ```
12
+ Rust is installed now. Great!
13
13
 
14
- To get started you need Cargo's bin directory ($HOME/.cargo/bin) in your PATH
15
- environment variable. Next time you log in this will be done automatically.
14
+ To get started you need Cargo's bin directory ($HOME/.cargo/bin) in your PATH
15
+ environment variable. Next time you log in this will be done automatically.
16
16
 
17
- To configure your current shell run source $HOME/.cargo/env
18
-
19
- ```
17
+ To configure your current shell run source $HOME/.cargo/env
18
+ ```
20
19
 
21
20
  2. `gem install xcsv`
22
21
 
@@ -34,7 +33,7 @@ end
34
33
  csv_reader = XSV.new("foo.csv")
35
34
  csv_reader.take(10).to_a #=> [[col1, ...], [col1, ...], ...]
36
35
 
37
- # While loop
36
+ # while loop
38
37
  csv_reader = XSV.new("bar.csv")
39
38
  while (rec = csv_reader.next) do
40
39
  rec #=> [col1, col2, col3, ...]
@@ -58,7 +57,7 @@ end
58
57
 
59
58
  # XCSV
60
59
  csv_reader = XCSV.new('sample.csv')
61
- while (l = csv_reader.next) do
60
+ while (rec = csv_reader.next) do
62
61
  end
63
62
 
64
63
  # CSV
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "xcsv"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/lib/xcsv.rb CHANGED
@@ -1,12 +1,23 @@
1
1
  require 'helix_runtime'
2
2
  require 'xcsv/native'
3
+ require 'xcsv/version'
3
4
 
4
5
  class XCSV
5
6
  include Enumerable
6
7
 
8
+ def self.open(path)
9
+ xcsv = new(path)
10
+ xcsv.open
11
+ begin
12
+ yield xcsv
13
+ ensure
14
+ xcsv.close
15
+ end
16
+ end
17
+
7
18
  def each
8
- while (r = self.next) do
9
- yield r
19
+ while (rec = self.next) do
20
+ yield rec
10
21
  end
11
22
  end
12
23
  end
data/lib/xcsv/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class XCSV
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/src/lib.rs CHANGED
@@ -12,14 +12,10 @@ use std::ops::{Deref, DerefMut};
12
12
  use flate2::read::GzDecoder;
13
13
  use regex::Regex;
14
14
 
15
- use helix::{FromRuby, CheckResult};
16
- use helix::sys::{VALUE};
17
-
18
15
  type CSVIterType = Iterator<Item=Result<csv::StringRecord, csv::Error>>;
19
16
 
20
17
  struct CSVIter {
21
18
  iter: Box<CSVIterType>,
22
- path: String,
23
19
  }
24
20
 
25
21
  impl Deref for CSVIter {
@@ -38,7 +34,7 @@ impl DerefMut for CSVIter {
38
34
 
39
35
  impl std::fmt::Debug for CSVIter {
40
36
  fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
41
- write!(f, "CSVIter: {}", self.path)
37
+ write!(f, "CSVIter")
42
38
  }
43
39
  }
44
40
 
@@ -48,61 +44,62 @@ impl Clone for CSVIter {
48
44
  }
49
45
  }
50
46
 
51
- impl FromRuby for CSVIter {
52
- type Checked = CSVIter;
53
-
54
- fn from_ruby(value: VALUE) -> CheckResult<CSVIter> {
55
- let checked_path = String::from_ruby(value)?;
56
-
57
- let path = String::from_checked(checked_path);
58
-
59
- let gz_regex = Regex::new("\\.gz\\z").unwrap();
47
+ ruby! {
48
+ class XCSV {
49
+ struct {
50
+ path: String,
51
+ iter: Option<CSVIter>,
52
+ }
60
53
 
61
- let buf_reader =
62
- match File::open(path.clone()) {
63
- Ok(f) => BufReader::new(f),
64
- Err(e) => raise!(format!("Error while opening file: {}", e)),
65
- };
54
+ def initialize(helix, path: String) {
55
+ XCSV { helix, path, iter: None }
56
+ }
66
57
 
67
- let gz_reader: Box<Read> =
68
- if gz_regex.is_match(&path) {
69
- Box::new(GzDecoder::new(buf_reader))
70
- } else {
71
- Box::new(buf_reader)
72
- };
58
+ def open(&mut self) -> Result<(), helix::Error> {
59
+ self.iter = None;
73
60
 
74
- let csv_reader =
75
- csv::ReaderBuilder::new()
76
- .has_headers(false)
77
- .from_reader(gz_reader);
61
+ let gz_regex = Regex::new("\\.gz\\z").unwrap();
78
62
 
79
- Ok(CSVIter{iter: Box::new(csv_reader.into_records()), path: path})
80
- }
63
+ let buf_reader =
64
+ match File::open(&self.path) {
65
+ Ok(f) => BufReader::new(f),
66
+ Err(e) => raise!(e.to_string()),
67
+ };
81
68
 
82
- fn from_checked(checked: CSVIter) -> CSVIter {
83
- checked
84
- }
85
- }
69
+ let gz_reader: Box<Read> =
70
+ if gz_regex.is_match(&self.path) {
71
+ Box::new(GzDecoder::new(buf_reader))
72
+ } else {
73
+ Box::new(buf_reader)
74
+ };
86
75
 
87
- ruby! {
88
- class XCSV {
89
- struct {
90
- iter: CSVIter,
91
- }
76
+ let csv_reader =
77
+ csv::ReaderBuilder::new()
78
+ .has_headers(false)
79
+ .from_reader(gz_reader);
92
80
 
93
- def initialize(helix, iter: CSVIter) {
94
- XCSV { helix, iter }
81
+ self.iter = Some(CSVIter{iter: Box::new(csv_reader.into_records())});
82
+ Ok(())
95
83
  }
96
84
 
97
85
  def next(&mut self) -> Result<Option<Vec<String>>, helix::Error> {
98
- match self.iter.next() {
99
- Some(Ok(record)) =>
100
- Ok(Some(record.iter().map(|s| s.to_string()).collect())),
101
- Some(Err(e)) =>
102
- raise!(e.to_string()),
86
+ match self.iter {
87
+ Some(ref mut iter) =>
88
+ match iter.next() {
89
+ Some(Ok(record)) =>
90
+ Ok(Some(record.iter().map(|s| s.to_string()).collect())),
91
+ Some(Err(e)) =>
92
+ raise!(e.to_string()),
93
+ None =>
94
+ Ok(None)
95
+ }
103
96
  None =>
104
- Ok(None)
97
+ raise!("closed file")
105
98
  }
106
99
  }
100
+
101
+ def close(&mut self) -> () {
102
+ self.iter = None
103
+ }
107
104
  }
108
105
  }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xcsv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Moroz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-17 00:00:00.000000000 Z
11
+ date: 2018-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: helix_runtime
@@ -80,6 +80,7 @@ files:
80
80
  - Gemfile.lock
81
81
  - README.md
82
82
  - Rakefile
83
+ - bin/console
83
84
  - extconfig.rb
84
85
  - lib/xcsv.rb
85
86
  - lib/xcsv/version.rb