smart_csv 0.0.6 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/{spec/.rspec → .rspec} +0 -1
- data/LICENSE.txt +22 -0
- data/README.md +44 -20
- data/lib/csv/table/table.rb +36 -7
- data/lib/smart_csv/version.rb +1 -1
- data/smart_csv.gemspec +14 -13
- data/spec/csv/row/row_spec.rb +1 -1
- data/spec/csv/table/table_spec.rb +21 -1
- metadata +7 -7
data/{spec/.rspec → .rspec}
RENAMED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Michał Szyma
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,35 +1,59 @@
|
|
1
1
|
# SmartCSV
|
2
2
|
|
3
|
+
[](http://badge.fury.io/rb/smart_csv)
|
4
|
+
|
3
5
|
Extend CSV class.
|
4
6
|
This gem need header.
|
5
7
|
|
6
8
|
## FOR EXAMPLE
|
7
9
|
|
8
|
-
|
9
|
-
|
10
|
-
|
10
|
+
```ruby
|
11
|
+
data = "id,firstname,lastname\n1,One,One\n2,Two,Two\n4,One,Four\n5,One,Five"
|
12
|
+
@data = CSV.parse(data, {:col_sep => ',', :headers => true}
|
13
|
+
```
|
11
14
|
## METHODS
|
12
15
|
|
13
16
|
* Create a new record
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
+
```ruby
|
18
|
+
@data.create("id"=> '13', "lastname" => '1992')
|
19
|
+
```
|
17
20
|
* Select records
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
+
```ruby
|
22
|
+
@data.where('firstname' => 'One').where_not('id' => '4')
|
23
|
+
```
|
21
24
|
* Update record
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
+
```ruby
|
26
|
+
@data.where('firstname' => 'One').first.update({"lastname" => "Seven", "wartosc" => 2012}) }
|
27
|
+
```
|
25
28
|
* Delete all records
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
+
```ruby
|
30
|
+
@data.delete_all
|
31
|
+
```
|
29
32
|
* Delete all records from scope of condition
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
+
```ruby
|
34
|
+
@data.where('firstname' => 'One').delete_all
|
35
|
+
```
|
33
36
|
* Select all records which have 'id' attribute greater than 2
|
34
|
-
|
35
|
-
|
37
|
+
```ruby
|
38
|
+
@data.gt('id', '2')
|
39
|
+
```
|
40
|
+
* Select all records which have 'id' attribute greater or equal 2
|
41
|
+
```ruby
|
42
|
+
@data.ge('id', '2')
|
43
|
+
```
|
44
|
+
* Select all records which have 'id' attribute less than 2
|
45
|
+
```ruby
|
46
|
+
@data.lt('id', '2')
|
47
|
+
```
|
48
|
+
* Select all records which have 'id' attribute less or equal 2
|
49
|
+
```ruby
|
50
|
+
@data.le('id', '2')
|
51
|
+
```
|
52
|
+
* Select all records which have 'firstname' attribute equal 'Tom'
|
53
|
+
```ruby
|
54
|
+
@data.eq('firstname', 'Tom')
|
55
|
+
```
|
56
|
+
* Select all records which have 'firstname' attribute not equal 'Tom'
|
57
|
+
```ruby
|
58
|
+
@data.ne('firstname', 'Tom')
|
59
|
+
```
|
data/lib/csv/table/table.rb
CHANGED
@@ -26,16 +26,36 @@ class CSV::Table
|
|
26
26
|
result
|
27
27
|
end
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
29
|
+
[
|
30
|
+
[:lt, :<],
|
31
|
+
[:le, :<=],
|
32
|
+
[:gt, :>],
|
33
|
+
[:ge, :>=],
|
34
|
+
].each do |name, operator|
|
35
|
+
define_method name do |attribute, value|
|
36
|
+
result = prepare_new_table
|
33
37
|
|
34
|
-
|
35
|
-
|
38
|
+
self.each do |record|
|
39
|
+
result << record if record[attribute].to_f.send(operator, value)
|
40
|
+
end
|
41
|
+
|
42
|
+
result
|
36
43
|
end
|
44
|
+
end
|
37
45
|
|
38
|
-
|
46
|
+
[
|
47
|
+
[:eq, :==],
|
48
|
+
[:ne, '!=']
|
49
|
+
].each do |name, operator|
|
50
|
+
define_method name do |attribute, value|
|
51
|
+
result = prepare_new_table
|
52
|
+
|
53
|
+
self.each do |record|
|
54
|
+
result << record if record[attribute].send(operator, value)
|
55
|
+
end
|
56
|
+
|
57
|
+
result
|
58
|
+
end
|
39
59
|
end
|
40
60
|
|
41
61
|
def create(*conditions)
|
@@ -66,4 +86,13 @@ class CSV::Table
|
|
66
86
|
end
|
67
87
|
end
|
68
88
|
end
|
89
|
+
|
90
|
+
private
|
91
|
+
|
92
|
+
def prepare_new_table
|
93
|
+
result = CSV::Table.new([])
|
94
|
+
result.ancestor = self.ancestor || self
|
95
|
+
result
|
96
|
+
end
|
97
|
+
|
69
98
|
end
|
data/lib/smart_csv/version.rb
CHANGED
data/smart_csv.gemspec
CHANGED
@@ -3,20 +3,21 @@ $:.push File.expand_path('../lib', __FILE__)
|
|
3
3
|
require 'smart_csv/version'
|
4
4
|
|
5
5
|
Gem::Specification.new do |gem|
|
6
|
-
gem.name
|
7
|
-
gem.version
|
8
|
-
gem.
|
9
|
-
gem.
|
10
|
-
gem.
|
11
|
-
gem.
|
12
|
-
gem.
|
13
|
-
gem.
|
6
|
+
gem.name = 'smart_csv'
|
7
|
+
gem.version = SmartCSV::VERSION
|
8
|
+
gem.platform = Gem::Platform::RUBY
|
9
|
+
gem.date = '2013-10-19'
|
10
|
+
gem.authors = ['Michał Szyma']
|
11
|
+
gem.email = ['raglub.ruby@gmail.com']
|
12
|
+
gem.homepage = "https://github.com/raglub/smart_csv"
|
13
|
+
gem.summary = %q{Extend CSV class.}
|
14
|
+
gem.description = %q{Extend CSV class. CSV can delete or select some records.}
|
14
15
|
|
15
|
-
gem.files
|
16
|
-
gem.executables
|
17
|
-
gem.test_files
|
16
|
+
gem.files = `git ls-files`.split($\)
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
18
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
19
|
|
19
|
-
gem.require_paths
|
20
|
+
gem.require_paths = ['lib']
|
20
21
|
|
21
|
-
gem.add_development_dependency 'rspec'
|
22
|
+
gem.add_development_dependency 'rspec', ">= 2.0.0"
|
22
23
|
end
|
data/spec/csv/row/row_spec.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'smart_csv'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe CSV::Table do
|
4
4
|
|
5
5
|
let(:csv_data) {"id,firstname,lastname\n1,One,One\n2,Two,Two\n4,Four,Four\n5,Five,Five\n6,One,One"}
|
6
6
|
|
@@ -22,6 +22,26 @@ describe "Check class CSV::Table" do
|
|
22
22
|
parse_data.gt('id', 2).size.should eq(3)
|
23
23
|
end
|
24
24
|
|
25
|
+
it "should select records with id greater than or equal to 2" do
|
26
|
+
parse_data.ge('id', 2).size.should eq(4)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should select records with id less than 2" do
|
30
|
+
parse_data.lt('id', 2).size.should eq(1)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should select records with id less than or equal to 2" do
|
34
|
+
parse_data.le('id', 2).size.should eq(2)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should select records with firstname equal to 'Four'" do
|
38
|
+
parse_data.eq('firstname', 'Four').size.should eq(1)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should select records with firstname not equal to 'Four'" do
|
42
|
+
parse_data.ne('firstname', 'Four').size.should eq(4)
|
43
|
+
end
|
44
|
+
|
25
45
|
it "should correct delete records" do
|
26
46
|
parse_data.size.should eql(5)
|
27
47
|
parse_data.delete_all
|
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.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-10-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -18,7 +18,7 @@ dependencies:
|
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version:
|
21
|
+
version: 2.0.0
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -26,7 +26,7 @@ dependencies:
|
|
26
26
|
requirements:
|
27
27
|
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version:
|
29
|
+
version: 2.0.0
|
30
30
|
description: Extend CSV class. CSV can delete or select some records.
|
31
31
|
email:
|
32
32
|
- raglub.ruby@gmail.com
|
@@ -35,7 +35,9 @@ extensions: []
|
|
35
35
|
extra_rdoc_files: []
|
36
36
|
files:
|
37
37
|
- .gitignore
|
38
|
+
- .rspec
|
38
39
|
- Gemfile
|
40
|
+
- LICENSE.txt
|
39
41
|
- README.md
|
40
42
|
- Rakefile
|
41
43
|
- lib/csv/csv.rb
|
@@ -44,7 +46,6 @@ files:
|
|
44
46
|
- lib/smart_csv.rb
|
45
47
|
- lib/smart_csv/version.rb
|
46
48
|
- smart_csv.gemspec
|
47
|
-
- spec/.rspec
|
48
49
|
- spec/csv/row/row_spec.rb
|
49
50
|
- spec/csv/table/table_spec.rb
|
50
51
|
- spec/spec_helper.rb
|
@@ -68,12 +69,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
68
69
|
version: '0'
|
69
70
|
requirements: []
|
70
71
|
rubyforge_project:
|
71
|
-
rubygems_version: 1.8.
|
72
|
+
rubygems_version: 1.8.25
|
72
73
|
signing_key:
|
73
74
|
specification_version: 3
|
74
75
|
summary: Extend CSV class.
|
75
76
|
test_files:
|
76
|
-
- spec/.rspec
|
77
77
|
- spec/csv/row/row_spec.rb
|
78
78
|
- spec/csv/table/table_spec.rb
|
79
79
|
- spec/spec_helper.rb
|