smart_csv 0.0.7 → 0.0.9
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/.gitignore +1 -0
- data/.travis.yml +6 -0
- data/Gemfile +6 -1
- data/{LICENSE.txt → LICENSE} +1 -1
- data/README.md +68 -27
- data/Rakefile +9 -1
- data/lib/csv/table/table.rb +10 -3
- data/lib/smart_csv/version.rb +1 -1
- data/smart_csv.gemspec +6 -1
- data/spec/csv/row/row_spec.rb +1 -1
- data/spec/csv/table/table_spec.rb +30 -21
- data/spec/spec_helper.rb +3 -0
- metadata +38 -4
    
        data/.gitignore
    CHANGED
    
    
    
        data/.travis.yml
    ADDED
    
    
    
        data/Gemfile
    CHANGED
    
    
    
        data/{LICENSE.txt → LICENSE}
    RENAMED
    
    
    
        data/README.md
    CHANGED
    
    | @@ -1,59 +1,100 @@ | |
| 1 1 | 
             
            # SmartCSV
         | 
| 2 2 |  | 
| 3 3 | 
             
            [](http://badge.fury.io/rb/smart_csv)
         | 
| 4 | 
            +
            [](https://travis-ci.org/raglub/smart_csv)
         | 
| 5 | 
            +
            [](https://codeclimate.com/github/raglub/smart_csv)
         | 
| 6 | 
            +
            [](https://gemnasium.com/raglub/smart_csv)
         | 
| 7 | 
            +
            [](https://coveralls.io/r/raglub/smart_csv)
         | 
| 4 8 |  | 
| 5 | 
            -
            Extend CSV class.
         | 
| 6 | 
            -
            This gem need header.
         | 
| 9 | 
            +
            Extend CSV class. CSV can delete or select some records.
         | 
| 7 10 |  | 
| 8 | 
            -
             | 
| 11 | 
            +
            Compatible with:
         | 
| 12 | 
            +
            Ruby 1.9.2, 1.9.3, and 2.0.0
         | 
| 13 | 
            +
             | 
| 14 | 
            +
            # Getting started
         | 
| 15 | 
            +
             | 
| 16 | 
            +
            For Example we can use following data
         | 
| 9 17 |  | 
| 10 18 | 
             
            ```ruby
         | 
| 11 19 | 
             
            data = "id,firstname,lastname\n1,One,One\n2,Two,Two\n4,One,Four\n5,One,Five"
         | 
| 12 | 
            -
             | 
| 20 | 
            +
            csv = CSV.parse(data, {:col_sep => ',', :headers => true}
         | 
| 13 21 | 
             
            ```
         | 
| 14 | 
            -
            ## METHODS
         | 
| 15 22 |  | 
| 16 | 
            -
             | 
| 23 | 
            +
            And now we can:
         | 
| 24 | 
            +
             | 
| 25 | 
            +
            * create a new record
         | 
| 26 | 
            +
             | 
| 17 27 | 
             
            ```ruby
         | 
| 18 | 
            -
             | 
| 28 | 
            +
            csv.create("id"=> '13', "lastname" => '1992')
         | 
| 19 29 | 
             
            ```
         | 
| 20 | 
            -
             | 
| 30 | 
            +
             | 
| 31 | 
            +
            * select records
         | 
| 32 | 
            +
             | 
| 21 33 | 
             
            ```ruby
         | 
| 22 | 
            -
             | 
| 34 | 
            +
            csv.where('firstname' => 'One').where_not('id' => '4')
         | 
| 23 35 | 
             
            ```
         | 
| 24 | 
            -
            *  | 
| 36 | 
            +
            * select opposite records
         | 
| 37 | 
            +
             | 
| 25 38 | 
             
            ```ruby
         | 
| 26 | 
            -
             | 
| 39 | 
            +
            csv.not{where('firstname' => 'One')}
         | 
| 27 40 | 
             
            ```
         | 
| 28 | 
            -
             | 
| 41 | 
            +
             | 
| 42 | 
            +
            * update record
         | 
| 43 | 
            +
             | 
| 29 44 | 
             
            ```ruby
         | 
| 30 | 
            -
             | 
| 45 | 
            +
            csv.where('firstname' => 'One').first.update({"lastname" => "Seven", "wartosc" => 2012}) }
         | 
| 31 46 | 
             
            ```
         | 
| 32 | 
            -
             | 
| 47 | 
            +
             | 
| 48 | 
            +
            * delete all records
         | 
| 49 | 
            +
             | 
| 50 | 
            +
            ```ruby
         | 
| 51 | 
            +
            csv.delete_all
         | 
| 52 | 
            +
            ```
         | 
| 53 | 
            +
             | 
| 54 | 
            +
            * delete all records from scope of condition
         | 
| 55 | 
            +
             | 
| 33 56 | 
             
            ```ruby
         | 
| 34 | 
            -
             | 
| 57 | 
            +
            csv.where('firstname' => 'One').delete_all
         | 
| 35 58 | 
             
            ```
         | 
| 36 | 
            -
             | 
| 59 | 
            +
             | 
| 60 | 
            +
            * select all records which have 'id' attribute greater than 2
         | 
| 61 | 
            +
             | 
| 37 62 | 
             
            ```ruby
         | 
| 38 | 
            -
             | 
| 63 | 
            +
            csv.gt('id', '2')
         | 
| 39 64 | 
             
            ```
         | 
| 40 | 
            -
             | 
| 65 | 
            +
             | 
| 66 | 
            +
            * select all records which have 'id' attribute greater or equal 2
         | 
| 67 | 
            +
             | 
| 41 68 | 
             
            ```ruby
         | 
| 42 | 
            -
             | 
| 69 | 
            +
            csv.ge('id', '2')
         | 
| 43 70 | 
             
            ```
         | 
| 44 | 
            -
             | 
| 71 | 
            +
             | 
| 72 | 
            +
            * select all records which have 'id' attribute less than 2
         | 
| 73 | 
            +
             | 
| 45 74 | 
             
            ```ruby
         | 
| 46 | 
            -
             | 
| 75 | 
            +
            csv.lt('id', '2')
         | 
| 47 76 | 
             
            ```
         | 
| 48 | 
            -
             | 
| 77 | 
            +
             | 
| 78 | 
            +
            * select all records which have 'id' attribute less or equal 2
         | 
| 79 | 
            +
             | 
| 49 80 | 
             
            ```ruby
         | 
| 50 | 
            -
             | 
| 81 | 
            +
            csv.le('id', '2')
         | 
| 51 82 | 
             
            ```
         | 
| 52 | 
            -
             | 
| 83 | 
            +
             | 
| 84 | 
            +
            * select all records which have 'firstname' attribute equal 'Tom'
         | 
| 85 | 
            +
             | 
| 53 86 | 
             
            ```ruby
         | 
| 54 | 
            -
             | 
| 87 | 
            +
            csv.eq('firstname', 'Tom')
         | 
| 55 88 | 
             
            ```
         | 
| 56 | 
            -
             | 
| 89 | 
            +
             | 
| 90 | 
            +
            * select all records which have 'firstname' attribute not equal 'Tom'
         | 
| 91 | 
            +
             | 
| 57 92 | 
             
            ```ruby
         | 
| 58 | 
            -
             | 
| 93 | 
            +
            csv.ne('firstname', 'Tom')
         | 
| 59 94 | 
             
            ```
         | 
| 95 | 
            +
             | 
| 96 | 
            +
            # License
         | 
| 97 | 
            +
             | 
| 98 | 
            +
            SmartCSV uses the MIT license. Please check the [LICENSE][] file for more details.
         | 
| 99 | 
            +
             | 
| 100 | 
            +
            [license]: https://github.com/raglub/smart_csv/blob/master/LICENSE
         | 
    
        data/Rakefile
    CHANGED
    
    
    
        data/lib/csv/table/table.rb
    CHANGED
    
    | @@ -5,7 +5,8 @@ class CSV::Table | |
| 5 5 | 
             
              attr_accessor :ancestor
         | 
| 6 6 |  | 
| 7 7 | 
             
              def where_not(*conditions)
         | 
| 8 | 
            -
                result =  | 
| 8 | 
            +
                result = prepare_new_table
         | 
| 9 | 
            +
             | 
| 9 10 | 
             
                self.each do |record|
         | 
| 10 11 | 
             
                  counter = 0
         | 
| 11 12 | 
             
                  conditions.first.each {|key, value| counter += 1 unless record[key] == value.to_s}
         | 
| @@ -14,9 +15,15 @@ class CSV::Table | |
| 14 15 | 
             
                result
         | 
| 15 16 | 
             
              end
         | 
| 16 17 |  | 
| 18 | 
            +
              # Select records which don't match with block
         | 
| 19 | 
            +
              def not(&block)
         | 
| 20 | 
            +
                result = prepare_new_table
         | 
| 21 | 
            +
                result.table.replace(self.table - self.instance_eval(&block).table)
         | 
| 22 | 
            +
                result
         | 
| 23 | 
            +
              end
         | 
| 24 | 
            +
             | 
| 17 25 | 
             
              def where(*conditions)
         | 
| 18 | 
            -
                result =  | 
| 19 | 
            -
                result.ancestor = self.ancestor || self
         | 
| 26 | 
            +
                result = prepare_new_table
         | 
| 20 27 |  | 
| 21 28 | 
             
                self.each do |record|
         | 
| 22 29 | 
             
                  counter = 0
         | 
    
        data/lib/smart_csv/version.rb
    CHANGED
    
    
    
        data/smart_csv.gemspec
    CHANGED
    
    | @@ -6,12 +6,13 @@ Gem::Specification.new do |gem| | |
| 6 6 | 
             
              gem.name           = 'smart_csv'
         | 
| 7 7 | 
             
              gem.version        = SmartCSV::VERSION
         | 
| 8 8 | 
             
              gem.platform       = Gem::Platform::RUBY
         | 
| 9 | 
            -
              gem.date           = '2013- | 
| 9 | 
            +
              gem.date           = '2013-11-02'
         | 
| 10 10 | 
             
              gem.authors        = ['Michał Szyma']
         | 
| 11 11 | 
             
              gem.email          = ['raglub.ruby@gmail.com']
         | 
| 12 12 | 
             
              gem.homepage       = "https://github.com/raglub/smart_csv"
         | 
| 13 13 | 
             
              gem.summary        = %q{Extend CSV class.}
         | 
| 14 14 | 
             
              gem.description    = %q{Extend CSV class. CSV can delete or select some records.}
         | 
| 15 | 
            +
              gem.license        = "MIT"
         | 
| 15 16 |  | 
| 16 17 | 
             
              gem.files          = `git ls-files`.split($\)
         | 
| 17 18 | 
             
              gem.executables    = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
         | 
| @@ -19,5 +20,9 @@ Gem::Specification.new do |gem| | |
| 19 20 |  | 
| 20 21 | 
             
              gem.require_paths  = ['lib']
         | 
| 21 22 |  | 
| 23 | 
            +
              gem.add_dependency 'bundler', '>= 1.0.0'
         | 
| 24 | 
            +
              gem.add_dependency 'rake'
         | 
| 25 | 
            +
             | 
| 22 26 | 
             
              gem.add_development_dependency 'rspec', ">= 2.0.0"
         | 
| 27 | 
            +
             | 
| 23 28 | 
             
            end
         | 
    
        data/spec/csv/row/row_spec.rb
    CHANGED
    
    
| @@ -1,57 +1,66 @@ | |
| 1 | 
            -
            require ' | 
| 1 | 
            +
            require 'spec_helper'
         | 
| 2 2 |  | 
| 3 3 | 
             
            describe CSV::Table do
         | 
| 4 4 |  | 
| 5 | 
            -
              let(:csv_data) {"id,firstname,lastname | 
| 5 | 
            +
              let(:csv_data) {"id,firstname,lastname
         | 
| 6 | 
            +
            1,One,One
         | 
| 7 | 
            +
            2,Two,Two
         | 
| 8 | 
            +
            4,Four,Four
         | 
| 9 | 
            +
            5,Five,Five
         | 
| 10 | 
            +
            6,One,One"}
         | 
| 6 11 |  | 
| 7 | 
            -
              let(: | 
| 12 | 
            +
              let(:csv) { CSV.parse(csv_data, {:col_sep => ',', :headers => true}) }
         | 
| 8 13 |  | 
| 9 14 | 
             
              it "should correct create one record" do
         | 
| 10 | 
            -
                 | 
| 11 | 
            -
                 | 
| 12 | 
            -
                 | 
| 13 | 
            -
                 | 
| 15 | 
            +
                csv.size.should eq(5)
         | 
| 16 | 
            +
                csv.create("id"=> '13', "lastname" => '1992')
         | 
| 17 | 
            +
                csv.size.should eq(6)
         | 
| 18 | 
            +
                csv[-1]["id"].should eql('13')
         | 
| 14 19 | 
             
              end
         | 
| 15 20 |  | 
| 16 21 | 
             
              it "should correct select records" do
         | 
| 17 | 
            -
                 | 
| 18 | 
            -
                 | 
| 22 | 
            +
                csv.where('firstname' => 'One').size.should eql(2)
         | 
| 23 | 
            +
                csv.where('firstname' => 'One').where_not('id' => '6').size.should eql(1)
         | 
| 24 | 
            +
              end
         | 
| 25 | 
            +
             | 
| 26 | 
            +
              it "#not" do
         | 
| 27 | 
            +
                csv.not{where('firstname' => "One")}.size.should eql(3)
         | 
| 19 28 | 
             
              end
         | 
| 20 29 |  | 
| 21 30 | 
             
              it "should select records with id greater than 2" do
         | 
| 22 | 
            -
                 | 
| 31 | 
            +
                csv.gt('id', 2).size.should eq(3)
         | 
| 23 32 | 
             
              end
         | 
| 24 33 |  | 
| 25 34 | 
             
              it "should select records with id greater than or equal to 2" do
         | 
| 26 | 
            -
                 | 
| 35 | 
            +
                csv.ge('id', 2).size.should eq(4)
         | 
| 27 36 | 
             
              end
         | 
| 28 37 |  | 
| 29 38 | 
             
              it "should select records with id less than 2" do
         | 
| 30 | 
            -
                 | 
| 39 | 
            +
                csv.lt('id', 2).size.should eq(1)
         | 
| 31 40 | 
             
              end
         | 
| 32 41 |  | 
| 33 42 | 
             
              it "should select records with id less than or equal to 2" do
         | 
| 34 | 
            -
                 | 
| 43 | 
            +
                csv.le('id', 2).size.should eq(2)
         | 
| 35 44 | 
             
              end
         | 
| 36 45 |  | 
| 37 46 | 
             
              it "should select records with firstname equal to 'Four'" do
         | 
| 38 | 
            -
                 | 
| 47 | 
            +
                csv.eq('firstname', 'Four').size.should eq(1)
         | 
| 39 48 | 
             
              end
         | 
| 40 49 |  | 
| 41 50 | 
             
              it "should select records with firstname not equal to 'Four'" do
         | 
| 42 | 
            -
                 | 
| 51 | 
            +
                csv.ne('firstname', 'Four').size.should eq(4)
         | 
| 43 52 | 
             
              end
         | 
| 44 53 |  | 
| 45 54 | 
             
              it "should correct delete records" do
         | 
| 46 | 
            -
                 | 
| 47 | 
            -
                 | 
| 48 | 
            -
                 | 
| 55 | 
            +
                csv.size.should eql(5)
         | 
| 56 | 
            +
                csv.delete_all
         | 
| 57 | 
            +
                csv.size.should eql(0)
         | 
| 49 58 | 
             
              end
         | 
| 50 59 |  | 
| 51 60 | 
             
              it "should correct delete records with selected group" do
         | 
| 52 | 
            -
                 | 
| 53 | 
            -
                 | 
| 54 | 
            -
                 | 
| 61 | 
            +
                csv.size.should eql(5)
         | 
| 62 | 
            +
                csv.where('firstname' => 'One').delete_all
         | 
| 63 | 
            +
                csv.size.should eql(3)
         | 
| 55 64 | 
             
              end
         | 
| 56 65 |  | 
| 57 66 | 
             
            end
         | 
    
        data/spec/spec_helper.rb
    CHANGED
    
    
    
        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.9
         | 
| 5 5 | 
             
              prerelease: 
         | 
| 6 6 | 
             
            platform: ruby
         | 
| 7 7 | 
             
            authors:
         | 
| @@ -9,8 +9,40 @@ authors: | |
| 9 9 | 
             
            autorequire: 
         | 
| 10 10 | 
             
            bindir: bin
         | 
| 11 11 | 
             
            cert_chain: []
         | 
| 12 | 
            -
            date: 2013- | 
| 12 | 
            +
            date: 2013-11-02 00:00:00.000000000 Z
         | 
| 13 13 | 
             
            dependencies:
         | 
| 14 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 15 | 
            +
              name: bundler
         | 
| 16 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 17 | 
            +
                none: false
         | 
| 18 | 
            +
                requirements:
         | 
| 19 | 
            +
                - - ! '>='
         | 
| 20 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 21 | 
            +
                    version: 1.0.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: 1.0.0
         | 
| 30 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 31 | 
            +
              name: rake
         | 
| 32 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 33 | 
            +
                none: false
         | 
| 34 | 
            +
                requirements:
         | 
| 35 | 
            +
                - - ! '>='
         | 
| 36 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 37 | 
            +
                    version: '0'
         | 
| 38 | 
            +
              type: :runtime
         | 
| 39 | 
            +
              prerelease: false
         | 
| 40 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 41 | 
            +
                none: false
         | 
| 42 | 
            +
                requirements:
         | 
| 43 | 
            +
                - - ! '>='
         | 
| 44 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 45 | 
            +
                    version: '0'
         | 
| 14 46 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 15 47 | 
             
              name: rspec
         | 
| 16 48 | 
             
              requirement: !ruby/object:Gem::Requirement
         | 
| @@ -36,8 +68,9 @@ extra_rdoc_files: [] | |
| 36 68 | 
             
            files:
         | 
| 37 69 | 
             
            - .gitignore
         | 
| 38 70 | 
             
            - .rspec
         | 
| 71 | 
            +
            - .travis.yml
         | 
| 39 72 | 
             
            - Gemfile
         | 
| 40 | 
            -
            - LICENSE | 
| 73 | 
            +
            - LICENSE
         | 
| 41 74 | 
             
            - README.md
         | 
| 42 75 | 
             
            - Rakefile
         | 
| 43 76 | 
             
            - lib/csv/csv.rb
         | 
| @@ -50,7 +83,8 @@ files: | |
| 50 83 | 
             
            - spec/csv/table/table_spec.rb
         | 
| 51 84 | 
             
            - spec/spec_helper.rb
         | 
| 52 85 | 
             
            homepage: https://github.com/raglub/smart_csv
         | 
| 53 | 
            -
            licenses: | 
| 86 | 
            +
            licenses:
         | 
| 87 | 
            +
            - MIT
         | 
| 54 88 | 
             
            post_install_message: 
         | 
| 55 89 | 
             
            rdoc_options: []
         | 
| 56 90 | 
             
            require_paths:
         |