Csv-Processor 1.0.0
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/lib/csv_processor.rb +40 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 50b27a1a14a93d1e16ea826e8f2eb31603ef8771
|
4
|
+
data.tar.gz: 112138b4baff1f1f07d8e1d07f07a18bc044839b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6d5ad8f5ec303c8204d4e16fa04d68338f9a9474dbc2d332b16c09743549fa9b1e9a09d92d383ba0db8b150939ae715b4ca85ffcce061efb43fae7e55b238a8a
|
7
|
+
data.tar.gz: 14bf373c041a63b322a34966cde7b54bf5789513a7db608e4fa3635a6a8ac02c8a8f9761e94f43c8641b992ec622e24f0c04fcddafc73cf2566a68ce5afabb50
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'csv'
|
2
|
+
module CsvProcessor
|
3
|
+
|
4
|
+
def import_from(csv_path, only = {}, except = {})
|
5
|
+
raise("Method does not accept only and except params at once") if !only.empty? && !except.empty?
|
6
|
+
csv_text = File.read(csv_path)
|
7
|
+
csv = CSV.parse(csv_text, :headers => true)
|
8
|
+
only_except_logic(csv, only, except)
|
9
|
+
csv.each do |row|
|
10
|
+
self.create!(row.to_hash)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_csv(options = {}, only = {}, except = {})
|
15
|
+
raise("Method does not accept only and except params at once") if !only.empty? && !except.empty?
|
16
|
+
CSV.generate(options) do |csv|
|
17
|
+
column_names = self.column_names
|
18
|
+
only_except_logic(column_names, only, except)
|
19
|
+
csv << column_names
|
20
|
+
all.each do |order|
|
21
|
+
csv << order.attributes.values_at(*column_names)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
def only_except_logic(str, only, except)
|
28
|
+
if only.empty? && !except.empty?
|
29
|
+
except.each do |key|
|
30
|
+
str.delete(key.to_s)
|
31
|
+
end
|
32
|
+
elsif !only.empty? && except.empty?
|
33
|
+
str = []
|
34
|
+
only.each do |key|
|
35
|
+
str << key.to_s
|
36
|
+
end
|
37
|
+
else
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: Csv-Processor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tri Bui
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-14 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: This is a tool to import data from csv file and export data to csv format
|
14
|
+
email: bui.minh.tri.318@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/csv_processor.rb
|
20
|
+
homepage: http://rubygems.org/gems/csv_processor
|
21
|
+
licenses:
|
22
|
+
- Tri Bui
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.2.2
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: CSV Export and Import
|
44
|
+
test_files: []
|