csv_manager 1.0.1.pre.rc.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 +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +95 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/csv_manager.gemspec +32 -0
- data/doc/create_custom_headers.md +46 -0
- data/doc/export_download.md +25 -0
- data/doc/import_choose_csv_and_parse.md +42 -0
- data/doc/import_populate_database.md +44 -0
- data/doc/import_specify_csv_path.md +37 -0
- data/lib/csv_manager/export.rb +53 -0
- data/lib/csv_manager/import.rb +48 -0
- data/lib/csv_manager/version.rb +3 -0
- data/lib/csv_manager.rb +8 -0
- metadata +174 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ced119dddc799c9c44f05de132e04f2bee096843
|
4
|
+
data.tar.gz: a8bb365c2826af9f3bb13ac11f3213fd8d56206a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: '00826715c6ed5a3d2cdd943c6a9454507fe9540e133e88f8298e3e4ac688982ba6ebf520e48a784791acd19f0fab7fdfb1e9519978165db8285f1e1ca3ee2892'
|
7
|
+
data.tar.gz: d8c0a2fa7f8089a92cf88eb3394b008d2f1f1e67f26214468595b8a2fa8a9dab34c77469bda6e9b04fdaf07d441226d36dc906f716ecbd7a8aa71f26a9221daf
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at ghbooth12@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Gahee Heo
|
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,95 @@
|
|
1
|
+
# CSVManager
|
2
|
+
|
3
|
+
This gem helps import/export a CSV file in a very easy and simple way.
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
# Import a CSV file and parse the file
|
7
|
+
import = CSVManager::Import.new
|
8
|
+
import.parse('tmp/default.csv')
|
9
|
+
import.create(Product)
|
10
|
+
|
11
|
+
# Export a CSV file and download it
|
12
|
+
export = CSVManager::Export.new
|
13
|
+
export.download(products_controller, Product.all, "my_file.csv")
|
14
|
+
```
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
Add this line to Gemfile, then execute `$ bundle`.
|
19
|
+
```ruby
|
20
|
+
gem 'csv_manager'
|
21
|
+
```
|
22
|
+
Or install it yourself as:
|
23
|
+
```bash
|
24
|
+
$ gem install csv_manager
|
25
|
+
```
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
### How to Import
|
30
|
+
First instantiate `CSVManager::Import` class.
|
31
|
+
```ruby
|
32
|
+
@import = CSVManager::Import.new
|
33
|
+
```
|
34
|
+
|
35
|
+
Import a CSV file and `parse` the file. Then `@import` can call `cols`, `rows`, `entire`, `count` and `get_contents`.
|
36
|
+
```ruby
|
37
|
+
@import.parse('tmp/default.csv')
|
38
|
+
|
39
|
+
@import.cols #=> ["name", "category", "quantity"]
|
40
|
+
@import.rows #=> [["eraser", "stationery", 100], ["spoon", "kitchen", 110]]
|
41
|
+
@import.entire #=> [{"name"=>"eraser", "category"=>"stationery", "quantity"=>100}, {"name"=>"spoon", "category"=>"kitchen", "quantity"=>110}]
|
42
|
+
@import.count #=> 2
|
43
|
+
@import.get_contents("category") #=> ["stationery", "kitchen"]
|
44
|
+
```
|
45
|
+
|
46
|
+
Customized headers can be sent as a second argument to `parse`.
|
47
|
+
```ruby
|
48
|
+
@import.parse('tmp/default.csv', ["Name", "Type", "Count"])
|
49
|
+
```
|
50
|
+
|
51
|
+
Populate database with data from `@import`
|
52
|
+
```ruby
|
53
|
+
@import.create(Product)
|
54
|
+
#<Product id: 27, name: "eraser", category: "stationery", quantity: 100, created_at: "2017-01-06 17:14:09", updated_at: "2017-01-06 17:14:09">
|
55
|
+
#<Product id: 28, name: "spoon", category: "kitchen", quantity: 110, created_at: "2017-01-06 17:14:09", updated_at: "2017-01-06 17:14:09">CSV
|
56
|
+
```
|
57
|
+
|
58
|
+
* [Example 1) Choose a CSV file and parse](doc/import_choose_csv_and_parse.md)
|
59
|
+
* [Example 2) Create custom headers](doc/create_custom_headers.md)
|
60
|
+
* [Example 3) Specify a CSV file path](doc/import_specify_csv_path.md)
|
61
|
+
* [Example 4) Populate Database](doc/import_populate_database.md)
|
62
|
+
|
63
|
+
-----
|
64
|
+
|
65
|
+
### How to Export
|
66
|
+
First instantiate `CSVManager::Export` class.
|
67
|
+
```ruby
|
68
|
+
@export = CSVManager::Export.new
|
69
|
+
```
|
70
|
+
|
71
|
+
Create a CSV file with the given model.
|
72
|
+
```ruby
|
73
|
+
@export.download(products_controller, Product.all, "my_file.csv")
|
74
|
+
```
|
75
|
+
|
76
|
+
In the view, make sure to send `format: "csv"` to the path.
|
77
|
+
```html
|
78
|
+
<%= link_to 'Download CSV', download_products_path(format: "csv") %>
|
79
|
+
```
|
80
|
+
|
81
|
+
* [Example 5) Download database in a CSV file](doc/export_download.md)
|
82
|
+
|
83
|
+
|
84
|
+
## Demo App
|
85
|
+
To see the `CSVManager` in action you can take a look at this app - [Inventory](https://inventory-ghbooth12.herokuapp.com).
|
86
|
+
|
87
|
+
|
88
|
+
## Contributing
|
89
|
+
|
90
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ghbooth12/csv_manager. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
91
|
+
|
92
|
+
|
93
|
+
## License
|
94
|
+
|
95
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "csv_manager"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/csv_manager.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'csv_manager/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "csv_manager"
|
8
|
+
spec.version = CSVManager::VERSION
|
9
|
+
spec.authors = ["Gahee Heo"]
|
10
|
+
spec.email = ["ghbooth12@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Import and export a CSV file}
|
13
|
+
spec.description = %q{Import and export a CSV file in a very easy and simple way}
|
14
|
+
spec.homepage = "https://github.com/ghbooth12/csv_manager"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "activerecord", "~> 4.0"
|
25
|
+
spec.add_development_dependency "sqlite3", "~> 1.3", ">= 1.3.13"
|
26
|
+
spec.add_development_dependency "railties", ">= 4.2.0", "< 5.1"
|
27
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
28
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
29
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
30
|
+
spec.add_development_dependency "pry", "~> 0.10.4"
|
31
|
+
# spec.add_development_dependency "logging"
|
32
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
Step 1) Route to HomeController#import_csv
|
2
|
+
```ruby
|
3
|
+
# routes.rb
|
4
|
+
resources :home do
|
5
|
+
collection { post :import_csv_with_headers }
|
6
|
+
end
|
7
|
+
...
|
8
|
+
```
|
9
|
+
|
10
|
+
Step 2) When we choose a CSV file from the view, we can get the custom headers and the file name from `params`.
|
11
|
+
```ruby
|
12
|
+
# home_controller.rb
|
13
|
+
class HomeController < ApplicationController
|
14
|
+
def import_csv_with_headers
|
15
|
+
@importer = CSVManager::Import.new
|
16
|
+
headers = params[:headers].split(',').map(&:strip)
|
17
|
+
@importer.parse(params[:file], headers)
|
18
|
+
end
|
19
|
+
...
|
20
|
+
end
|
21
|
+
```
|
22
|
+
|
23
|
+
Step 3) Create the form where we can choose a CSV file and give the customized headers.
|
24
|
+
```html
|
25
|
+
<!-- # home/index.html.erb -->
|
26
|
+
<%= form_tag import_csv_with_headers_home_index_path, multipart: true do %>
|
27
|
+
<%= label_tag :headers, "If you want custom headers" %>
|
28
|
+
<%= text_field_tag :headers %>
|
29
|
+
|
30
|
+
<%= label_tag :file %>
|
31
|
+
<%= file_field_tag :file %>
|
32
|
+
<%= submit_tag 'Import CSV With Headers' %>
|
33
|
+
<% end %>
|
34
|
+
```
|
35
|
+
|
36
|
+
Step 4) After CSV file is parsed, we can use `cols`, `rows`, `entire`, `count` and `get_contents` methods.
|
37
|
+
```ruby
|
38
|
+
@import.cols #=> ["name", "category", "quantity"]
|
39
|
+
@import.rows #=> [["eraser", "stationery", 100], ["spoon", "kitchen", 110]]
|
40
|
+
@import.entire #=> [{"name"=>"eraser", "category"=>"stationery", "quantity"=>100}, {"name"=>"spoon", "category"=>"kitchen", "quantity"=>110}]
|
41
|
+
@import.count #=> 2
|
42
|
+
|
43
|
+
@import.get_contents("name") #=> ["eraser", "spoon"]
|
44
|
+
@import.get_contents("category") #=> ["stationery", "kitchen"]
|
45
|
+
@import.get_contents("quantity") #=> [100, 110]
|
46
|
+
```
|
@@ -0,0 +1,25 @@
|
|
1
|
+
Step 1) Route to ProductsController#download
|
2
|
+
```ruby
|
3
|
+
# routes.rb
|
4
|
+
resources :products do
|
5
|
+
collection { get :download_list }
|
6
|
+
end
|
7
|
+
...
|
8
|
+
```
|
9
|
+
|
10
|
+
Step 2) Get a CSV formatted string by calling `download` on the instance of `CSVManager::Export` class. We can specify certain data such as `Product.where(quantity: 0)`. We can also name the CSV file. (If file name is omitted, the CSV file will be named after the method name. In this case, the CSV file will be `download_list.csv` if file name is omitted.)
|
11
|
+
```ruby
|
12
|
+
# products_controller.rb
|
13
|
+
class ProductsController < ApplicationController
|
14
|
+
def download_list
|
15
|
+
CSVManager::Export.new.download(self, Product.all, "products-#{Date.today}.csv")
|
16
|
+
end
|
17
|
+
...
|
18
|
+
end
|
19
|
+
```
|
20
|
+
|
21
|
+
Step 3) Make sure to send `format: "csv"` to the path.
|
22
|
+
```html
|
23
|
+
<!-- # products/index.html.erb -->
|
24
|
+
<%= link_to 'Download CSV', download_list_products_path(format: "csv") %>
|
25
|
+
```
|
@@ -0,0 +1,42 @@
|
|
1
|
+
Step 1) Route to HomeController#import_csv
|
2
|
+
```ruby
|
3
|
+
# routes.rb
|
4
|
+
resources :home do
|
5
|
+
collection { post :import_csv }
|
6
|
+
end
|
7
|
+
...
|
8
|
+
```
|
9
|
+
|
10
|
+
Step 2) When we choose a CSV file from the view, we can get the file name from `params`.
|
11
|
+
```ruby
|
12
|
+
# home_controller.rb
|
13
|
+
class HomeController < ApplicationController
|
14
|
+
def import_csv
|
15
|
+
@import = CSVManager::Import.new
|
16
|
+
@import.parse(params[:file])
|
17
|
+
end
|
18
|
+
...
|
19
|
+
end
|
20
|
+
```
|
21
|
+
|
22
|
+
Step 3) Create the form where we can choose a CSV file.
|
23
|
+
```html
|
24
|
+
<!-- # home/index.html.erb -->
|
25
|
+
<%= form_tag import_csv_home_index_path, multipart: true do %>
|
26
|
+
<%= label_tag :file %>
|
27
|
+
<%= file_field_tag :file %>
|
28
|
+
<%= submit_tag 'Import CSV' %>
|
29
|
+
<% end %>
|
30
|
+
```
|
31
|
+
|
32
|
+
Step 4) After CSV file is parsed, we can use `cols`, `rows`, `entire`, `count` and `get_contents` methods.
|
33
|
+
```ruby
|
34
|
+
@import.cols #=> ["name", "category", "quantity"]
|
35
|
+
@import.rows #=> [["eraser", "stationery", 100], ["spoon", "kitchen", 110]]
|
36
|
+
@import.entire #=> [{"name"=>"eraser", "category"=>"stationery", "quantity"=>100}, {"name"=>"spoon", "category"=>"kitchen", "quantity"=>110}]
|
37
|
+
@import.count #=> 2
|
38
|
+
|
39
|
+
@import.get_contents("name") #=> ["eraser", "spoon"]
|
40
|
+
@import.get_contents("category") #=> ["stationery", "kitchen"]
|
41
|
+
@import.get_contents("quantity") #=> [100, 110]
|
42
|
+
```
|
@@ -0,0 +1,44 @@
|
|
1
|
+
Step 1) Route to HomeController#save_all
|
2
|
+
```ruby
|
3
|
+
# routes.rb
|
4
|
+
resources :home, only: [] do
|
5
|
+
collection { post :save_all }
|
6
|
+
end
|
7
|
+
...
|
8
|
+
```
|
9
|
+
|
10
|
+
Step 2) `create` populates product database with data from `@import`.
|
11
|
+
```ruby
|
12
|
+
# home_controller.rb
|
13
|
+
class HomeController < ApplicationController
|
14
|
+
def save_all
|
15
|
+
@import = CSVManager::Import.new
|
16
|
+
@import.parse(params[:file])
|
17
|
+
@import.create(Product)
|
18
|
+
...
|
19
|
+
end
|
20
|
+
...
|
21
|
+
end
|
22
|
+
```
|
23
|
+
|
24
|
+
Step 3) Create the form where we can choose a CSV file.
|
25
|
+
```html
|
26
|
+
<!-- # home/index.html.erb -->
|
27
|
+
<%= form_tag save_all_home_index_path, multipart: true %>
|
28
|
+
<%= label_tag :file %>
|
29
|
+
<%= file_field_tag :file %>
|
30
|
+
<%= submit_tag 'Import CSV & Save' %>
|
31
|
+
<% end %>
|
32
|
+
```
|
33
|
+
|
34
|
+
Step 4) After CSV file is parsed, we can use `cols`, `rows`, `entire`, `count` and `get_contents` methods.
|
35
|
+
```ruby
|
36
|
+
@import.cols #=> ["name", "category", "quantity"]
|
37
|
+
@import.rows #=> [["eraser", "stationery", 100], ["spoon", "kitchen", 110]]
|
38
|
+
@import.entire #=> [{"name"=>"eraser", "category"=>"stationery", "quantity"=>100}, {"name"=>"spoon", "category"=>"kitchen", "quantity"=>110}]
|
39
|
+
@import.count #=> 2
|
40
|
+
|
41
|
+
@import.get_contents("name") #=> ["eraser", "spoon"]
|
42
|
+
@import.get_contents("category") #=> ["stationery", "kitchen"]
|
43
|
+
@import.get_contents("quantity") #=> [100, 110]
|
44
|
+
```
|
@@ -0,0 +1,37 @@
|
|
1
|
+
Step 1) Route to HomeController#quick_import
|
2
|
+
```ruby
|
3
|
+
# routes.rb
|
4
|
+
get 'quick_import' => 'home#quick_import'
|
5
|
+
...
|
6
|
+
```
|
7
|
+
|
8
|
+
Step 2) We can specify the file path directly in the `parse` method.
|
9
|
+
```ruby
|
10
|
+
# home_controller.rb
|
11
|
+
class HomeController < ApplicationController
|
12
|
+
def quick_import
|
13
|
+
@import = CSVManager::Import.new
|
14
|
+
@import.parse('tmp/default.csv')
|
15
|
+
...
|
16
|
+
end
|
17
|
+
...
|
18
|
+
end
|
19
|
+
```
|
20
|
+
|
21
|
+
Step 3) (Optional) Create a link that executes `quick_import`
|
22
|
+
```html
|
23
|
+
<!-- # home/index.html.erb -->
|
24
|
+
<%= link_to 'Quick Default Import', quick_import_path %>
|
25
|
+
```
|
26
|
+
|
27
|
+
Step 4) After CSV file is parsed, we can use `cols`, `rows`, `entire`, `count` and `get_contents` methods.
|
28
|
+
```ruby
|
29
|
+
@import.cols #=> ["name", "category", "quantity"]
|
30
|
+
@import.rows #=> [["eraser", "stationery", 100], ["spoon", "kitchen", 110]]
|
31
|
+
@import.entire #=> [{"name"=>"eraser", "category"=>"stationery", "quantity"=>100}, {"name"=>"spoon", "category"=>"kitchen", "quantity"=>110}]
|
32
|
+
@import.count #=> 2
|
33
|
+
|
34
|
+
@import.get_contents("name") #=> ["eraser", "spoon"]
|
35
|
+
@import.get_contents("category") #=> ["stationery", "kitchen"]
|
36
|
+
@import.get_contents("quantity") #=> [100, 110]
|
37
|
+
```
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# require 'logging'
|
2
|
+
|
3
|
+
module CSVManager
|
4
|
+
class Export
|
5
|
+
# def initialize
|
6
|
+
# @logger = Logging.logger['csv_manager_export_log.txt']
|
7
|
+
# @logger.level = :debug
|
8
|
+
# end
|
9
|
+
|
10
|
+
def download(controller, objs, filename=nil)
|
11
|
+
# @logger.debug "DOWNLOAD METHOD CALLED"
|
12
|
+
return if controller.nil? || objs.nil?
|
13
|
+
controller.send_data to_csv(objs), filename: filename
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_csv(objs)
|
17
|
+
return unless validate(objs)
|
18
|
+
|
19
|
+
if objs.is_a?(Array)
|
20
|
+
model = objs.first.class
|
21
|
+
else
|
22
|
+
model_str = objs.class.to_s.split("::").first
|
23
|
+
model = Object.const_get(model_str)
|
24
|
+
end
|
25
|
+
cols = model.column_names
|
26
|
+
|
27
|
+
csv_file = CSV.generate do |csv|
|
28
|
+
csv << cols
|
29
|
+
objs.each do |x|
|
30
|
+
csv << x.attributes.values_at(*cols)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
return csv_file
|
35
|
+
end
|
36
|
+
|
37
|
+
# This method checks to see if an instance of ActiveRecord::Relation or an array of instance of ActiveRecord::Base is passed in.
|
38
|
+
def validate(objs)
|
39
|
+
valid = false
|
40
|
+
|
41
|
+
if objs.is_a?(Array) # eg) Product[0..10].class == Array
|
42
|
+
objs.each do |obj|
|
43
|
+
valid = true if obj.is_a?(ActiveRecord::Base)
|
44
|
+
end
|
45
|
+
else # eg) Product.all.class == ActiveRecord::Relation
|
46
|
+
valid = true if objs.is_a?(ActiveRecord::Relation)
|
47
|
+
end
|
48
|
+
|
49
|
+
return valid
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module CSVManager
|
2
|
+
class Import
|
3
|
+
attr_reader :cols, :rows, :entire, :error
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@cols = []
|
7
|
+
@rows = []
|
8
|
+
@entire = []
|
9
|
+
@error = nil
|
10
|
+
end
|
11
|
+
|
12
|
+
def parse(file, headers=true)
|
13
|
+
return if file.nil?
|
14
|
+
file = file.path if file.respond_to?(:path)
|
15
|
+
|
16
|
+
begin
|
17
|
+
raw_rows = CSV.read(file, headers: headers, converters: :numeric).to_a
|
18
|
+
rescue
|
19
|
+
@error = "ERROR! #{$!.message}"
|
20
|
+
return
|
21
|
+
end
|
22
|
+
|
23
|
+
@cols = raw_rows.shift
|
24
|
+
@rows = raw_rows
|
25
|
+
|
26
|
+
@entire = []
|
27
|
+
CSV.foreach(file, headers: headers, converters: :numeric) do |row|
|
28
|
+
@entire << row.to_hash
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def count
|
33
|
+
@rows.count
|
34
|
+
end
|
35
|
+
|
36
|
+
def get_contents(col)
|
37
|
+
@entire.map {|hash| hash[col]}
|
38
|
+
end
|
39
|
+
|
40
|
+
# Before calling this method, `parse` has to be called first.
|
41
|
+
def create(model, hash_arr=@entire)
|
42
|
+
return if model.nil?
|
43
|
+
for hash in hash_arr
|
44
|
+
model.create(hash)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/csv_manager.rb
ADDED
metadata
ADDED
@@ -0,0 +1,174 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: csv_manager
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1.pre.rc.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gahee Heo
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-01-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sqlite3
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: 1.3.13
|
37
|
+
type: :development
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - "~>"
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '1.3'
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 1.3.13
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: railties
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 4.2.0
|
54
|
+
- - "<"
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '5.1'
|
57
|
+
type: :development
|
58
|
+
prerelease: false
|
59
|
+
version_requirements: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: 4.2.0
|
64
|
+
- - "<"
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '5.1'
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
name: bundler
|
69
|
+
requirement: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - "~>"
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '1.13'
|
74
|
+
type: :development
|
75
|
+
prerelease: false
|
76
|
+
version_requirements: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - "~>"
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '1.13'
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: rake
|
83
|
+
requirement: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - "~>"
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '10.0'
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - "~>"
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '10.0'
|
95
|
+
- !ruby/object:Gem::Dependency
|
96
|
+
name: rspec
|
97
|
+
requirement: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - "~>"
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '3.0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - "~>"
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '3.0'
|
109
|
+
- !ruby/object:Gem::Dependency
|
110
|
+
name: pry
|
111
|
+
requirement: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - "~>"
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: 0.10.4
|
116
|
+
type: :development
|
117
|
+
prerelease: false
|
118
|
+
version_requirements: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - "~>"
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: 0.10.4
|
123
|
+
description: Import and export a CSV file in a very easy and simple way
|
124
|
+
email:
|
125
|
+
- ghbooth12@gmail.com
|
126
|
+
executables: []
|
127
|
+
extensions: []
|
128
|
+
extra_rdoc_files: []
|
129
|
+
files:
|
130
|
+
- ".gitignore"
|
131
|
+
- ".rspec"
|
132
|
+
- ".travis.yml"
|
133
|
+
- CODE_OF_CONDUCT.md
|
134
|
+
- Gemfile
|
135
|
+
- LICENSE.txt
|
136
|
+
- README.md
|
137
|
+
- Rakefile
|
138
|
+
- bin/console
|
139
|
+
- bin/setup
|
140
|
+
- csv_manager.gemspec
|
141
|
+
- doc/create_custom_headers.md
|
142
|
+
- doc/export_download.md
|
143
|
+
- doc/import_choose_csv_and_parse.md
|
144
|
+
- doc/import_populate_database.md
|
145
|
+
- doc/import_specify_csv_path.md
|
146
|
+
- lib/csv_manager.rb
|
147
|
+
- lib/csv_manager/export.rb
|
148
|
+
- lib/csv_manager/import.rb
|
149
|
+
- lib/csv_manager/version.rb
|
150
|
+
homepage: https://github.com/ghbooth12/csv_manager
|
151
|
+
licenses:
|
152
|
+
- MIT
|
153
|
+
metadata: {}
|
154
|
+
post_install_message:
|
155
|
+
rdoc_options: []
|
156
|
+
require_paths:
|
157
|
+
- lib
|
158
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
159
|
+
requirements:
|
160
|
+
- - ">="
|
161
|
+
- !ruby/object:Gem::Version
|
162
|
+
version: '0'
|
163
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
164
|
+
requirements:
|
165
|
+
- - ">"
|
166
|
+
- !ruby/object:Gem::Version
|
167
|
+
version: 1.3.1
|
168
|
+
requirements: []
|
169
|
+
rubyforge_project:
|
170
|
+
rubygems_version: 2.5.2
|
171
|
+
signing_key:
|
172
|
+
specification_version: 4
|
173
|
+
summary: Import and export a CSV file
|
174
|
+
test_files: []
|