rcsv_loader 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f581428c6e050728dbc4aa92a115ecd9c1441621
4
+ data.tar.gz: cd70140fdf7f62279bb6b28e01dd36ab0c9411f3
5
+ SHA512:
6
+ metadata.gz: 8e0da230d509d4dca60d489d23551c8fda507d672ef33f372cfba747fe2b2ea904f1e1c531d67e1db72461bbc89452293cb0346a033f6a4da8702b5de25fdcba
7
+ data.tar.gz: 904038d9043c618727974778f5b93ea265abb6d8ac9f042ccec4a8867e60dabe5b0531a3403aa47ff8e1997292dfb2d8445e2cf5f768eb575aacca762c980de6
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1.0
7
+ - 2.2.0
8
+ script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Osadake212
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 ADDED
@@ -0,0 +1,76 @@
1
+ # RCsvLoader
2
+
3
+ Mange a csv file more easily.
4
+
5
+ [![Build Status](https://travis-ci.org/osadake212/rcsv_loader.svg?branch=master)](https://travis-ci.org/osadake212/rcsv_loader)
6
+
7
+ ## Usage
8
+
9
+ Only 3 steps to start.
10
+
11
+ ### 0. Prepare a csv file.
12
+
13
+ ```
14
+
15
+ "id","file name"
16
+ "1","file_01.zip"
17
+
18
+ ```
19
+
20
+ ### 1. Define a class that represent csv schema.
21
+
22
+ ```ruby
23
+
24
+ require 'rcsv_loader'
25
+
26
+ class SampleCsv < RCsvLoader::Base
27
+ column 'id'
28
+ column 'file name', :file_name
29
+ end
30
+
31
+ ```
32
+
33
+ ### 2. Load csv file with options.
34
+
35
+ ```ruby
36
+
37
+ sample = SampleCsv.load_file csv_file_path, encoding: 'utf-8', headers: true
38
+
39
+ ```
40
+
41
+ ### 3. Get values with the accessor that is previously defined.
42
+
43
+ ```ruby
44
+
45
+ rows = sample.where({ :id => "1" })
46
+ p rows.first.file_name # => "file_01.zip"
47
+
48
+ ```
49
+
50
+ ### More
51
+
52
+ [More information](https://github.com/osadake212/rcsv_loader/wiki).
53
+
54
+ ## Installation
55
+
56
+ Add this line to your application's Gemfile:
57
+
58
+ ```ruby
59
+ gem 'rcsv_loader'
60
+ ```
61
+
62
+ And then execute:
63
+
64
+ $ bundle
65
+
66
+ Or install it yourself as:
67
+
68
+ $ gem install rcsv_loader
69
+
70
+ ## Contributing
71
+
72
+ 1. Fork it ( https://github.com/osadake212/rcsv_loader/fork )
73
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
74
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
75
+ 4. Push to the branch (`git push origin my-new-feature`)
76
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ module RCsvLoader
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,140 @@
1
+ require "rcsv_loader/version"
2
+ require 'csv'
3
+
4
+ module RCsvLoader
5
+
6
+ attr_reader :headers
7
+
8
+ #
9
+ # Define accessor names for csv column.
10
+ #
11
+ def column column_name, alias_name = nil
12
+ alias_name = column_name.to_sym unless alias_name
13
+ @headers = all_headers
14
+ @headers.merge!(alias_name => column_name)
15
+ define_row(@headers)
16
+ end
17
+
18
+ #
19
+ # Define headers for no headers csv.
20
+ #
21
+ def insert_headers headers = []
22
+ @headers = Hash[headers.map.with_index { |e, i| [e, i] }]
23
+ define_row(@headers)
24
+ end
25
+
26
+ class Base
27
+ extend RCsvLoader
28
+ extend Forwardable
29
+
30
+ #
31
+ # Laod a csv file
32
+ #
33
+ # options: The options for 'csv' which is Ruby's built-in library.
34
+ #
35
+ def self.load_csv file, options = {}
36
+
37
+ rows = CSV.readlines file, options
38
+
39
+ rows = yield rows if block_given?
40
+
41
+ self.new rows.map { |row| self::Row.new row }
42
+ end
43
+
44
+ def_delegators :@rows, :+, :<<
45
+
46
+ def initialize rows = []
47
+ @rows = rows
48
+ end
49
+
50
+ def all
51
+ @rows
52
+ end
53
+
54
+ #
55
+ # params = {
56
+ # :#{accessor_name} => expected_value,
57
+ # ...
58
+ # }
59
+ #
60
+ # compare with '=='
61
+ #
62
+ # return empty array if the condition is invalid or couldn't find any rows.
63
+ #
64
+ def where params = {}
65
+ return @rows if params.empty?
66
+
67
+ # filter the conditions.
68
+ params.select! do |k, v|
69
+ self.class.headers.keys.any? { |key| key.to_s == k.to_s }
70
+ end
71
+
72
+ return [] if params.empty?
73
+
74
+ @rows.select do |row|
75
+ params.all? { |attr, con| (row.send attr) == con }
76
+ end
77
+
78
+ end
79
+
80
+ #
81
+ # Convert to csv string
82
+ #
83
+ # options: {
84
+ # headers: boolean
85
+ # }
86
+ #
87
+ def to_csv options = {}
88
+ csv = ""
89
+ csv += CSV.generate_line(self.class.headers.map { |k, v| v } ) if options[:headers]
90
+ csv += @rows.map(&:to_csv).join
91
+ end
92
+ end
93
+
94
+ private
95
+ #
96
+ # Get headers that include super class's one.
97
+ #
98
+ def all_headers current_class = nil
99
+ current_class ||= self
100
+ return {} if current_class == Base
101
+ current_header = current_class.headers || {}
102
+ return current_header.merge all_headers(current_class.superclass)
103
+ end
104
+
105
+ #
106
+ # Define a class which represent a row of csv.
107
+ #
108
+ def define_row headers
109
+ headers ||= {}
110
+
111
+ # get the superclass for Row
112
+ baseclass = if superclass.const_defined? :Row
113
+ superclass::Row
114
+ else
115
+ Object
116
+ end
117
+
118
+ # remove class definition
119
+ if const_defined? :Row and baseclass != self::Row
120
+ self.send :remove_const, :Row
121
+ end
122
+
123
+ # define Row class
124
+ const_set :Row, (Class.new(baseclass) do
125
+
126
+ attr_accessor *headers.keys
127
+
128
+ define_method :initialize do |line = {}|
129
+ headers.each do |k, v|
130
+ instance_variable_set "@#{k.to_s}", line[v]
131
+ end
132
+ end
133
+
134
+ define_method :to_csv do
135
+ CSV.generate_line headers.map { |k, v| self.send k }
136
+ end
137
+
138
+ end)
139
+ end
140
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rcsv_loader/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rcsv_loader"
8
+ spec.version = RCsvLoader::VERSION
9
+ spec.authors = ["osadake212"]
10
+ spec.email = ["osadake.212@gmail.com"]
11
+ spec.summary = %q{A simple csv loader.}
12
+ spec.description = %q{A simple csv loader.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0.0"
24
+ end
@@ -0,0 +1,2 @@
1
+ "1","file_01.zip","zip","https://example.com/file_01.zip"
2
+ "2","file_02.zip","zip","https://example.com/file_02.zip"
@@ -0,0 +1,3 @@
1
+ "id","file name","extension","url"
2
+ "1","file_01.zip","zip","https://example.com/file_01.zip"
3
+ "2","file_02.zip","zip","https://example.com/file_02.zip"
@@ -0,0 +1,8 @@
1
+ require 'rcsv_loader'
2
+
3
+ class SampleCsv < RCsvLoader::Base
4
+ column 'id'
5
+ column 'file name', :file_name
6
+ column 'extension'
7
+ column 'url'
8
+ end
@@ -0,0 +1,5 @@
1
+ require 'rcsv_loader'
2
+
3
+ class SampleCsvNoHeaders < RCsvLoader::Base
4
+ insert_headers ['id', 'file_name', 'extension', 'url']
5
+ end
@@ -0,0 +1,138 @@
1
+ require 'spec_helper'
2
+ require 'pathname'
3
+
4
+ require 'models/sample_csv'
5
+ require 'models/sample_csv_no_headers'
6
+
7
+ describe RCsvLoader do
8
+ it 'has a version number' do
9
+ expect(RCsvLoader::VERSION).not_to be nil
10
+ end
11
+
12
+ context "sample" do
13
+
14
+ shared_examples 'Row attributes' do |expected|
15
+ expected.each do |attr_name|
16
+ it { is_expected.to respond_to attr_name }
17
+ end
18
+ end
19
+
20
+ shared_examples "where" do |expected|
21
+ describe "data count" do
22
+ subject { results.count }
23
+ it { is_expected.to eq expected[:data_count] }
24
+ end
25
+
26
+ context "first row" do
27
+ subject(:row) { results.first }
28
+ describe "file name" do
29
+ subject { row.file_name }
30
+ it { is_expected.to eq expected[:first_row][:file_name] }
31
+ end
32
+ describe "extension" do
33
+ subject { row.extension }
34
+ it { is_expected.to eq expected[:first_row][:extension] }
35
+ end
36
+ describe "url" do
37
+ subject { row.url }
38
+ it { is_expected.to eq expected[:first_row][:url] }
39
+ end
40
+ end
41
+ end
42
+
43
+
44
+ context "with headers" do
45
+ let(:sample) do
46
+ path = File.join(Pathname(__FILE__).dirname, "fixtures/sample.csv")
47
+ SampleCsv.load_csv(path, :headers => true)
48
+ end
49
+
50
+ describe "data count" do
51
+ subject { sample.all.count }
52
+ it { is_expected.to eq 2 }
53
+ end
54
+
55
+ context "first row" do
56
+ subject { sample.all.first }
57
+ expected = [:id, :file_name, :extension, :url]
58
+ it_behaves_like 'Row attributes', expected
59
+ end
60
+
61
+ describe "where" do
62
+ context ':id => "2"' do
63
+ let(:results) { sample.where({ :id => "2" }) }
64
+ expected = {
65
+ :data_count => 1,
66
+ :first_row => {
67
+ :file_name => "file_02.zip",
68
+ :extension => "zip",
69
+ :url => "https://example.com/file_02.zip"
70
+ }
71
+ }
72
+ it_behaves_like "where", expected
73
+ end
74
+
75
+ context ':id => "1", :extension => "zip"' do
76
+ let(:results) { sample.where({ :id => "1", :extension => "zip" }) }
77
+ expected = {
78
+ :data_count => 1,
79
+ :first_row => {
80
+ :file_name => "file_01.zip",
81
+ :extension => "zip",
82
+ :url => "https://example.com/file_01.zip"
83
+ }
84
+ }
85
+ it_behaves_like "where", expected
86
+ end
87
+ end
88
+ end
89
+
90
+ context "with no_headers" do
91
+ let(:sample) do
92
+ path = File.join(Pathname(__FILE__).dirname, "fixtures/no_headers_sample.csv")
93
+ SampleCsvNoHeaders.load_csv path
94
+ end
95
+
96
+ describe "data count" do
97
+ subject { sample.all.count }
98
+ it { is_expected.to eq 2 }
99
+ end
100
+
101
+ context "first row" do
102
+ subject { sample.all.first }
103
+ expected = [:id, :file_name, :extension, :url]
104
+ it_behaves_like 'Row attributes', expected
105
+ end
106
+
107
+ describe "where" do
108
+ context ':extension => "zip"' do
109
+ let(:results) { sample.where({ :extension => "zip" }) }
110
+ expected = {
111
+ :data_count => 2,
112
+ :first_row => {
113
+ :file_name => "file_01.zip",
114
+ :extension => "zip",
115
+ :url => "https://example.com/file_01.zip"
116
+ }
117
+ }
118
+ it_behaves_like "where", expected
119
+ end
120
+
121
+ context ':id => "1", :extension => "zip"' do
122
+ let(:results) { sample.where({ :id => "1", :extension => "zip" }) }
123
+ expected = {
124
+ :data_count => 1,
125
+ :first_row => {
126
+ :file_name => "file_01.zip",
127
+ :extension => "zip",
128
+ :url => "https://example.com/file_01.zip"
129
+ }
130
+ }
131
+ it_behaves_like "where", expected
132
+ end
133
+ end
134
+
135
+ end
136
+ end
137
+
138
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'rcsv_loader'
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rcsv_loader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - osadake212
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.0
55
+ description: A simple csv loader.
56
+ email:
57
+ - osadake.212@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .rspec
64
+ - .travis.yml
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/rcsv_loader.rb
70
+ - lib/rcsv_loader/version.rb
71
+ - rcsv_loader.gemspec
72
+ - spec/fixtures/no_headers_sample.csv
73
+ - spec/fixtures/sample.csv
74
+ - spec/models/sample_csv.rb
75
+ - spec/models/sample_csv_no_headers.rb
76
+ - spec/rcsv_loader_spec.rb
77
+ - spec/spec_helper.rb
78
+ homepage: ''
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.0.14
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: A simple csv loader.
102
+ test_files:
103
+ - spec/fixtures/no_headers_sample.csv
104
+ - spec/fixtures/sample.csv
105
+ - spec/models/sample_csv.rb
106
+ - spec/models/sample_csv_no_headers.rb
107
+ - spec/rcsv_loader_spec.rb
108
+ - spec/spec_helper.rb