rails_spreadsheet_reader 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +38 -0
- data/Rakefile +1 -0
- data/lib/generators/rails/spreadsheet_generator.rb +40 -0
- data/lib/generators/rails/templates/spreadsheet.rb +68 -0
- data/lib/rails_spreadsheet_reader.rb +3 -0
- data/lib/rails_spreadsheet_reader/base.rb +236 -0
- data/lib/rails_spreadsheet_reader/row_collection.rb +31 -0
- data/lib/rails_spreadsheet_reader/version.rb +3 -0
- data/rails_spreadsheet_reader.gemspec +28 -0
- data/spec/base_spec.rb +82 -0
- data/spec/files/users.csv +3 -0
- data/spec/files/users_invalid.csv +9 -0
- data/spec/models/user_invalid_spreadsheet.rb +24 -0
- data/spec/models/user_spreadsheet.rb +11 -0
- data/spec/spec_helper.rb +82 -0
- metadata +152 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8cb068a98505cc537d62613ee7c32c50035f15c4
|
4
|
+
data.tar.gz: a716652b67871f0e27523ee725dbbfdccb50b288
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6586a01666ce199b41deee31f7c3b98f6beea0f95547b5b512f9263c654086ebb1d02ac4489e8bab343b4f5f221189d7123852a4a136df277e6c68d3a31f575d
|
7
|
+
data.tar.gz: e08e369c5df81a842857f704938cdd7a4ad42a8e86bfc0bd5b37df253ba1bf866b6fbb508bdecebf2d27bb60e0da5a76c77297eba558011a34aef47377d820ae
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 muzk
|
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,38 @@
|
|
1
|
+
# RailsSpreadsheetReader
|
2
|
+
|
3
|
+
Provides an easy way to add model-based validations to excel files.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'rails_spreadsheet_reader'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install rails_spreadsheet_reader
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Run
|
22
|
+
$ rails g spreadsheet name
|
23
|
+
|
24
|
+
to create a spreadsheet reader into app/spreadsheet_reader directory.
|
25
|
+
|
26
|
+
Edit the columns, validate_multiple_rows and persist methods depending on your use-case.
|
27
|
+
|
28
|
+
## Example
|
29
|
+
|
30
|
+
TODO: Add example app link
|
31
|
+
|
32
|
+
## Contributing
|
33
|
+
|
34
|
+
1. Fork it
|
35
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
36
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
37
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
38
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
module Rails
|
4
|
+
module Generators
|
5
|
+
class SpreadsheetGenerator < ::Rails::Generators::Base
|
6
|
+
desc 'Creates a *_spreadsheet model in the app/spreadsheet_reader directory.'
|
7
|
+
|
8
|
+
source_root File.expand_path('../templates', __FILE__)
|
9
|
+
argument :name, :type => :string
|
10
|
+
|
11
|
+
def generate_spreadsheet
|
12
|
+
file_prefix = set_filename(name)
|
13
|
+
@spreadsheet_name = set_spreadsheet_name(name)
|
14
|
+
template 'spreadsheet.rb', File.join(
|
15
|
+
'app/spreadsheet_reader', "#{file_prefix}_spreadsheet.rb"
|
16
|
+
)
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def set_filename(name)
|
22
|
+
name.include?('_') ? name : name.to_s.underscore
|
23
|
+
end
|
24
|
+
|
25
|
+
def set_spreadsheet_name(name)
|
26
|
+
name.include?('_') ? build_name(name) : capitalize(name)
|
27
|
+
end
|
28
|
+
|
29
|
+
def build_name(name)
|
30
|
+
pieces = name.split('_')
|
31
|
+
pieces.map(&:titleize).join
|
32
|
+
end
|
33
|
+
|
34
|
+
def capitalize(name)
|
35
|
+
return name if name[0] == name[0].upcase
|
36
|
+
name.capitalize
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
class <%= @spreadsheet_name %>Spreadsheet < RailsSpreadsheetReader::Base
|
2
|
+
|
3
|
+
# add the columns you want to read:
|
4
|
+
# attr_accessor :attr1, :attr2, :attr3
|
5
|
+
|
6
|
+
# add validations to your fields
|
7
|
+
# validates_presence_of :attr1
|
8
|
+
|
9
|
+
# map this model attributes to your spreadsheet attributes
|
10
|
+
# you can use a hash { attr1: 0, attr2: 1, attr3: 2, ... }
|
11
|
+
# or a array %w(attr1 attr2 attr3 ...)
|
12
|
+
|
13
|
+
def self.columns
|
14
|
+
# return { attr1: 0, attr2: 1, ... }
|
15
|
+
end
|
16
|
+
|
17
|
+
# set the row number where the data start, default is 2 (1-based)
|
18
|
+
def self.starting_row
|
19
|
+
2
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.validate_multiple_rows(row_collection)
|
23
|
+
# This method is called when all rows of row_collection are valid. The main
|
24
|
+
# idea of this method is to run validations that have to do with
|
25
|
+
# the set of rows of the excel. For example, you can check here if a
|
26
|
+
# excel column is unique. It have to raise an exception if there was
|
27
|
+
# an error with a certain row.
|
28
|
+
#
|
29
|
+
# Example:
|
30
|
+
#
|
31
|
+
# def self.validate_multiple_rows(row_collection)
|
32
|
+
# username_list = []
|
33
|
+
# row_collection.rows.each do |row|
|
34
|
+
# if username_list.include?(row.username)
|
35
|
+
# row_collection.invalid_row = row
|
36
|
+
# row.errors[:username] = 'is unique'
|
37
|
+
# raise 'Validation Error'
|
38
|
+
# else
|
39
|
+
# username_list << row.username
|
40
|
+
# end
|
41
|
+
# end
|
42
|
+
# end
|
43
|
+
#
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.persist(row_collection)
|
47
|
+
# This method is called after the self.validate_multiple_rows method
|
48
|
+
# only if it have not raised an exception. The idea of this method
|
49
|
+
# is to persist the row's data. If there was an error with a certain
|
50
|
+
# row, you have to Rollback the valid transactions, set the
|
51
|
+
# invalid error to the row_collection and set the row errors to the
|
52
|
+
# row object.
|
53
|
+
#
|
54
|
+
# Example:
|
55
|
+
# def self.persist(row_collection)
|
56
|
+
# User.transaction do
|
57
|
+
# row_collection.rows.each do |row|
|
58
|
+
# user = User.new(attr1: row.attr1, attr2: row.attr2, ...)
|
59
|
+
# unless user.save
|
60
|
+
# row_collection.set_invalid_row(row, user) # pass the model with errors as second parameter
|
61
|
+
# rollback # use the rollback helper to rollback the transaction.
|
62
|
+
# end
|
63
|
+
# end
|
64
|
+
# end
|
65
|
+
# end
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
@@ -0,0 +1,236 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
require 'rails'
|
3
|
+
require 'roo'
|
4
|
+
|
5
|
+
module RailsSpreadsheetReader
|
6
|
+
|
7
|
+
class Base
|
8
|
+
|
9
|
+
include ActiveModel::Model
|
10
|
+
|
11
|
+
class MethodNotImplementedError < StandardError; end
|
12
|
+
class InvalidTypeError < StandardError; end
|
13
|
+
|
14
|
+
attr_accessor :row_number
|
15
|
+
|
16
|
+
# Copy a ActiveModel::Model errors
|
17
|
+
def copy_errors(model)
|
18
|
+
model.errors.each do |key, value|
|
19
|
+
self.errors[key] = value
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Defines the starting row of the excel where the class should start reading the data.
|
24
|
+
#
|
25
|
+
# == Returns:
|
26
|
+
# A integer representing the starting row of the data.
|
27
|
+
#
|
28
|
+
def self.starting_row
|
29
|
+
2
|
30
|
+
end
|
31
|
+
|
32
|
+
# Defines the columns of the excel that the class will read. This method must return
|
33
|
+
# a Array of strings/symbols (representing columns names) or a Hash (which map column names to columns indexes).
|
34
|
+
#
|
35
|
+
# Array Example
|
36
|
+
# def self.columns
|
37
|
+
# [:username, :email, :gender]
|
38
|
+
# end
|
39
|
+
#
|
40
|
+
# Hash Example
|
41
|
+
# def self.columns
|
42
|
+
# { :username => 0, :email => 1, :gender => 2 }
|
43
|
+
# end
|
44
|
+
#
|
45
|
+
# == Returns:
|
46
|
+
# An Array or a Hash defining the columns of the excel.
|
47
|
+
#
|
48
|
+
def self.columns
|
49
|
+
fail(
|
50
|
+
MethodNotImplementedError,
|
51
|
+
'Please implement this method in your class.'
|
52
|
+
)
|
53
|
+
end
|
54
|
+
|
55
|
+
# Returns the Hash representation of a given array using self.format method
|
56
|
+
# (which internally uses self.columns). The keys of this Hash are defined
|
57
|
+
# in the self.columns method and the values of each key depends on the
|
58
|
+
# order of the columns.
|
59
|
+
#
|
60
|
+
# For example, given the following self.columns definition
|
61
|
+
# def self.columns
|
62
|
+
# [:username, :email, :gender]
|
63
|
+
# end
|
64
|
+
# Or
|
65
|
+
# def self.columns
|
66
|
+
# { :username => 0, :email => 1, :gender => 2 }
|
67
|
+
# end
|
68
|
+
# Row.formatted([username email@test.cl male]) will return
|
69
|
+
# { username: 'username', email: 'email@test.cl', gender: 'male' }
|
70
|
+
#
|
71
|
+
# == Parameters:
|
72
|
+
# array::
|
73
|
+
# Array of values which represents an excel column.
|
74
|
+
#
|
75
|
+
# == Returns:
|
76
|
+
# Hash representation of the given array which maps columns names to the array values.
|
77
|
+
|
78
|
+
def self.formatted_hash(array)
|
79
|
+
|
80
|
+
if format.keys.count > 0
|
81
|
+
parsed_row = {}
|
82
|
+
format.each_pair { |key, col| parsed_row[key] = array[col] }
|
83
|
+
return parsed_row
|
84
|
+
end
|
85
|
+
|
86
|
+
return nil
|
87
|
+
end
|
88
|
+
|
89
|
+
# Generalizes the constructor of ActiveModel::Model to make it work
|
90
|
+
# with an array argument.
|
91
|
+
# When an array argument is passed, it calls formatted_hash method
|
92
|
+
# to generate a Hash and then pass it to the ActiveModel::Model constructor
|
93
|
+
#
|
94
|
+
# == Parameters:
|
95
|
+
# arr_or_hash::
|
96
|
+
# Array or Hash of values which represents an excel column.
|
97
|
+
|
98
|
+
def initialize(arr_or_hash = {})
|
99
|
+
if arr_or_hash.is_a?(Array)
|
100
|
+
super(self.class.formatted_hash(arr_or_hash))
|
101
|
+
else
|
102
|
+
super(arr_or_hash)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
# This method is called when all rows of row_collection are valid. The main
|
107
|
+
# idea of this method is to run validations that have to do with
|
108
|
+
# the set of rows of the excel. For example, you can check here if a
|
109
|
+
# excel column is unique. It have to raise an exception if there was
|
110
|
+
# an error with a certain row.
|
111
|
+
#
|
112
|
+
# Example:
|
113
|
+
#
|
114
|
+
# def self.validate_multiple_rows(row_collection)
|
115
|
+
# username_list = []
|
116
|
+
# row_collection.rows.each do |row|
|
117
|
+
# if username_list.include?(row.username)
|
118
|
+
# row_collection.invalid_row = row
|
119
|
+
# row.errors[:username] = 'is unique'
|
120
|
+
# raise 'Validation Error'
|
121
|
+
# else
|
122
|
+
# username_list << row.username
|
123
|
+
# end
|
124
|
+
# end
|
125
|
+
# end
|
126
|
+
#
|
127
|
+
# == Parameters:
|
128
|
+
# row_collection::
|
129
|
+
# SpreadsheetReader::RowCollection instance
|
130
|
+
def self.validate_multiple_rows(row_collection)
|
131
|
+
|
132
|
+
end
|
133
|
+
|
134
|
+
# This method is called after the self.validate_multiple_rows method
|
135
|
+
# only if it have not raised an exception. The idea of this method
|
136
|
+
# is to persist the row's data. If there was an error with a certain
|
137
|
+
# row, you have to Rollback the valid transactions, set the
|
138
|
+
# invalid error to the row_collection and set the row errors to the
|
139
|
+
# row object.
|
140
|
+
#
|
141
|
+
# Example:
|
142
|
+
# def self.persist(row_collection)
|
143
|
+
# User.transaction do
|
144
|
+
# row_collection.rows.each do |row|
|
145
|
+
# user = User.new(attr1: row.attr1, attr2: row.attr2, ...)
|
146
|
+
# unless user.save
|
147
|
+
# row_collection.set_invalid_row(row, user) # pass the model with errors as second parameter
|
148
|
+
# rollback # use the rollback helper to rollback the transaction.
|
149
|
+
# end
|
150
|
+
# end
|
151
|
+
# end
|
152
|
+
# end
|
153
|
+
#
|
154
|
+
# == Parameters:
|
155
|
+
# row_collection::
|
156
|
+
# SpreadsheetReader::RowCollection instance
|
157
|
+
def self.persist(row_collection)
|
158
|
+
|
159
|
+
end
|
160
|
+
|
161
|
+
def self.open_spreadsheet(spreadsheet_file)
|
162
|
+
Roo::Spreadsheet.open(spreadsheet_file)
|
163
|
+
end
|
164
|
+
|
165
|
+
# Read and validates the given #spreadsheet_file. First calls
|
166
|
+
# #validate_rows method which run #ActiveModel::Model.valid? on each row.
|
167
|
+
# Then calls #validate_multiple_rows and finally ends with #persist
|
168
|
+
#
|
169
|
+
# == Parameters:
|
170
|
+
# spreadsheet_file::
|
171
|
+
# File instance
|
172
|
+
# == Returns:
|
173
|
+
# row_collection::
|
174
|
+
# SpreadsheetReader::RowCollection instance
|
175
|
+
def self.read_spreadsheet(spreadsheet_file)
|
176
|
+
|
177
|
+
if columns.empty?
|
178
|
+
raise NotImplementedError
|
179
|
+
end
|
180
|
+
|
181
|
+
spreadsheet = open_spreadsheet(spreadsheet_file)
|
182
|
+
row_collection = RailsSpreadsheetReader::RowCollection.new
|
183
|
+
|
184
|
+
begin
|
185
|
+
validate_rows(spreadsheet, row_collection)
|
186
|
+
validate_multiple_rows(row_collection)
|
187
|
+
persist(row_collection)
|
188
|
+
row_collection
|
189
|
+
rescue
|
190
|
+
row_collection
|
191
|
+
end
|
192
|
+
|
193
|
+
end
|
194
|
+
|
195
|
+
# Transform an array [a0, a1, a2, ...] to a Hash { a0 => 0, a1 => 1, etc }
|
196
|
+
# which is the format required by the #formatted_hash method.
|
197
|
+
def self.array_to_hash(arr)
|
198
|
+
Hash[[*arr.map { |e| e.to_sym }.map.with_index]]
|
199
|
+
end
|
200
|
+
|
201
|
+
private
|
202
|
+
|
203
|
+
# Calls row.valid? on every row of the spreadsheet. Raises an exception
|
204
|
+
# when row_collection.valid? returns false. It happens when
|
205
|
+
# the current row is an invalid ActiveModel::Model.
|
206
|
+
def self.validate_rows(spreadsheet, row_collection)
|
207
|
+
(starting_row..spreadsheet.last_row).each do |number|
|
208
|
+
hash = formatted_hash(spreadsheet.row(number))
|
209
|
+
hash[:row_number] = number
|
210
|
+
row_collection.push self.new(hash)
|
211
|
+
raise 'Invalid Row in Excel' unless row_collection.valid?
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
# Returns the Hash representation of the defined columns
|
216
|
+
def self.format
|
217
|
+
|
218
|
+
if columns.is_a?(Array) or columns.is_a?(Hash)
|
219
|
+
return columns.is_a?(Array) ? array_to_hash(columns) : columns
|
220
|
+
end
|
221
|
+
|
222
|
+
fail(
|
223
|
+
InvalidTypeError,
|
224
|
+
'Please check that the self.columns implementation returns a instance of Hash or Array.'
|
225
|
+
)
|
226
|
+
|
227
|
+
end
|
228
|
+
|
229
|
+
# Helper for rollbacks inside #after_multiple_row_validations
|
230
|
+
def self.rollback
|
231
|
+
raise ActiveRecord::Rollback
|
232
|
+
end
|
233
|
+
|
234
|
+
end
|
235
|
+
|
236
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module RailsSpreadsheetReader
|
2
|
+
|
3
|
+
class RowCollection
|
4
|
+
|
5
|
+
attr_accessor :invalid_row, :rows
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
self.rows = []
|
9
|
+
end
|
10
|
+
|
11
|
+
def push(row)
|
12
|
+
self.invalid_row = row unless row.valid?
|
13
|
+
self.rows << row
|
14
|
+
end
|
15
|
+
|
16
|
+
def valid?
|
17
|
+
self.invalid_row.nil?
|
18
|
+
end
|
19
|
+
|
20
|
+
def errors
|
21
|
+
self.invalid_row.errors
|
22
|
+
end
|
23
|
+
|
24
|
+
def set_invalid_row(row, model_with_errors)
|
25
|
+
row.copy_errors(model_with_errors)
|
26
|
+
self.invalid_row = row
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rails_spreadsheet_reader/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'rails_spreadsheet_reader'
|
8
|
+
spec.version = RailsSpreadsheetReader::VERSION
|
9
|
+
spec.authors = ['muzk']
|
10
|
+
spec.email = ['ngomez@hasu.cl']
|
11
|
+
spec.description = 'Provides an easy way to add model-based validations to excel files.'
|
12
|
+
spec.summary = 'Provides an easy way to add model-based validations to excel files.'
|
13
|
+
spec.homepage = 'https://github.com/HasuSoftware/rails_spreadsheet_reader'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
22
|
+
spec.add_development_dependency 'rake'
|
23
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
24
|
+
spec.add_development_dependency 'factory_girl', '~> 4.4'
|
25
|
+
|
26
|
+
spec.add_dependency 'roo', '~> 1.13'
|
27
|
+
spec.add_dependency 'rails', '~> 4.2.0'
|
28
|
+
end
|
data/spec/base_spec.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require_relative 'models/user_spreadsheet'
|
3
|
+
require_relative 'models/user_invalid_spreadsheet'
|
4
|
+
|
5
|
+
def test_file(filename, ext)
|
6
|
+
File.open(File.join TEST_DIR, "#{filename}.#{ext}")
|
7
|
+
end
|
8
|
+
|
9
|
+
describe RailsSpreadsheetReader::Base do
|
10
|
+
|
11
|
+
it 'Base#columns' do
|
12
|
+
expect { RailsSpreadsheetReader::Base.columns }.to raise_error
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'Base#format' do
|
16
|
+
expect { RailsSpreadsheetReader::Base.format }.to raise_error
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'Base#formatted_hash' do
|
20
|
+
expect { RailsSpreadsheetReader::Base.formatted_hash(%w(username email@test.com male)) }.to raise_error
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'Base#valid? should work as desired' do
|
24
|
+
expect(RailsSpreadsheetReader::Base.new.valid?).to eq(true)
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'Base#open_spreadsheet' do
|
28
|
+
file = test_file 'users', :csv
|
29
|
+
row = RailsSpreadsheetReader::Base.open_spreadsheet file
|
30
|
+
expect(row).not_to eq(false)
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'Base#read_spreadsheet' do
|
34
|
+
file = test_file 'users', :csv
|
35
|
+
expect { RailsSpreadsheetReader::Base.read_spreadsheet(file) }.to raise_error
|
36
|
+
end
|
37
|
+
|
38
|
+
it '#formatted_hash should work in a derived class which overrides "columns" method' do
|
39
|
+
formatted_hash = UserSpreadsheet.formatted_hash %w(username email@test.com male)
|
40
|
+
expect(formatted_hash).to eq({ username: 'username', email: 'email@test.com', gender: 'male' })
|
41
|
+
|
42
|
+
formatted_hash = UserSpreadsheet.formatted_hash %w(username male email@test.com)
|
43
|
+
expect(formatted_hash).not_to eq({ username: 'username', email: 'email@test.com', gender: 'male' })
|
44
|
+
end
|
45
|
+
|
46
|
+
it '#new from derived class' do
|
47
|
+
user_spreadsheet = UserSpreadsheet.new(%w(username email@test.com male))
|
48
|
+
expect(user_spreadsheet.username).to eq('username')
|
49
|
+
expect(user_spreadsheet.email).to eq('email@test.com')
|
50
|
+
expect(user_spreadsheet.gender).to eq('male')
|
51
|
+
|
52
|
+
user_spreadsheet = UserSpreadsheet.new({ email: 'email@test.com', gender: 'male', username: 'username' })
|
53
|
+
expect(user_spreadsheet.username).to eq('username')
|
54
|
+
expect(user_spreadsheet.email).to eq('email@test.com')
|
55
|
+
expect(user_spreadsheet.gender).to eq('male')
|
56
|
+
end
|
57
|
+
|
58
|
+
it '#valid? should work as desired (derived class)' do
|
59
|
+
valid_user_spreadsheet = UserSpreadsheet.new({ email: 'email@test.com', gender: 'male', username: 'username' })
|
60
|
+
expect(valid_user_spreadsheet.valid?).to eq(true)
|
61
|
+
|
62
|
+
invalid_user_spreadsheet = UserSpreadsheet.new({ gender: 'male', username: 'username' })
|
63
|
+
expect(invalid_user_spreadsheet.valid?).to eq(false)
|
64
|
+
end
|
65
|
+
|
66
|
+
it '#read_spreadsheet should be invalid because a row is not valid' do
|
67
|
+
file = test_file 'users_invalid', :csv
|
68
|
+
row_collection = UserSpreadsheet.read_spreadsheet(file)
|
69
|
+
expect(row_collection.valid?).to eq(false)
|
70
|
+
expect(row_collection.invalid_row.row_number).to eq(2)
|
71
|
+
expect(row_collection.errors.full_messages).to eq(["Email can't be blank"])
|
72
|
+
end
|
73
|
+
|
74
|
+
it '#read_spreadsheet should be invalid because #validate_multiple_rows raises an exception' do
|
75
|
+
file = test_file 'users_invalid', :csv
|
76
|
+
row_collection = UserInvalidSpreadsheet.read_spreadsheet(file)
|
77
|
+
expect(row_collection.valid?).to eq(false)
|
78
|
+
expect(row_collection.invalid_row.row_number).to eq(5)
|
79
|
+
expect(row_collection.errors.full_messages).to eq(['Username is unique'])
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
Username,Email,Gender,Birthday
|
2
|
+
username1,,male,31/12/1990
|
3
|
+
username2,email1@test.com,male,31/12/1991
|
4
|
+
username3,email1@test.com,male,31/12/1992
|
5
|
+
username3,email1@test.com,male,31/12/1993
|
6
|
+
username5,email1@test.com,male,31/12/1994
|
7
|
+
username6,email1@test.com,male,31/12/1995
|
8
|
+
username7,email1@test.com,male,31/12/1996
|
9
|
+
username8,email1@test.com,male,31/12/1997
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class UserInvalidSpreadsheet < RailsSpreadsheetReader::Base
|
2
|
+
|
3
|
+
attr_accessor :username, :email, :gender
|
4
|
+
|
5
|
+
validates_presence_of :username
|
6
|
+
|
7
|
+
def self.columns
|
8
|
+
{ :username => 0, :email => 1, :gender => 2 }
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.validate_multiple_rows(row_collection)
|
12
|
+
usernames = {}
|
13
|
+
row_collection.rows.each do |row|
|
14
|
+
if usernames.has_key?(row.username)
|
15
|
+
row_collection.invalid_row = row
|
16
|
+
row.errors[:username] = 'is unique'
|
17
|
+
raise 'Validation Error'
|
18
|
+
else
|
19
|
+
usernames[row.username] = true
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause this
|
4
|
+
# file to always be loaded, without a need to explicitly require it in any files.
|
5
|
+
#
|
6
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
7
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
8
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
9
|
+
# individual file that may not need all of that loaded. Instead, make a
|
10
|
+
# separate helper file that requires this one and then use it only in the specs
|
11
|
+
# that actually need it.
|
12
|
+
#
|
13
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
14
|
+
# users commonly want.
|
15
|
+
#
|
16
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
17
|
+
|
18
|
+
require File.expand_path('../../lib/rails_spreadsheet_reader', __FILE__)
|
19
|
+
TEST_DIR = File.join(File.dirname(__FILE__), 'files')
|
20
|
+
|
21
|
+
RSpec.configure do |config|
|
22
|
+
# The settings below are suggested to provide a good initial experience
|
23
|
+
# with RSpec, but feel free to customize to your heart's content.
|
24
|
+
=begin
|
25
|
+
# These two settings work together to allow you to limit a spec run
|
26
|
+
# to individual examples or groups you care about by tagging them with
|
27
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
28
|
+
# get run.
|
29
|
+
config.filter_run :focus
|
30
|
+
config.run_all_when_everything_filtered = true
|
31
|
+
|
32
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
33
|
+
# file, and it's useful to allow more verbose output when running an
|
34
|
+
# individual spec file.
|
35
|
+
if config.files_to_run.one?
|
36
|
+
# Use the documentation formatter for detailed output,
|
37
|
+
# unless a formatter has already been configured
|
38
|
+
# (e.g. via a command-line flag).
|
39
|
+
config.default_formatter = 'doc'
|
40
|
+
end
|
41
|
+
|
42
|
+
# Print the 10 slowest examples and example groups at the
|
43
|
+
# end of the spec run, to help surface which specs are running
|
44
|
+
# particularly slow.
|
45
|
+
config.profile_examples = 10
|
46
|
+
|
47
|
+
# Run specs in random order to surface order dependencies. If you find an
|
48
|
+
# order dependency and want to debug it, you can fix the order by providing
|
49
|
+
# the seed, which is printed after each run.
|
50
|
+
# --seed 1234
|
51
|
+
config.order = :random
|
52
|
+
|
53
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
54
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
55
|
+
# test failures related to randomization by passing the same `--seed` value
|
56
|
+
# as the one that triggered the failure.
|
57
|
+
Kernel.srand config.seed
|
58
|
+
|
59
|
+
# rspec-expectations config goes here. You can use an alternate
|
60
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
61
|
+
# assertions if you prefer.
|
62
|
+
config.expect_with :rspec do |expectations|
|
63
|
+
# Enable only the newer, non-monkey-patching expect syntax.
|
64
|
+
# For more details, see:
|
65
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
66
|
+
expectations.syntax = :expect
|
67
|
+
end
|
68
|
+
|
69
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
70
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
71
|
+
config.mock_with :rspec do |mocks|
|
72
|
+
# Enable only the newer, non-monkey-patching expect syntax.
|
73
|
+
# For more details, see:
|
74
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
75
|
+
mocks.syntax = :expect
|
76
|
+
|
77
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
78
|
+
# a real object. This is generally recommended.
|
79
|
+
mocks.verify_partial_doubles = true
|
80
|
+
end
|
81
|
+
=end
|
82
|
+
end
|
metadata
ADDED
@@ -0,0 +1,152 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_spreadsheet_reader
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- muzk
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: factory_girl
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '4.4'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '4.4'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: roo
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.13'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.13'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rails
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ~>
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 4.2.0
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ~>
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 4.2.0
|
97
|
+
description: Provides an easy way to add model-based validations to excel files.
|
98
|
+
email:
|
99
|
+
- ngomez@hasu.cl
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- .gitignore
|
105
|
+
- Gemfile
|
106
|
+
- LICENSE.txt
|
107
|
+
- README.md
|
108
|
+
- Rakefile
|
109
|
+
- lib/generators/rails/spreadsheet_generator.rb
|
110
|
+
- lib/generators/rails/templates/spreadsheet.rb
|
111
|
+
- lib/rails_spreadsheet_reader.rb
|
112
|
+
- lib/rails_spreadsheet_reader/base.rb
|
113
|
+
- lib/rails_spreadsheet_reader/row_collection.rb
|
114
|
+
- lib/rails_spreadsheet_reader/version.rb
|
115
|
+
- rails_spreadsheet_reader.gemspec
|
116
|
+
- spec/base_spec.rb
|
117
|
+
- spec/files/users.csv
|
118
|
+
- spec/files/users_invalid.csv
|
119
|
+
- spec/models/user_invalid_spreadsheet.rb
|
120
|
+
- spec/models/user_spreadsheet.rb
|
121
|
+
- spec/spec_helper.rb
|
122
|
+
homepage: https://github.com/HasuSoftware/rails_spreadsheet_reader
|
123
|
+
licenses:
|
124
|
+
- MIT
|
125
|
+
metadata: {}
|
126
|
+
post_install_message:
|
127
|
+
rdoc_options: []
|
128
|
+
require_paths:
|
129
|
+
- lib
|
130
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - '>='
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - '>='
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
requirements: []
|
141
|
+
rubyforge_project:
|
142
|
+
rubygems_version: 2.4.5
|
143
|
+
signing_key:
|
144
|
+
specification_version: 4
|
145
|
+
summary: Provides an easy way to add model-based validations to excel files.
|
146
|
+
test_files:
|
147
|
+
- spec/base_spec.rb
|
148
|
+
- spec/files/users.csv
|
149
|
+
- spec/files/users_invalid.csv
|
150
|
+
- spec/models/user_invalid_spreadsheet.rb
|
151
|
+
- spec/models/user_spreadsheet.rb
|
152
|
+
- spec/spec_helper.rb
|