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.
@@ -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({"id"=> '13', "lastname" => '1992'})
14
+ @data.create("id"=> '13', "lastname" => '1992')
15
15
 
16
16
  * Select records
17
- @data.where({'firstname' => 'One'}).where_not({'id' => '4'})
17
+ @data.where('firstname' => 'One').where_not('id' => '4')
18
18
 
19
19
  * Update record
20
- @data.where({'firstname' => 'One'}).first.update({"lastname" => "Seven", "wartosc" => 2012}) }
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
@@ -4,6 +4,6 @@ require 'csv'
4
4
  class CSV
5
5
  def self.example_data
6
6
  data = "id,firstname,lastname\n1,One,One\n2,Two,Two\n4,Four,Four\n5,Five,Five"
7
- CSV.parse(data, {:col_sep => ',', :headers => true})
7
+ CSV.parse(data, {:col_sep => ',', :headers => true})
8
8
  end
9
9
  end
@@ -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
- while not self.empty?
43
- self.delete(0)
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
@@ -1,3 +1,3 @@
1
1
  module SmartCSV
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.5"
3
3
  end
data/smart_csv.gemspec CHANGED
@@ -1,24 +1,22 @@
1
1
  # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
3
- require "smart_csv/version"
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'smart_csv/version'
4
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}
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
- s.rubyforge_project = "active_csv"
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
- s.files = `git ls-files`.split("\n")
18
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ gem.require_paths = ['lib']
19
20
 
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"
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
- 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
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
- @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')
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
- @data.where('firstname' => 'One').size.should eql(2)
18
- @data.where('firstname' => 'One').where_not('id' => '6').size.should eql(1)
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
- @data.size.should eql(5)
23
- @data.delete_all
24
- @data.size.should eql(0)
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.2
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: 2012-02-07 00:00:00.000000000 Z
12
+ date: 2013-09-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: csv
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: :runtime
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.rdoc
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: active_csv
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