coercell 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.md +35 -0
- data/Rakefile +32 -0
- data/lib/coercell.rb +90 -0
- data/lib/coercell/value.rb +16 -0
- data/lib/coercell/version.rb +3 -0
- metadata +118 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2013 JZ/UP
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Coercell [![Code Climate](https://codeclimate.com/github/jzup/coercell.png)](https://codeclimate.com/github/jzup/coercell) [![Build Status](https://travis-ci.org/jzup/coercell.png)](https://travis-ci.org/jzup/coercell])
|
2
|
+
|
3
|
+
A spreadsheet parser and importer to ActiveRecord models
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'coercell'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install coercell
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
parser = Coercell::Parser.new(YourModel)
|
22
|
+
parser.spreadsheet = "path/to/spreadsheet.xls"
|
23
|
+
|
24
|
+
parser.parse!
|
25
|
+
|
26
|
+
errors = parser.errors
|
27
|
+
valid = parser.valid
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
1. Fork it
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
|
3
|
+
require 'rspec/core/rake_task'
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'bundler/setup'
|
7
|
+
rescue LoadError
|
8
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
9
|
+
end
|
10
|
+
|
11
|
+
begin
|
12
|
+
require 'rdoc/task'
|
13
|
+
rescue LoadError
|
14
|
+
require 'rdoc/rdoc'
|
15
|
+
require 'rake/rdoctask'
|
16
|
+
RDoc::Task = Rake::RDocTask
|
17
|
+
end
|
18
|
+
|
19
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
20
|
+
rdoc.rdoc_dir = 'rdoc'
|
21
|
+
rdoc.title = 'Coercell'
|
22
|
+
rdoc.options << '--line-numbers'
|
23
|
+
rdoc.rdoc_files.include('README.rdoc')
|
24
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
Bundler::GemHelper.install_tasks
|
29
|
+
|
30
|
+
RSpec::Core::RakeTask.new
|
31
|
+
|
32
|
+
task :default => :spec
|
data/lib/coercell.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'roo'
|
2
|
+
require 'coercell/value'
|
3
|
+
|
4
|
+
module Coercell
|
5
|
+
class Parser
|
6
|
+
|
7
|
+
def initialize(model)
|
8
|
+
@model = model
|
9
|
+
end
|
10
|
+
|
11
|
+
def prepare_content(start=2)
|
12
|
+
@content ||= (start..spreadsheet.last_row).collect do |line|
|
13
|
+
content_line = {}
|
14
|
+
titles.each do |title|
|
15
|
+
content_line[title[:value]] = get_cell_value(title,line)
|
16
|
+
end
|
17
|
+
content_line
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def parse!
|
22
|
+
data = []
|
23
|
+
errors = []
|
24
|
+
|
25
|
+
prepare_content
|
26
|
+
|
27
|
+
@content.each_with_index do |line_data, i|
|
28
|
+
instance = @model.new(line_data)
|
29
|
+
if instance.valid?
|
30
|
+
data << instance
|
31
|
+
else
|
32
|
+
error = {}
|
33
|
+
error[:line] = i+2
|
34
|
+
error[:messages] = instance.errors.full_messages
|
35
|
+
errors << error
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
@valid_objects = data
|
40
|
+
@errors = errors
|
41
|
+
end
|
42
|
+
|
43
|
+
def errors
|
44
|
+
@errors
|
45
|
+
end
|
46
|
+
|
47
|
+
def valid_objects
|
48
|
+
@valid_objects
|
49
|
+
end
|
50
|
+
|
51
|
+
def spreadsheet
|
52
|
+
@spreadsheet
|
53
|
+
end
|
54
|
+
|
55
|
+
def spreadsheet=(file)
|
56
|
+
extension = File.extname(file)[1..-1].downcase
|
57
|
+
|
58
|
+
@spreadsheet = case extension
|
59
|
+
when "ods"
|
60
|
+
Roo::Openoffice.new(file)
|
61
|
+
when "xls"
|
62
|
+
Roo::Excel.new(file)
|
63
|
+
when "xlsx"
|
64
|
+
Roo::Excelx.new(file)
|
65
|
+
else
|
66
|
+
raise ArgumentError, "Provided file must be Openffice(.ods) or Excel(.xls or .xlsx). .#{extension} is not valid"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def titles
|
73
|
+
attributes = @model.attribute_names
|
74
|
+
|
75
|
+
@titles ||= ((1..spreadsheet.last_column).collect do |column|
|
76
|
+
value = spreadsheet.cell(1,column).strip #getting content from first line
|
77
|
+
{ :column => column, :value => value } if attributes.include? value
|
78
|
+
end).compact
|
79
|
+
|
80
|
+
@titles
|
81
|
+
end
|
82
|
+
|
83
|
+
|
84
|
+
def get_cell_value(title,line)
|
85
|
+
Coercell::Value.coerce( @model,
|
86
|
+
title[:value],
|
87
|
+
@spreadsheet.cell(line, title[:column]) )
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Coercell
|
2
|
+
module Value
|
3
|
+
def self.coerce(model,attribute_name,value)
|
4
|
+
attribute_type = (model.columns.select { |a| a.name == attribute_name }).first.type
|
5
|
+
|
6
|
+
value = case attribute_type
|
7
|
+
when :integer
|
8
|
+
value.to_i if value.is_a? Numeric
|
9
|
+
else
|
10
|
+
value
|
11
|
+
end
|
12
|
+
|
13
|
+
value
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: coercell
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Rafael Barros
|
9
|
+
- Gustavo Sales
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2013-05-08 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rails
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ~>
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 3.2.13
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ~>
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: 3.2.13
|
31
|
+
- !ruby/object:Gem::Dependency
|
32
|
+
name: roo
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
type: :runtime
|
40
|
+
prerelease: false
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: sqlite3
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
name: rspec-rails
|
65
|
+
requirement: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
type: :development
|
72
|
+
prerelease: false
|
73
|
+
version_requirements: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
description: ! " Parses spreadsheets, validates its data against
|
80
|
+
ActiveRecord model \n and instanciates valid data\n"
|
81
|
+
email:
|
82
|
+
- rafael.barros@jazz.etc.br
|
83
|
+
- vatsu21@gmail.com
|
84
|
+
executables: []
|
85
|
+
extensions: []
|
86
|
+
extra_rdoc_files: []
|
87
|
+
files:
|
88
|
+
- lib/coercell/value.rb
|
89
|
+
- lib/coercell/version.rb
|
90
|
+
- lib/coercell.rb
|
91
|
+
- MIT-LICENSE
|
92
|
+
- Rakefile
|
93
|
+
- README.md
|
94
|
+
homepage: https://github.com/jzup/coercell
|
95
|
+
licenses: []
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
none: false
|
102
|
+
requirements:
|
103
|
+
- - ! '>='
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ! '>='
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
requirements: []
|
113
|
+
rubyforge_project:
|
114
|
+
rubygems_version: 1.8.25
|
115
|
+
signing_key:
|
116
|
+
specification_version: 3
|
117
|
+
summary: A spreadsheet parser and importer to ActiveRecord models
|
118
|
+
test_files: []
|