smart_csv 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +5 -0
- data/README.rdoc +24 -0
- data/Rakefile +1 -0
- data/lib/csv/csv.rb +9 -0
- data/lib/csv/row/row.rb +17 -0
- data/lib/csv/table/table.rb +47 -0
- data/lib/smart_csv.rb +4 -0
- data/lib/smart_csv/version.rb +3 -0
- data/smart_csv.gemspec +24 -0
- data/spec/.rspec +3 -0
- data/spec/csv/row/row_spec.rb +15 -0
- data/spec/csv/table/table_spec.rb +26 -0
- data/spec/spec_helper.rb +8 -0
- metadata +75 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
== SmartCSV
|
2
|
+
|
3
|
+
Extend CSV class.
|
4
|
+
This gem need header.
|
5
|
+
|
6
|
+
== FOR EXAMPLE
|
7
|
+
|
8
|
+
data = "id,firstname,lastname\n1,One,One\n2,Two,Two\n4,One,Four\n5,One,Five"
|
9
|
+
@data = CSV.parse(data, {:col_sep => ',', :headers => true}
|
10
|
+
|
11
|
+
== METHODS
|
12
|
+
|
13
|
+
* Create a new record
|
14
|
+
@data.create({"id"=> '13', "lastname" => '1992'})
|
15
|
+
|
16
|
+
* Select records
|
17
|
+
@data.where({'firstname' => 'One'}).where_not({'id' => '4'})
|
18
|
+
|
19
|
+
* Update record
|
20
|
+
@data.where({'firstname' => 'One'}).first.update({"lastname" => "Seven", "wartosc" => 2012}) }
|
21
|
+
|
22
|
+
* Delete all records
|
23
|
+
@data.delete_all
|
24
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/csv/csv.rb
ADDED
data/lib/csv/row/row.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#coding: utf-8
|
2
|
+
require 'csv'
|
3
|
+
class CSV::Row
|
4
|
+
|
5
|
+
def update(conditions)
|
6
|
+
conditions.each do |key, value|
|
7
|
+
raise "In headers: '#{self.headers}' don't have key: '#{key}'" unless self.include?(key)
|
8
|
+
self[key] = value
|
9
|
+
end
|
10
|
+
rescue Exception => e
|
11
|
+
puts "#{self.class} #{e.message}"
|
12
|
+
end
|
13
|
+
|
14
|
+
def delete
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
#coding: utf-8
|
2
|
+
require 'csv'
|
3
|
+
class CSV::Table
|
4
|
+
|
5
|
+
def where_not(*conditions)
|
6
|
+
result = CSV::Table.new([])
|
7
|
+
self.each do |record|
|
8
|
+
counter = 0
|
9
|
+
conditions.first.each {|key, value| counter += 1 unless record[key] == value.to_s}
|
10
|
+
result << record if counter == conditions.size
|
11
|
+
end
|
12
|
+
result
|
13
|
+
end
|
14
|
+
|
15
|
+
def where(*conditions)
|
16
|
+
result = CSV::Table.new([])
|
17
|
+
self.each do |record|
|
18
|
+
counter = 0
|
19
|
+
conditions.first.each {|key, value| counter += 1 if record[key] == value.to_s}
|
20
|
+
result << record if counter == conditions.size
|
21
|
+
end
|
22
|
+
result
|
23
|
+
end
|
24
|
+
|
25
|
+
def create(*conditions)
|
26
|
+
headers = self.headers
|
27
|
+
row = headers.inject({}){|result, value| result[value] = nil; result}
|
28
|
+
conditions.first.each do |key, value|
|
29
|
+
raise "In headers: '#{headers}' don't have key: '#{key}'" unless headers.include?(key)
|
30
|
+
row[key] = value
|
31
|
+
end
|
32
|
+
values = headers.inject([]) {|result, key| result << row[key]; result}
|
33
|
+
record = CSV::Row.new(headers, values)
|
34
|
+
self << record
|
35
|
+
record
|
36
|
+
rescue Exception => e
|
37
|
+
puts "#{self.class} #{e.message}"
|
38
|
+
end
|
39
|
+
|
40
|
+
# Delete all record of table
|
41
|
+
def delete_all
|
42
|
+
while not self.empty?
|
43
|
+
self.delete(0)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
data/lib/smart_csv.rb
ADDED
data/smart_csv.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "smart_csv/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "smart_csv"
|
7
|
+
s.version = SmartCSV::VERSION
|
8
|
+
s.date = "2012-02-07"
|
9
|
+
s.authors = ["Michał Szyma"]
|
10
|
+
s.email = ["raglub.ruby@gmail.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{Extend CSV class}
|
13
|
+
s.description = %q{Extend CSV class}
|
14
|
+
|
15
|
+
s.rubyforge_project = "active_csv"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
|
20
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
21
|
+
s.require_paths = ["lib"]
|
22
|
+
|
23
|
+
s.add_dependency "csv"
|
24
|
+
end
|
data/spec/.rspec
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'smart_csv'
|
2
|
+
|
3
|
+
describe "Check class CSV::Row" do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
data = "id,firstname,lastname\n1,One,One\n2,Two,Two\n4,Four,Four\n5,Five,Five\n6,One,One"
|
7
|
+
@data = CSV.parse(data, {:col_sep => ',', :headers => true})
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should correct updete record" do
|
11
|
+
@data[2].update("lastname" => "Seven", "id" => "2012")
|
12
|
+
@data.where("id" => "2012").size.should eql(1)
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'smart_csv'
|
2
|
+
|
3
|
+
describe "Check class CSV::Table" do
|
4
|
+
before(:each) do
|
5
|
+
data = "id,firstname,lastname\n1,One,One\n2,Two,Two\n4,Four,Four\n5,Five,Five\n6,One,One"
|
6
|
+
@data = CSV.parse(data, {:col_sep => ',', :headers => true})
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should correct create one record" do
|
10
|
+
@data.size.should eql(5)
|
11
|
+
@data.create("id"=> '13', "lastname" => '1992')
|
12
|
+
@data.size.should eql(6)
|
13
|
+
@data[-1]["id"].should eql('13')
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should correct select records" do
|
17
|
+
@data.where('firstname' => 'One').size.should eql(2)
|
18
|
+
@data.where('firstname' => 'One').where_not('id' => '6').size.should eql(1)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should correct delete records" do
|
22
|
+
@data.size.should eql(5)
|
23
|
+
@data.delete_all
|
24
|
+
@data.size.should eql(0)
|
25
|
+
end
|
26
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: smart_csv
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Michał Szyma
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: csv
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: Extend CSV class
|
31
|
+
email:
|
32
|
+
- raglub.ruby@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- README.rdoc
|
40
|
+
- Rakefile
|
41
|
+
- lib/csv/csv.rb
|
42
|
+
- lib/csv/row/row.rb
|
43
|
+
- lib/csv/table/table.rb
|
44
|
+
- lib/smart_csv.rb
|
45
|
+
- lib/smart_csv/version.rb
|
46
|
+
- smart_csv.gemspec
|
47
|
+
- spec/.rspec
|
48
|
+
- spec/csv/row/row_spec.rb
|
49
|
+
- spec/csv/table/table_spec.rb
|
50
|
+
- spec/spec_helper.rb
|
51
|
+
homepage: ''
|
52
|
+
licenses: []
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options: []
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
requirements: []
|
70
|
+
rubyforge_project: active_csv
|
71
|
+
rubygems_version: 1.8.24
|
72
|
+
signing_key:
|
73
|
+
specification_version: 3
|
74
|
+
summary: Extend CSV class
|
75
|
+
test_files: []
|