smart_csv 0.0.2 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/{README.rdoc → README.md} +7 -4
- data/lib/csv/csv.rb +1 -1
- data/lib/csv/table/table.rb +15 -5
- data/lib/smart_csv/version.rb +1 -1
- data/smart_csv.gemspec +16 -18
- data/spec/csv/table/table_spec.rb +26 -14
- metadata +14 -10
data/{README.rdoc → README.md}
RENAMED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Extend CSV class.
|
4
4
|
This gem need header.
|
5
|
-
|
5
|
+
|
6
6
|
== FOR EXAMPLE
|
7
7
|
|
8
8
|
data = "id,firstname,lastname\n1,One,One\n2,Two,Two\n4,One,Four\n5,One,Five"
|
@@ -11,14 +11,17 @@ This gem need header.
|
|
11
11
|
== METHODS
|
12
12
|
|
13
13
|
* Create a new record
|
14
|
-
@data.create(
|
14
|
+
@data.create("id"=> '13', "lastname" => '1992')
|
15
15
|
|
16
16
|
* Select records
|
17
|
-
@data.where(
|
17
|
+
@data.where('firstname' => 'One').where_not('id' => '4')
|
18
18
|
|
19
19
|
* Update record
|
20
|
-
@data.where(
|
20
|
+
@data.where('firstname' => 'One').first.update({"lastname" => "Seven", "wartosc" => 2012}) }
|
21
21
|
|
22
22
|
* Delete all records
|
23
23
|
@data.delete_all
|
24
24
|
|
25
|
+
* Delete all records from scope of condition
|
26
|
+
@data.where({'firstname' => 'One'}).delete_all
|
27
|
+
|
data/lib/csv/csv.rb
CHANGED
data/lib/csv/table/table.rb
CHANGED
@@ -2,18 +2,22 @@
|
|
2
2
|
require 'csv'
|
3
3
|
class CSV::Table
|
4
4
|
|
5
|
+
attr_accessor :ancestor
|
6
|
+
|
5
7
|
def where_not(*conditions)
|
6
8
|
result = CSV::Table.new([])
|
7
9
|
self.each do |record|
|
8
10
|
counter = 0
|
9
11
|
conditions.first.each {|key, value| counter += 1 unless record[key] == value.to_s}
|
10
|
-
result << record if counter == conditions.size
|
12
|
+
result.table << record if counter == conditions.size
|
11
13
|
end
|
12
14
|
result
|
13
15
|
end
|
14
16
|
|
15
17
|
def where(*conditions)
|
16
18
|
result = CSV::Table.new([])
|
19
|
+
result.ancestor = self.ancestor || self
|
20
|
+
|
17
21
|
self.each do |record|
|
18
22
|
counter = 0
|
19
23
|
conditions.first.each {|key, value| counter += 1 if record[key] == value.to_s}
|
@@ -37,11 +41,17 @@ class CSV::Table
|
|
37
41
|
puts "#{self.class} #{e.message}"
|
38
42
|
end
|
39
43
|
|
40
|
-
# Delete all record of table
|
44
|
+
# Delete all record of ancestor table
|
45
|
+
# it works with method #where
|
41
46
|
def delete_all
|
42
|
-
|
43
|
-
self.
|
47
|
+
if self.ancestor
|
48
|
+
self.table.each do |row|
|
49
|
+
self.ancestor.table.delete(row)
|
50
|
+
end
|
51
|
+
else
|
52
|
+
while not self.empty?
|
53
|
+
self.delete(0)
|
54
|
+
end
|
44
55
|
end
|
45
56
|
end
|
46
|
-
|
47
57
|
end
|
data/lib/smart_csv/version.rb
CHANGED
data/smart_csv.gemspec
CHANGED
@@ -1,24 +1,22 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
$:.push File.expand_path(
|
3
|
-
require
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
require 'smart_csv/version'
|
4
4
|
|
5
|
-
Gem::Specification.new do |
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.name = 'smart_csv'
|
7
|
+
gem.version = SmartCSV::VERSION
|
8
|
+
gem.date = '2013-09-20'
|
9
|
+
gem.authors = ['Michał Szyma']
|
10
|
+
gem.email = ['raglub.ruby@gmail.com']
|
11
|
+
gem.homepage = "https://github.com/raglub/smart_csv"
|
12
|
+
gem.summary = %q{Extend CSV class.}
|
13
|
+
gem.description = %q{Extend CSV class. CSV can delete or select some records.}
|
14
14
|
|
15
|
-
|
15
|
+
gem.files = `git ls-files`.split($\)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
16
18
|
|
17
|
-
|
18
|
-
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
gem.require_paths = ['lib']
|
19
20
|
|
20
|
-
|
21
|
-
s.require_paths = ["lib"]
|
22
|
-
|
23
|
-
s.add_dependency "csv"
|
21
|
+
gem.add_development_dependency 'rspec'
|
24
22
|
end
|
@@ -1,26 +1,38 @@
|
|
1
1
|
require 'smart_csv'
|
2
|
-
|
2
|
+
require 'pry'
|
3
3
|
describe "Check class CSV::Table" do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
|
5
|
+
let(:csv_data) {"id,firstname,lastname\n1,One,One\n2,Two,Two\n4,Four,Four\n5,Five,Five\n6,One,One"}
|
6
|
+
|
7
|
+
let(:parse_data) { CSV.parse(csv_data, {:col_sep => ',', :headers => true}) }
|
8
8
|
|
9
9
|
it "should correct create one record" do
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
10
|
+
parse_data.size.should eql(5)
|
11
|
+
parse_data.create("id"=> '13', "lastname" => '1992')
|
12
|
+
parse_data.size.should eql(6)
|
13
|
+
parse_data[-1]["id"].should eql('13')
|
14
14
|
end
|
15
15
|
|
16
16
|
it "should correct select records" do
|
17
|
-
|
18
|
-
|
17
|
+
parse_data.where('firstname' => 'One').size.should eql(2)
|
18
|
+
parse_data.where('firstname' => 'One').where_not('id' => '6').size.should eql(1)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should correct delete records" do
|
22
|
+
parse_data.size.should eql(5)
|
23
|
+
parse_data.delete_all
|
24
|
+
parse_data.size.should eql(0)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should correct delete records" do
|
28
|
+
parse_data.size.should eql(5)
|
29
|
+
parse_data.delete_all
|
30
|
+
parse_data.size.should eql(0)
|
19
31
|
end
|
20
32
|
|
21
33
|
it "should correct delete records" do
|
22
|
-
|
23
|
-
|
24
|
-
|
34
|
+
parse_data.size.should eql(5)
|
35
|
+
parse_data.where('firstname' => 'One').delete_all
|
36
|
+
parse_data.size.should eql(3)
|
25
37
|
end
|
26
38
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smart_csv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,17 +9,17 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-09-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
15
|
+
name: rspec
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
20
20
|
- !ruby/object:Gem::Version
|
21
21
|
version: '0'
|
22
|
-
type: :
|
22
|
+
type: :development
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
25
25
|
none: false
|
@@ -27,7 +27,7 @@ dependencies:
|
|
27
27
|
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '0'
|
30
|
-
description: Extend CSV class
|
30
|
+
description: Extend CSV class. CSV can delete or select some records.
|
31
31
|
email:
|
32
32
|
- raglub.ruby@gmail.com
|
33
33
|
executables: []
|
@@ -36,7 +36,7 @@ extra_rdoc_files: []
|
|
36
36
|
files:
|
37
37
|
- .gitignore
|
38
38
|
- Gemfile
|
39
|
-
- README.
|
39
|
+
- README.md
|
40
40
|
- Rakefile
|
41
41
|
- lib/csv/csv.rb
|
42
42
|
- lib/csv/row/row.rb
|
@@ -48,7 +48,7 @@ files:
|
|
48
48
|
- spec/csv/row/row_spec.rb
|
49
49
|
- spec/csv/table/table_spec.rb
|
50
50
|
- spec/spec_helper.rb
|
51
|
-
homepage:
|
51
|
+
homepage: https://github.com/raglub/smart_csv
|
52
52
|
licenses: []
|
53
53
|
post_install_message:
|
54
54
|
rdoc_options: []
|
@@ -67,9 +67,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
requirements: []
|
70
|
-
rubyforge_project:
|
70
|
+
rubyforge_project:
|
71
71
|
rubygems_version: 1.8.24
|
72
72
|
signing_key:
|
73
73
|
specification_version: 3
|
74
|
-
summary: Extend CSV class
|
75
|
-
test_files:
|
74
|
+
summary: Extend CSV class.
|
75
|
+
test_files:
|
76
|
+
- spec/.rspec
|
77
|
+
- spec/csv/row/row_spec.rb
|
78
|
+
- spec/csv/table/table_spec.rb
|
79
|
+
- spec/spec_helper.rb
|