Commit 0145dfac authored by Immanuel Häussermann's avatar Immanuel Häussermann
Browse files

add default import behavior

parent a162ce34
Pipeline #20561 passed with stage
in 1 minute and 14 seconds
......@@ -2,11 +2,20 @@ PATH
remote: .
specs:
excelsior (0.1.0)
activesupport (~> 5.0)
simple_xlsx_reader (~> 1.0.2)
GEM
remote: https://rubygems.org/
specs:
activesupport (5.2.0)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
concurrent-ruby (1.0.5)
i18n (1.0.1)
concurrent-ruby (~> 1.0)
mini_portile2 (2.3.0)
minitest (5.11.3)
nokogiri (1.8.2)
......@@ -16,6 +25,9 @@ GEM
simple_xlsx_reader (1.0.2)
nokogiri
rubyzip
thread_safe (0.3.6)
tzinfo (1.2.5)
thread_safe (~> 0.1)
PLATFORMS
ruby
......
......@@ -36,10 +36,21 @@ class UserImporter < Excelsior::Importer
end
```
Then create an instance of your import and run it:
Create an instance of your import and run it. By default it infers the model
to be imported from the classname, e.g.:
```ruby
import = UserImport.new # you may also pass a file per instance here
import = UserImport.new
import.run # calls User.create!(row) for each row
```
### Extended API
You may want to pass an excel file per instance. You can also define your own
import behavior by passing a block to the `run` method:
```ruby
import = UserImport.new("users/all.xlsx")
import.run do |row|
User.create!(row) # raise an exception if the data doesn't match your expectations
end
......
......@@ -26,4 +26,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "minitest", "~> 5.0"
spec.add_runtime_dependency "simple_xlsx_reader", "~> 1.0.2"
spec.add_runtime_dependency "activesupport", "~> 5.0"
end
require "simple_xlsx_reader"
require "active_support"
require "active_support/core_ext"
require "excelsior/source"
require "excelsior/mapping"
......@@ -24,9 +25,14 @@ module Excelsior
valid?
end
def run(&block)
def run # takes an optional block
@rows.map do |row|
yield map_row_values(row, @columns)
attributes = map_row_values(row, @columns)
if block_given?
yield attributes
else
model_class.create!(attributes)
end
end
end
......@@ -40,5 +46,11 @@ module Excelsior
acc
end
end
private
def model_class
self.class.name.gsub("Import", "").constantize
end
end
end
......@@ -9,6 +9,19 @@ class UserImport < Excelsior::Import
map "E-Mail", to: :email
end
class User
class << self
def create!(attributes)
@all ||= []
@all << attributes
end
def all
@all ||= []
end
end
end
class ExcelsiorTest < Minitest::Test
def setup
@import = UserImport.new
......@@ -48,6 +61,11 @@ class ExcelsiorTest < Minitest::Test
end
def test_import_run
results = @import.run
assert_equal User.all.size, 2
end
def test_import_run_with_block
results = @import.run { |v| v }
assert_equal results[0], {
firstname: "Hans",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment