simple_csv_importer 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 +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +35 -0
- data/LICENSE.txt +21 -0
- data/README.md +47 -0
- data/Rakefile +6 -0
- data/example/user.rb +3 -0
- data/example/user_importer.rb +11 -0
- data/lib/simple_csv_importer.rb +55 -0
- data/lib/simple_csv_importer/version.rb +3 -0
- data/simple_csv_importer.gemspec +25 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: '0974b7d4838d2467be6262d67617fb74cdcdaeac'
|
4
|
+
data.tar.gz: 33049bd4c8fae68ae122a59bb6feaaab0eafc52e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: '04298a4b9d4f18c8dda33b1866b674912f60f83ed0c77d59b3f6cb2730b2b3a771eb8307c3afbf9d0ca80575cc07739478124e6e286445a77ce20b232c1759bc'
|
7
|
+
data.tar.gz: 530ec305bbfa746300faaa998a96c240875812e545e3931a663ad18c5334f0f04e6916c5265def651e9637b0a09a9fe9baf790a2fdb60ebf843f57c5207ce3ed
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
simple_csv_importer (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.3)
|
10
|
+
rake (10.5.0)
|
11
|
+
rspec (3.7.0)
|
12
|
+
rspec-core (~> 3.7.0)
|
13
|
+
rspec-expectations (~> 3.7.0)
|
14
|
+
rspec-mocks (~> 3.7.0)
|
15
|
+
rspec-core (3.7.0)
|
16
|
+
rspec-support (~> 3.7.0)
|
17
|
+
rspec-expectations (3.7.0)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.7.0)
|
20
|
+
rspec-mocks (3.7.0)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.7.0)
|
23
|
+
rspec-support (3.7.0)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
bundler (~> 1.16)
|
30
|
+
rake (~> 10.0)
|
31
|
+
rspec (~> 3.0)
|
32
|
+
simple_csv_importer!
|
33
|
+
|
34
|
+
BUNDLED WITH
|
35
|
+
1.16.0
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Daiki Matoba
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# SimpleCsvImporter
|
2
|
+
|
3
|
+
Convert CSV data to instances of model and import to database easily.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
1. prepare model class
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
class User
|
11
|
+
attr_accessor :email, :name, :sex, :age
|
12
|
+
end
|
13
|
+
```
|
14
|
+
|
15
|
+
2. create importer
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
class UserImporter
|
19
|
+
include SimpleImporter
|
20
|
+
|
21
|
+
assign :email, 'Email'
|
22
|
+
assign :name, ->(row) { "#{row['First Name']} #{row['Last Name']}" }
|
23
|
+
assign :sex, 'Sex' do |sex|
|
24
|
+
sexes = { 'Male' => 1, 'Female' => 2 }
|
25
|
+
sexes[sex]
|
26
|
+
end
|
27
|
+
assign :age, 'Age', &:to_i
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
3. load csv and convert to instances of the model
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
importer = UserImporter.new
|
35
|
+
csv_text = <<-TEXT
|
36
|
+
Email,First Name,Last Name,Sex,Age,
|
37
|
+
alice@example.com,Alice,Abbot,Female,20
|
38
|
+
bob@mail.com,Bob,Brown,Male,25
|
39
|
+
TEXT
|
40
|
+
|
41
|
+
importer.load(csv_text)
|
42
|
+
# =>
|
43
|
+
#[
|
44
|
+
# #<User:0x01 @age=20, @email="alice@example.com", @name="Alice Abbot", @sex=2>,
|
45
|
+
# #<User:0x02 @age=25, @email="bob@mail.com", @name="Bob Brown", @sex=1>
|
46
|
+
#]
|
47
|
+
```
|
data/Rakefile
ADDED
data/example/user.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
class UserImporter
|
2
|
+
include SimpleCsvImporter
|
3
|
+
|
4
|
+
assign :email, 'Email'
|
5
|
+
assign :name, ->(row) { "#{row['First Name']} #{row['Last Name']}" }
|
6
|
+
assign :sex, 'Sex' do |sex|
|
7
|
+
sexes = { 'Male' => 1, 'Female' => 2 }
|
8
|
+
sexes[sex]
|
9
|
+
end
|
10
|
+
assign :age, 'Age', &:to_i
|
11
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'simple_csv_importer/version'
|
2
|
+
require 'csv'
|
3
|
+
|
4
|
+
module SimpleCsvImporter
|
5
|
+
def self.included(base)
|
6
|
+
base.class_eval do
|
7
|
+
@target_klass = base.to_s.sub(/Importer$/, '')
|
8
|
+
@rules = []
|
9
|
+
class << self
|
10
|
+
attr_reader :rules, :target_klass
|
11
|
+
end
|
12
|
+
end
|
13
|
+
base.extend ClassMethods
|
14
|
+
end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
def assign(attr_name, column_or_proc)
|
18
|
+
unless attr_name.is_a? Symbol
|
19
|
+
raise ArgumentError, 'attr_name must be a Symbol'
|
20
|
+
end
|
21
|
+
unless [Proc, String].include? column_or_proc.class
|
22
|
+
raise ArgumentError, 'column_or_proc must be a Proc or String'
|
23
|
+
end
|
24
|
+
|
25
|
+
processor =
|
26
|
+
if column_or_proc.is_a? String
|
27
|
+
if block_given?
|
28
|
+
->(row) { yield row[column_or_proc] }
|
29
|
+
else
|
30
|
+
->(row) { row[column_or_proc] }
|
31
|
+
end
|
32
|
+
elsif column_or_proc.is_a? Proc
|
33
|
+
column_or_proc
|
34
|
+
end
|
35
|
+
@rules << [attr_name, processor]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
### Instance Methods
|
40
|
+
|
41
|
+
# @param [String] csv_text
|
42
|
+
# @return [Array]
|
43
|
+
def load(csv_text)
|
44
|
+
csv = CSV.parse(csv_text, headers: true)
|
45
|
+
|
46
|
+
target_class = Object.const_get(self.class.target_klass)
|
47
|
+
csv.map do |row|
|
48
|
+
target = target_class.new
|
49
|
+
self.class.rules.each do |attr_name, processor|
|
50
|
+
target.public_send(:"#{attr_name}=", processor.call(row))
|
51
|
+
end
|
52
|
+
target
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'simple_csv_importer/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'simple_csv_importer'
|
7
|
+
spec.version = SimpleCsvImporter::VERSION
|
8
|
+
spec.authors = ['Daiki Matoba']
|
9
|
+
spec.email = ['telnetstat@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = %q{Convert CSV data to instances of model and import to database easily.}
|
12
|
+
spec.description = %q{Convert CSV data to instances of model and import to database easily.}
|
13
|
+
spec.homepage = 'https://github.com/d-mato/simple_csv_importer'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
23
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
24
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_csv_importer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daiki Matoba
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-12-19 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.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
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'
|
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'
|
55
|
+
description: Convert CSV data to instances of model and import to database easily.
|
56
|
+
email:
|
57
|
+
- telnetstat@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- Gemfile.lock
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- example/user.rb
|
71
|
+
- example/user_importer.rb
|
72
|
+
- lib/simple_csv_importer.rb
|
73
|
+
- lib/simple_csv_importer/version.rb
|
74
|
+
- simple_csv_importer.gemspec
|
75
|
+
homepage: https://github.com/d-mato/simple_csv_importer
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.6.11
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: Convert CSV data to instances of model and import to database easily.
|
99
|
+
test_files: []
|