-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 78cb7e5
Showing
26 changed files
with
800 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
*.gem | ||
*.rbc | ||
.bundle | ||
.config | ||
.yardoc | ||
Gemfile.lock | ||
InstalledFiles | ||
_yardoc | ||
coverage | ||
doc/ | ||
lib/bundler/man | ||
pkg | ||
rdoc | ||
spec/reports | ||
test/tmp | ||
test/version_tmp | ||
tmp | ||
lib/goodsheet/spreadsheet_v1.rb | ||
notex.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
source 'https://rubygems.org' | ||
|
||
# Specify your gem's dependencies in goodsheet.gemspec | ||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Copyright (c) 2013 Iwan Buetti | ||
|
||
MIT License | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Goodsheet | ||
|
||
Read and validate the content of a spreadsheet. | ||
The gem take advantage of wonderful validation methods available in Rails ActiveModel library and the methods of Roo gem to read and validate a spreadsheet. | ||
Refer to the [official guide](http://guides.rubyonrails.org/active_record_validations.html) for the validation rules. | ||
Thanks to [Roo gem](https://github.com/Empact/roo) Goodsheet can handle OpenOffice, LibreOffice, Excel (both '.xls' and '.xlsx') and Google spreadsheets. | ||
|
||
|
||
## Installation | ||
|
||
Add this line to your application's Gemfile: | ||
|
||
gem 'goodsheet' | ||
|
||
And then execute: | ||
|
||
$ bundle | ||
|
||
Or install it yourself as: | ||
|
||
$ gem install goodsheet | ||
|
||
## Usage | ||
|
||
### Getting started | ||
|
||
```ruby | ||
ss = Goodsheet::Spreadsheet.new("my_data.xlsx") | ||
res = ss.read do | ||
column_names :a => 0, :b => 1 | ||
validates :a, :presence => true, :numericality => { :greater_than_or_equal_to => 0.0, :less_than_or_equal_to => 10 } | ||
validates :b, :presence => true, :numericality => { :greater_than_or_equal_to => 0.0, :less_than_or_equal_to => 100 } | ||
end | ||
|
||
res.valid? # => true | ||
res.values # => {:a => [1.0, 1.0, 1.4], :b => []} | ||
``` | ||
|
||
By default: | ||
* the first sheet is selected | ||
* one line (the first) is skipped (i'm expeting that is the header line) | ||
|
||
Pass your validation rules into the block passed to the read method, together with the column_names method that define the position (or index) and the name of the columns you want to read. | ||
|
||
### Advanced usage | ||
|
||
to do | ||
|
||
|
||
|
||
Warning: | ||
* integer numbers are converted to float numbers. Also don't pretend to obtain an integer in validation. This undesired behaviour depend on Roo gem | ||
* if you import data from a CSV spreadsheet keep in mind that numbers are readed as strings | ||
|
||
|
||
|
||
|
||
## Contributing | ||
|
||
1. Fork it | ||
2. Create your feature branch (`git checkout -b my-new-feature`) | ||
3. Commit your changes (`git commit -am 'Add some feature'`) | ||
4. Push to the branch (`git push origin my-new-feature`) | ||
5. Create new Pull Request |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
require "bundler/gem_tasks" | ||
require 'rake/testtask' | ||
|
||
Rake::TestTask.new do |t| | ||
t.libs << 'test' | ||
end | ||
|
||
desc "Run tests" | ||
task :default => :test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# coding: utf-8 | ||
lib = File.expand_path('../lib', __FILE__) | ||
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) | ||
require 'goodsheet/version' | ||
|
||
Gem::Specification.new do |spec| | ||
spec.name = "goodsheet" | ||
spec.version = Goodsheet::VERSION | ||
spec.authors = ["Iwan Buetti"] | ||
spec.email = ["iwan.buetti@gmail.com"] | ||
spec.description = "Little gem that take advantage of Roo gem and Rails ActiveModel validation methods to read and validate the content of a spreadsheet" | ||
spec.summary = "Extract and validate data from a spreadsheet" | ||
spec.homepage = "https://github.com/iwan/goodsheet" | ||
spec.license = "MIT" | ||
spec.date = '2013-07-19' | ||
spec.files = `git ls-files`.split($/) | ||
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } | ||
spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) | ||
spec.require_paths = ["lib"] | ||
|
||
spec.add_development_dependency "bundler", "~> 1.4" | ||
spec.add_development_dependency "rake" | ||
|
||
spec.add_dependency('roo', '>= 1.12.1') # https://github.com/Empact/roo | ||
spec.add_dependency('activemodel', '>= 3.2.14') | ||
spec.add_dependency('google-drive-ruby') | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
require "goodsheet/version" | ||
|
||
module Goodsheet | ||
autoload :Row, 'goodsheet/row' | ||
autoload :SheetNotFoundError, 'goodsheet/exceptions' | ||
autoload :ReadResult, 'goodsheet/read_result' | ||
autoload :Row, 'goodsheet/row' | ||
autoload :Spreadsheet, 'goodsheet/spreadsheet' | ||
autoload :ValidationError, 'goodsheet/validation_error' | ||
autoload :ValidationErrors, 'goodsheet/validation_errors' | ||
autoload :Version, 'goodsheet/version' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module Goodsheet | ||
|
||
class SheetNotFoundError < StandardError; end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module Goodsheet | ||
|
||
class ReadResult | ||
attr_reader :values | ||
|
||
def initialize(errors=ValidationErrors.new) | ||
@errors = errors | ||
@values = {} | ||
end | ||
|
||
def valid? | ||
@errors.empty? | ||
end | ||
|
||
def invalid? | ||
!valid? | ||
end | ||
|
||
def add(attribute, row, force_nil=nil) | ||
attribute = attribute.to_sym | ||
(@values[attribute] ||= []) << (row.send(attribute) || force_nil) | ||
end | ||
|
||
def errors | ||
@errors.array | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
require 'active_model' | ||
|
||
module Goodsheet | ||
|
||
class Row | ||
include ActiveModel::Validations | ||
include ActiveModel::Conversion | ||
extend ActiveModel::Naming | ||
|
||
class << self | ||
attr_accessor :keys | ||
end | ||
@keys = {} # idx => key | ||
|
||
def initialize(arr) | ||
arr.each_with_index do |v, idx| | ||
if k = self.class.keys[idx] | ||
send("#{k}=", v) | ||
end | ||
end | ||
super() | ||
end | ||
|
||
def self.inherit(block) | ||
c = Class.new(self) do | ||
@keys = {} # idx => key | ||
end | ||
c.class_eval(&block) | ||
c | ||
end | ||
|
||
# Define the position (or index) and the name of columns. | ||
# There are available three mode to define them: | ||
# using an hash index to name (like { 0 => :year, 2 => :day }) | ||
# or name to index (like { :year => 0, :day => 2 }) or using an array | ||
# with the names at desired positions (like [:year, nil, :day]), put a nil | ||
# at the position | ||
# The positions are 0-based. | ||
def self.column_names(param) | ||
@keys = {} | ||
if param.is_a? Hash | ||
if param.first[0].is_a? Integer | ||
param.each do |idx, name| | ||
self.keys[idx] = name | ||
attr_accessor name | ||
end | ||
else | ||
param.each do |name, idx| | ||
self.keys[idx] = name | ||
attr_accessor name | ||
end | ||
end | ||
elsif param.is_a? Array | ||
param.each_with_index do |name, idx| | ||
if name | ||
self.keys[idx] = name | ||
attr_accessor name | ||
end | ||
end | ||
|
||
else | ||
raise "parameter non valid" | ||
end | ||
end | ||
|
||
|
||
def persisted? | ||
false | ||
end | ||
|
||
# Get the list of attributes (the columns to import) | ||
def self.row_attributes | ||
@keys.values | ||
end | ||
end | ||
end |
Oops, something went wrong.