importeroo 0.1.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/Gemfile +10 -0
- data/Gemfile.lock +77 -0
- data/LICENSE +22 -0
- data/README.md +33 -0
- data/Rakefile +7 -0
- data/importeroo.gemspec +24 -0
- data/lib/importeroo/importer.rb +68 -0
- data/lib/importeroo/version.rb +3 -0
- data/spec/fixtures/bicycles.csv +5 -0
- data/spec/fixtures/bicycles.xlsx +0 -0
- data/spec/importeroo/importer_spec.rb +44 -0
- data/spec/spec_helper.rb +13 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ad1f196c48911508eed721f788d8d17c7867e73f
|
4
|
+
data.tar.gz: 5358b4f8fff7c6ef1f76ae9250a07884fc28a605
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c3ef0330aee79d9f6c8171bf6c35466f74ab921722616e4e01a5d4063d6469235e9bb850acb4934ed1490a6fd688d9a4ec7985d1ff9e6fc1b11021dca8159f86
|
7
|
+
data.tar.gz: 0dd95f02d267aeb10f623ef20360a0dcb2bdb97b817be672dc17b75ce3aec3e06d12f8c79b1efef7853b380c0e2f8efeeb3219a23ad03f5a7a2885c8ff1bc7a7
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
importeroo (0.1.0)
|
5
|
+
roo
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (4.0.0)
|
11
|
+
activesupport (= 4.0.0)
|
12
|
+
builder (~> 3.1.0)
|
13
|
+
activerecord (4.0.0)
|
14
|
+
activemodel (= 4.0.0)
|
15
|
+
activerecord-deprecated_finders (~> 1.0.2)
|
16
|
+
activesupport (= 4.0.0)
|
17
|
+
arel (~> 4.0.0)
|
18
|
+
activerecord-deprecated_finders (1.0.3)
|
19
|
+
activesupport (4.0.0)
|
20
|
+
i18n (~> 0.6, >= 0.6.4)
|
21
|
+
minitest (~> 4.2)
|
22
|
+
multi_json (~> 1.3)
|
23
|
+
thread_safe (~> 0.1)
|
24
|
+
tzinfo (~> 0.3.37)
|
25
|
+
arel (4.0.0)
|
26
|
+
atomic (1.1.10)
|
27
|
+
builder (3.1.4)
|
28
|
+
coderay (1.0.9)
|
29
|
+
diff-lcs (1.2.4)
|
30
|
+
i18n (0.6.4)
|
31
|
+
method_source (0.8.1)
|
32
|
+
mini_portile (0.5.1)
|
33
|
+
minitest (4.7.5)
|
34
|
+
multi_json (1.7.7)
|
35
|
+
nokogiri (1.6.0)
|
36
|
+
mini_portile (~> 0.5.0)
|
37
|
+
pry (0.9.12.2)
|
38
|
+
coderay (~> 1.0.5)
|
39
|
+
method_source (~> 0.8)
|
40
|
+
slop (~> 3.4)
|
41
|
+
rake (10.1.0)
|
42
|
+
roo (1.11.2)
|
43
|
+
nokogiri
|
44
|
+
rubyzip
|
45
|
+
spreadsheet (> 0.6.4)
|
46
|
+
rspec (2.14.1)
|
47
|
+
rspec-core (~> 2.14.0)
|
48
|
+
rspec-expectations (~> 2.14.0)
|
49
|
+
rspec-mocks (~> 2.14.0)
|
50
|
+
rspec-core (2.14.4)
|
51
|
+
rspec-expectations (2.14.0)
|
52
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
53
|
+
rspec-mocks (2.14.1)
|
54
|
+
ruby-ole (1.2.11.7)
|
55
|
+
rubyzip (0.9.9)
|
56
|
+
slop (3.4.5)
|
57
|
+
spreadsheet (0.8.6)
|
58
|
+
ruby-ole (>= 1.0)
|
59
|
+
sqlite3 (1.3.7)
|
60
|
+
thread_safe (0.1.2)
|
61
|
+
atomic
|
62
|
+
tzinfo (0.3.37)
|
63
|
+
with_model (0.3.2)
|
64
|
+
activerecord (>= 2.3.5, < 5.0.0)
|
65
|
+
rspec (~> 2.11)
|
66
|
+
|
67
|
+
PLATFORMS
|
68
|
+
ruby
|
69
|
+
|
70
|
+
DEPENDENCIES
|
71
|
+
activerecord-jdbcsqlite3-adapter
|
72
|
+
importeroo!
|
73
|
+
pry
|
74
|
+
rake
|
75
|
+
rspec (~> 2.11)
|
76
|
+
sqlite3
|
77
|
+
with_model (~> 0.3)
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Case Commons, LLC
|
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,33 @@
|
|
1
|
+
# importeroo
|
2
|
+
|
3
|
+
A simple gem to load ActiveRecord seeds files from a Excel Spreadsheet, OpenOffice Spreadsheet, or CSV.
|
4
|
+
Coming soon: Google Drive Spreadsheets.
|
5
|
+
Note: this will deletes any existing data in the seed tables before importing.
|
6
|
+
|
7
|
+
## Requirements
|
8
|
+
* Ruby 1.9
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add to gemfile:
|
13
|
+
|
14
|
+
gem 'importeroo', :git => "git@github.com:tracedwax/importeroo.git"
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
In db/seeds.rb:
|
19
|
+
|
20
|
+
require 'importeroo/importer'
|
21
|
+
|
22
|
+
Importeroo::Importer.new(MyActiveRecordClass, "FileType", "path/to/file").import!
|
23
|
+
|
24
|
+
Options are:
|
25
|
+
|
26
|
+
Importeroo::Importer.new(MyActiveRecordClass, "CSV", "path/to/file.csv")
|
27
|
+
Importeroo::Importer.new(MyActiveRecordClass, "Excelx", "path/to/file.xlsx") # current Excel
|
28
|
+
Importeroo::Importer.new(MyActiveRecordClass, "Excel", "path/to/file.xls") # old Excel
|
29
|
+
Importeroo::Importer.new(MyActiveRecordClass, "OpenOffice", "path/to/file.ods")
|
30
|
+
|
31
|
+
Recommended path to file:
|
32
|
+
data/import/my_active_record_class_pluralized.csv
|
33
|
+
|
data/Rakefile
ADDED
data/importeroo.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/importeroo/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Trace Wax", "Eric Hu"]
|
6
|
+
gem.email = ["gems@tracedwax.com"]
|
7
|
+
gem.summary = "Import items from a CSV into an activerecord model"
|
8
|
+
gem.homepage = "https://github.com/tracedwax/importeroo"
|
9
|
+
|
10
|
+
gem.files = `git ls-files`.split($\)
|
11
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
12
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
13
|
+
gem.name = "importeroo"
|
14
|
+
gem.require_paths = ["lib"]
|
15
|
+
gem.version = Importeroo::VERSION
|
16
|
+
gem.license = "MIT"
|
17
|
+
|
18
|
+
gem.required_ruby_version = '>= 1.9.2'
|
19
|
+
|
20
|
+
gem.add_dependency 'roo'
|
21
|
+
|
22
|
+
gem.add_development_dependency 'rspec', '~> 2.11'
|
23
|
+
gem.add_development_dependency 'with_model', '~> 0.3'
|
24
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'roo'
|
2
|
+
require 'pry'
|
3
|
+
|
4
|
+
module Importeroo
|
5
|
+
class Importer < Struct.new(:klass, :data_source_type, :data_source)
|
6
|
+
FIELDS_TO_EXCLUDE = ["created_at", "updated_at"]
|
7
|
+
|
8
|
+
def import!
|
9
|
+
ActiveRecord::Base.transaction do
|
10
|
+
ActiveRecord::Base.connection.execute("DELETE FROM #{klass.table_name}")
|
11
|
+
|
12
|
+
(2..data.last_row).each do |row_num|
|
13
|
+
import_row! data.row(row_num)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def data
|
21
|
+
@data ||= roo_class.new(data_source, nil, :ignore)
|
22
|
+
end
|
23
|
+
|
24
|
+
def roo_class
|
25
|
+
"Roo::#{data_source_type.titleize}".constantize
|
26
|
+
end
|
27
|
+
|
28
|
+
def import_row!(row)
|
29
|
+
record = klass.new
|
30
|
+
|
31
|
+
fields.each do |field|
|
32
|
+
record.public_send(field.assignment_method_name, row[field.column_num])
|
33
|
+
end
|
34
|
+
raise ActiveRecord::Rollback unless record.save
|
35
|
+
end
|
36
|
+
|
37
|
+
def fields
|
38
|
+
@fields ||= set_fields
|
39
|
+
end
|
40
|
+
|
41
|
+
def set_fields
|
42
|
+
field_list.map do |field_pair|
|
43
|
+
field_name = field_pair[0].downcase.gsub(" ", "_")
|
44
|
+
column_num = field_pair[1]
|
45
|
+
|
46
|
+
Field.new(column_num, field_name) unless FIELDS_TO_EXCLUDE.include?(field_name)
|
47
|
+
end.compact
|
48
|
+
end
|
49
|
+
|
50
|
+
def all_fields
|
51
|
+
data.row 1
|
52
|
+
end
|
53
|
+
|
54
|
+
def field_list
|
55
|
+
all_fields.to_enum.with_index(0).to_a
|
56
|
+
end
|
57
|
+
|
58
|
+
class Field < Struct.new(:column_num, :name)
|
59
|
+
def assignment_method_name
|
60
|
+
"#{name}="
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def underscore(string)
|
65
|
+
string.gsub(/(.)([A-Z])/,'\1_\2').downcase + "s"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
Binary file
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'importeroo/importer'
|
3
|
+
|
4
|
+
describe Importeroo::Importer do
|
5
|
+
with_model :BicycleType do
|
6
|
+
table do |t|
|
7
|
+
t.string :bicycle_type
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "#import!" do
|
12
|
+
context "when a CSV" do
|
13
|
+
it "deletes old data and imports the new data" do
|
14
|
+
BicycleType.create(id: 5, bicycle_type: "velocipede")
|
15
|
+
|
16
|
+
described_class.new(BicycleType, "CSV", "spec/fixtures/bicycles.csv").import!
|
17
|
+
|
18
|
+
BicycleType.count.should == 4
|
19
|
+
BicycleType.find(1).bicycle_type.should == "road bike"
|
20
|
+
BicycleType.find(2).bicycle_type.should == "mountain bike"
|
21
|
+
BicycleType.find(3).bicycle_type.should == "touring bike"
|
22
|
+
BicycleType.find(4).bicycle_type.should == "beach cruiser"
|
23
|
+
|
24
|
+
BicycleType.all.map(&:bicycle_type).should_not include("velocipede")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "when an Excel file" do
|
29
|
+
it "deletes old data and imports the new data" do
|
30
|
+
BicycleType.create(id: 5, bicycle_type: "velocipede")
|
31
|
+
|
32
|
+
described_class.new(BicycleType, "Excelx", "spec/fixtures/bicycles.xlsx").import!
|
33
|
+
|
34
|
+
BicycleType.count.should == 4
|
35
|
+
BicycleType.find(1).bicycle_type.should == "road bike"
|
36
|
+
BicycleType.find(2).bicycle_type.should == "mountain bike"
|
37
|
+
BicycleType.find(3).bicycle_type.should == "touring bike"
|
38
|
+
BicycleType.find(4).bicycle_type.should == "beach cruiser"
|
39
|
+
|
40
|
+
BicycleType.all.map(&:bicycle_type).should_not include("velocipede")
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require "active_record"
|
2
|
+
require "with_model"
|
3
|
+
|
4
|
+
RSpec.configure do |config|
|
5
|
+
config.extend WithModel
|
6
|
+
end
|
7
|
+
|
8
|
+
jruby = RUBY_PLATFORM =~ /\bjava\b/
|
9
|
+
adapter = jruby ? "jdbcsqlite3" : "sqlite3"
|
10
|
+
|
11
|
+
# Importeroo requires ActiveRecord::Base.connection to be established.
|
12
|
+
# If ActiveRecord already has a connection, as in a Rails app, this is unnecessary.
|
13
|
+
ActiveRecord::Base.establish_connection(:adapter => adapter, :database => ":memory:")
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: importeroo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Trace Wax
|
8
|
+
- Eric Hu
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-07-24 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: roo
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - '>='
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - '>='
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rspec
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '2.11'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ~>
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '2.11'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: with_model
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ~>
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0.3'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ~>
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0.3'
|
56
|
+
description:
|
57
|
+
email:
|
58
|
+
- gems@tracedwax.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- Gemfile
|
64
|
+
- Gemfile.lock
|
65
|
+
- LICENSE
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- importeroo.gemspec
|
69
|
+
- lib/importeroo/importer.rb
|
70
|
+
- lib/importeroo/version.rb
|
71
|
+
- spec/fixtures/bicycles.csv
|
72
|
+
- spec/fixtures/bicycles.xlsx
|
73
|
+
- spec/importeroo/importer_spec.rb
|
74
|
+
- spec/spec_helper.rb
|
75
|
+
homepage: https://github.com/tracedwax/importeroo
|
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: 1.9.2
|
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.0.3
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: Import items from a CSV into an activerecord model
|
99
|
+
test_files:
|
100
|
+
- spec/fixtures/bicycles.csv
|
101
|
+
- spec/fixtures/bicycles.xlsx
|
102
|
+
- spec/importeroo/importer_spec.rb
|
103
|
+
- spec/spec_helper.rb
|