csv_to_seed 0.1.0 → 0.2.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 +4 -4
- data/.DS_Store +0 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +1 -16
- data/{README.rdoc → README.md} +12 -3
- data/Rakefile +5 -5
- data/VERSION +1 -1
- data/csv_to_seed.gemspec +10 -9
- data/lib/csv_to_seed.rb +30 -14
- data/test/csv_files/test.csv +16 -0
- data/test/helper.rb +4 -21
- data/test/test_csv_to_seed.rb +45 -3
- metadata +7 -6
- data/test.csv +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ce13d20ba955a635825bd21285b9d61be1d8da7a
|
4
|
+
data.tar.gz: 788d1958efd9df6509369cf169e45dc3ae365ec1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a9a2c6c62c10e717b992f3657d31f0222189c61419c12f11927cd1a35643fa8d36e788754cf07fd5bc0217d3700bca4c6d59c1ba21dfccca58700290028d5f4c
|
7
|
+
data.tar.gz: 2cc4fae5e7ec3c7fb40ce7ad1ac52813969b754d53052b3c586d3b2d5c7e44ce46a81aee95b283e97ddbc9543d1620bfbbd99fc22d9b91a5d07c05ab744fd538
|
data/.DS_Store
ADDED
Binary file
|
data/Gemfile
CHANGED
@@ -6,7 +6,7 @@ source "http://rubygems.org"
|
|
6
6
|
# Add dependencies to develop your gem here.
|
7
7
|
# Include everything needed to run rake, tests, features, etc.
|
8
8
|
group :development do
|
9
|
-
gem "
|
9
|
+
gem "minitest"
|
10
10
|
gem "rdoc", "~> 3.12"
|
11
11
|
gem "bundler", "~> 1.0"
|
12
12
|
gem "jeweler", "~> 2.0.1"
|
data/Gemfile.lock
CHANGED
@@ -1,12 +1,6 @@
|
|
1
1
|
GEM
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
|
-
activesupport (4.1.1)
|
5
|
-
i18n (~> 0.6, >= 0.6.9)
|
6
|
-
json (~> 1.7, >= 1.7.7)
|
7
|
-
minitest (~> 5.1)
|
8
|
-
thread_safe (~> 0.1)
|
9
|
-
tzinfo (~> 1.1)
|
10
4
|
addressable (2.3.6)
|
11
5
|
builder (3.2.2)
|
12
6
|
descendants_tracker (0.0.4)
|
@@ -25,7 +19,6 @@ GEM
|
|
25
19
|
oauth2
|
26
20
|
hashie (2.1.1)
|
27
21
|
highline (1.6.21)
|
28
|
-
i18n (0.6.9)
|
29
22
|
jeweler (2.0.1)
|
30
23
|
builder
|
31
24
|
bundler (>= 1.0)
|
@@ -55,20 +48,12 @@ GEM
|
|
55
48
|
rake (10.3.1)
|
56
49
|
rdoc (3.12.2)
|
57
50
|
json (~> 1.4)
|
58
|
-
shoulda (3.5.0)
|
59
|
-
shoulda-context (~> 1.0, >= 1.0.1)
|
60
|
-
shoulda-matchers (>= 1.4.1, < 3.0)
|
61
|
-
shoulda-context (1.2.1)
|
62
|
-
shoulda-matchers (2.6.1)
|
63
|
-
activesupport (>= 3.0.0)
|
64
51
|
simplecov (0.8.2)
|
65
52
|
docile (~> 1.1.0)
|
66
53
|
multi_json
|
67
54
|
simplecov-html (~> 0.8.0)
|
68
55
|
simplecov-html (0.8.0)
|
69
56
|
thread_safe (0.3.3)
|
70
|
-
tzinfo (1.1.0)
|
71
|
-
thread_safe (~> 0.1)
|
72
57
|
|
73
58
|
PLATFORMS
|
74
59
|
ruby
|
@@ -76,6 +61,6 @@ PLATFORMS
|
|
76
61
|
DEPENDENCIES
|
77
62
|
bundler (~> 1.0)
|
78
63
|
jeweler (~> 2.0.1)
|
64
|
+
minitest
|
79
65
|
rdoc (~> 3.12)
|
80
|
-
shoulda
|
81
66
|
simplecov
|
data/{README.rdoc → README.md}
RENAMED
@@ -1,9 +1,19 @@
|
|
1
1
|
= csv_to_seed
|
2
2
|
|
3
|
-
|
3
|
+
This gem is a basic tool for those who get annoyed by parsing CSVs file to import them to their DBs,
|
4
|
+
instead this script just read the CSV file and write an array based on the file to seed.rb for Rails Apps.
|
5
|
+
|
6
|
+
```
|
7
|
+
$ csv_to_seed 'path_to_CSV_file' 'name_of_the_array'
|
8
|
+
```
|
9
|
+
|
10
|
+
@TODO
|
11
|
+
- Add testing to the class
|
12
|
+
- Unhardcode the structure to insert
|
13
|
+
- Update the README
|
4
14
|
|
5
15
|
== Contributing to csv_to_seed
|
6
|
-
|
16
|
+
|
7
17
|
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
8
18
|
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
9
19
|
* Fork the project.
|
@@ -16,4 +26,3 @@ Description goes here.
|
|
16
26
|
|
17
27
|
Copyright (c) 2014 JoseLuis Torres. See LICENSE.txt for
|
18
28
|
further details.
|
19
|
-
|
data/Rakefile
CHANGED
@@ -32,11 +32,11 @@ Rake::TestTask.new(:test) do |test|
|
|
32
32
|
test.verbose = true
|
33
33
|
end
|
34
34
|
|
35
|
-
desc "Code coverage detail"
|
36
|
-
task :simplecov do
|
37
|
-
|
38
|
-
|
39
|
-
end
|
35
|
+
# desc "Code coverage detail"
|
36
|
+
# task :simplecov do
|
37
|
+
# ENV['COVERAGE'] = "true"
|
38
|
+
# Rake::Task['test'].execute
|
39
|
+
# end
|
40
40
|
|
41
41
|
task :default => :test
|
42
42
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/csv_to_seed.gemspec
CHANGED
@@ -2,36 +2,37 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: csv_to_seed 0.
|
5
|
+
# stub: csv_to_seed 0.2.0 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "csv_to_seed"
|
9
|
-
s.version = "0.
|
9
|
+
s.version = "0.2.0"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib"]
|
13
13
|
s.authors = ["JoseLuis Torres"]
|
14
|
-
s.date = "2014-
|
14
|
+
s.date = "2014-06-02"
|
15
15
|
s.description = "Utility to get a CSV file into seed.rb"
|
16
16
|
s.email = "jl@joseluistorres.me"
|
17
17
|
s.executables = ["csv_to_seed"]
|
18
18
|
s.extra_rdoc_files = [
|
19
19
|
"LICENSE.txt",
|
20
|
-
"README.
|
20
|
+
"README.md"
|
21
21
|
]
|
22
22
|
s.files = [
|
23
|
+
".DS_Store",
|
23
24
|
".document",
|
24
25
|
"Gemfile",
|
25
26
|
"Gemfile.lock",
|
26
27
|
"LICENSE.txt",
|
27
|
-
"README.
|
28
|
+
"README.md",
|
28
29
|
"Rakefile",
|
29
30
|
"VERSION",
|
30
31
|
"bin/csv_to_seed",
|
31
32
|
"csv_to_seed.gemspec",
|
32
33
|
"db/seeds.rb",
|
33
34
|
"lib/csv_to_seed.rb",
|
34
|
-
"test.csv",
|
35
|
+
"test/csv_files/test.csv",
|
35
36
|
"test/helper.rb",
|
36
37
|
"test/test_csv_to_seed.rb"
|
37
38
|
]
|
@@ -44,20 +45,20 @@ Gem::Specification.new do |s|
|
|
44
45
|
s.specification_version = 4
|
45
46
|
|
46
47
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
47
|
-
s.add_development_dependency(%q<
|
48
|
+
s.add_development_dependency(%q<minitest>, [">= 0"])
|
48
49
|
s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
|
49
50
|
s.add_development_dependency(%q<bundler>, ["~> 1.0"])
|
50
51
|
s.add_development_dependency(%q<jeweler>, ["~> 2.0.1"])
|
51
52
|
s.add_development_dependency(%q<simplecov>, [">= 0"])
|
52
53
|
else
|
53
|
-
s.add_dependency(%q<
|
54
|
+
s.add_dependency(%q<minitest>, [">= 0"])
|
54
55
|
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
55
56
|
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
56
57
|
s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
|
57
58
|
s.add_dependency(%q<simplecov>, [">= 0"])
|
58
59
|
end
|
59
60
|
else
|
60
|
-
s.add_dependency(%q<
|
61
|
+
s.add_dependency(%q<minitest>, [">= 0"])
|
61
62
|
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
62
63
|
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
63
64
|
s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
|
data/lib/csv_to_seed.rb
CHANGED
@@ -1,16 +1,21 @@
|
|
1
1
|
require 'csv'
|
2
2
|
|
3
3
|
class CSVToSeed
|
4
|
-
attr_accessor :file, :headers
|
4
|
+
attr_accessor :file, :headers, :name_of_array
|
5
5
|
|
6
6
|
def initialize(args)
|
7
7
|
@csv_path_to_file = args[:csv_path_to_file]
|
8
8
|
@name_of_array = args[:name_of_array]
|
9
|
+
validate_name_of_array(@name_of_array)
|
9
10
|
end
|
10
11
|
|
11
12
|
def get_csv_body
|
12
|
-
|
13
|
-
|
13
|
+
begin
|
14
|
+
@file = File.open(@csv_path_to_file, "rb")
|
15
|
+
@file.read
|
16
|
+
rescue
|
17
|
+
raise 'Invalid file path'
|
18
|
+
end
|
14
19
|
end
|
15
20
|
|
16
21
|
def csv_object
|
@@ -37,22 +42,33 @@ class CSVToSeed
|
|
37
42
|
.gsub(/\}\,/, "\s},")
|
38
43
|
end
|
39
44
|
|
45
|
+
def validate_name_of_array(value)
|
46
|
+
raise 'Must be a valid variable name' if (value =~ /^[a-z_][a-zA-Z_0-9]*$/).nil?
|
47
|
+
end
|
48
|
+
|
40
49
|
def set_string_to_create_loop
|
41
|
-
|
42
|
-
|
43
|
-
Static::SupportPracticeLu.find_or_initialize_by_name(attributes[:name]).tap do |support_practice|
|
44
|
-
support_practice.name = attributes[:name]
|
45
|
-
support_practice.support_practice_type_id = attributes[:support_practice_type_id]
|
46
|
-
support_practice.number = attributes[:number]
|
47
|
-
support_practice.width = attributes[:width]
|
48
|
-
support_practice.save!
|
50
|
+
if @headers.empty?
|
51
|
+
csv_to_hash
|
49
52
|
end
|
50
|
-
|
51
|
-
|
53
|
+
inside_vars = ''
|
54
|
+
@headers.each do |header|
|
55
|
+
inside_vars = inside_vars +
|
56
|
+
<<-TEXT
|
57
|
+
support_practice.#{header.to_s} = attributes[:#{header.to_s}]
|
58
|
+
TEXT
|
59
|
+
end
|
60
|
+
<<-TEXT
|
61
|
+
#{@name_of_array}.each do |attributes|
|
62
|
+
Static::SupportPracticeLu.find_or_initialize_by_name(attributes[:name]).tap do |support_practice|
|
63
|
+
#{inside_vars}
|
64
|
+
support_practice.save!
|
65
|
+
end
|
66
|
+
end
|
67
|
+
TEXT
|
52
68
|
end
|
53
69
|
|
54
70
|
def write_seedrb_file
|
55
71
|
File.write(Dir.pwd + '/db/seeds.rb', "\n\r#{@name_of_array} = #{csv_to_hash} \n\r #{set_string_to_create_loop}", File.size(Dir.pwd + '/db/seeds.rb'), mode: 'a')
|
56
72
|
@file.close
|
57
73
|
end
|
58
|
-
end
|
74
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
name,path,support_practice_type_id,number,width
|
2
|
+
0.05% grade channel,nuevo path.18,1,null
|
3
|
+
0.05% grade channel-impoundment,nuevo path,18,1,null
|
4
|
+
0.1% grade channel,nuevo path,18,1,null
|
5
|
+
0.1% grade channel-impoundment,nuevo path,18,1,null
|
6
|
+
0.2% grade channel,nuevo path,18,1,null
|
7
|
+
0.2% grade channel-impoundment,nuevo path,18,1,null
|
8
|
+
0.3% grade channel,nuevo path,18,1,null
|
9
|
+
0.3% grade channel-impoundment,nuevo path,18,1,null
|
10
|
+
0.4% grade channel,nuevo path,18,1,null
|
11
|
+
0.4% grade channel-impoundment,nuevo path,18,1,null
|
12
|
+
0.5% grade channel,nuevo path,18,1,null
|
13
|
+
0.5% grade channel-impoundment,nuevo path,18,1,null
|
14
|
+
0.6% grade channel,nuevo path,18,1,null
|
15
|
+
0.6% grade channel-impoundment,nuevo path,18,1,null
|
16
|
+
0.75% grade channel,nuevo path,18,1,null
|
data/test/helper.rb
CHANGED
@@ -1,19 +1,3 @@
|
|
1
|
-
require 'simplecov'
|
2
|
-
|
3
|
-
module SimpleCov::Configuration
|
4
|
-
def clean_filters
|
5
|
-
@filters = []
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
9
|
-
SimpleCov.configure do
|
10
|
-
clean_filters
|
11
|
-
load_adapter 'test_frameworks'
|
12
|
-
end
|
13
|
-
|
14
|
-
ENV["COVERAGE"] && SimpleCov.start do
|
15
|
-
add_filter "/.rvm/"
|
16
|
-
end
|
17
1
|
require 'rubygems'
|
18
2
|
require 'bundler'
|
19
3
|
begin
|
@@ -23,12 +7,11 @@ rescue Bundler::BundlerError => e
|
|
23
7
|
$stderr.puts "Run `bundle install` to install missing gems"
|
24
8
|
exit e.status_code
|
25
9
|
end
|
26
|
-
|
27
|
-
require
|
10
|
+
|
11
|
+
require "minitest"
|
12
|
+
require 'minitest/spec'
|
13
|
+
require 'minitest/autorun'
|
28
14
|
|
29
15
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
30
16
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
31
17
|
require 'csv_to_seed'
|
32
|
-
|
33
|
-
class Test::Unit::TestCase
|
34
|
-
end
|
data/test/test_csv_to_seed.rb
CHANGED
@@ -1,7 +1,49 @@
|
|
1
1
|
require 'helper'
|
2
|
+
#Dir["test/rules/*.rb"].each {|file| require './' + file }
|
3
|
+
#Dir["test/test_classes/*.rb"].each {|file| require './' + file }
|
2
4
|
|
3
|
-
|
4
|
-
|
5
|
-
|
5
|
+
describe CSVToSeed do
|
6
|
+
before do
|
7
|
+
@csv_to_seed = CSVToSeed.new({ :csv_path_to_file => 'test/csv_files/test.csv', :name_of_array => 'my_test' })
|
8
|
+
@csv_to_seed_broken = CSVToSeed.new({ :csv_path_to_file => 'missing.csv', :name_of_array => 'my_test' })
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should read a CSV file" do
|
12
|
+
@csv_to_seed.get_csv_body.must_match /name,path,support_practice_type_id,number,width/
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should show an error when the CSV file is missing" do
|
16
|
+
error = -> { @csv_to_seed_broken.get_csv_body }.must_raise RuntimeError
|
17
|
+
error.message.must_match /Invalid file path/
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should have a valid CSV object " do
|
21
|
+
@csv_to_seed.csv_object.must_be_instance_of CSV
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should have a valid hash object before writing" do
|
25
|
+
@csv_to_seed.csv_to_hash.must_be_instance_of String
|
26
|
+
@csv_to_seed.csv_to_hash.must_match /\n\r\{\ \:name\=\>\"0.05\%\ grade\ channel\"\,/
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should have a valid name of array" do
|
30
|
+
@csv_to_seed.name_of_array.must_match /^[a-z_][a-zA-Z_0-9]*$/
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should raise an error when a name of array is invalid" do
|
34
|
+
error =-> { CSVToSeed.new({ :csv_path_to_file => 'test.csv', :name_of_array => '123my_test' }) }.must_raise RuntimeError
|
35
|
+
error.message.must_match /Must be a valid variable name/
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should read the first row as headers" do
|
39
|
+
# name,support_practice_type_id,number,width
|
40
|
+
@csv_to_seed.csv_to_hash
|
41
|
+
@csv_to_seed.headers.must_be_instance_of Array
|
42
|
+
@csv_to_seed.headers.must_include :support_practice_type_id
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should create a string for the loop with headers" do
|
46
|
+
@csv_to_seed.csv_to_hash
|
47
|
+
@csv_to_seed.set_string_to_create_loop.must_match /\.path\s\=\sattributes\[\:path\]/
|
6
48
|
end
|
7
49
|
end
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: csv_to_seed
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- JoseLuis Torres
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-06-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: minitest
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - '>='
|
@@ -87,20 +87,21 @@ executables:
|
|
87
87
|
extensions: []
|
88
88
|
extra_rdoc_files:
|
89
89
|
- LICENSE.txt
|
90
|
-
- README.
|
90
|
+
- README.md
|
91
91
|
files:
|
92
|
+
- .DS_Store
|
92
93
|
- .document
|
93
94
|
- Gemfile
|
94
95
|
- Gemfile.lock
|
95
96
|
- LICENSE.txt
|
96
|
-
- README.
|
97
|
+
- README.md
|
97
98
|
- Rakefile
|
98
99
|
- VERSION
|
99
100
|
- bin/csv_to_seed
|
100
101
|
- csv_to_seed.gemspec
|
101
102
|
- db/seeds.rb
|
102
103
|
- lib/csv_to_seed.rb
|
103
|
-
- test.csv
|
104
|
+
- test/csv_files/test.csv
|
104
105
|
- test/helper.rb
|
105
106
|
- test/test_csv_to_seed.rb
|
106
107
|
homepage: http://github.com/joseluistorres/csv_to_seed
|
data/test.csv
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
name,support_practice_type_id,number,width
|
2
|
-
0.05% grade channel,18,1,null
|
3
|
-
0.05% grade channel-impoundment,18,1,null
|
4
|
-
0.1% grade channel,18,1,null
|
5
|
-
0.1% grade channel-impoundment,18,1,null
|
6
|
-
0.2% grade channel,18,1,null
|
7
|
-
0.2% grade channel-impoundment,18,1,null
|
8
|
-
0.3% grade channel,18,1,null
|
9
|
-
0.3% grade channel-impoundment,18,1,null
|
10
|
-
0.4% grade channel,18,1,null
|
11
|
-
0.4% grade channel-impoundment,18,1,null
|
12
|
-
0.5% grade channel,18,1,null
|
13
|
-
0.5% grade channel-impoundment,18,1,null
|
14
|
-
0.6% grade channel,18,1,null
|
15
|
-
0.6% grade channel-impoundment,18,1,null
|
16
|
-
0.75% grade channel,18,1,null
|