rseed-roo 1.0.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 ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YTkyYjNjYTUzNzU4NjcwMjZjNTc1MzYyMzI3YzlkNjhjZDAyZGUyOA==
5
+ data.tar.gz: !binary |-
6
+ OTFjYTI3ZDI1MTNmYmQ3MjdkMjJkOWNmNmZlMjI2NDEwM2VkZTMwMw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MzlhMTVmMTBhZDJjOGRiNjJlYjAwOTM2OWZiYmJhZmE4YTlkODc2MDFmMDNk
10
+ MmMyZDZjMWUwZmJmMmU3ZmM4Zjk0OGNjOWNlOTkwNjQ4MWM1MzI0ODk2YTUx
11
+ NGNkNzdhOTk3MjE3YTcwMjkwZjMyOGY0MDk4MzU5NTg3N2I4MTQ=
12
+ data.tar.gz: !binary |-
13
+ MjIyNzQ2YmU4OGZiMWM0NjQxMWE2NWE4OTJiZGViNDUyNzlmNGQzNGNiMDE0
14
+ ZDMyMDg5M2M5YmUyNjRjZjcwNTFhNDI4NDMxZGUxMmI3YWRiZGNmZTFkOTg0
15
+ Y2FkOWVmZTYxMDQxMzFlZjYyYzYxOTRlM2M5NTI2Mjc3ODllNzM=
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rseed-roo.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 David Monagle
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.rdoc ADDED
@@ -0,0 +1,24 @@
1
+ # Rseed::Roo
2
+
3
+ This is an adapter for the Rseed gem http://github.com/intrica/rseed
4
+
5
+ The RooAdapter allows you to import excel files using rseed.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'rseed-roo'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rseed-roo
20
+
21
+ ## Usage
22
+
23
+ The Roo adapter takes the same parameters as the CSV adapter that is built into Rseed. It also provides a *from_exel*
24
+ helper that works just like the from_csv helper in Rseed.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/rseed/roo.rb ADDED
@@ -0,0 +1,15 @@
1
+ require "rseed/roo/version"
2
+ require "rseed/roo/roo_adapter"
3
+ require "rseed/roo/helpers"
4
+
5
+ module Rseed
6
+ module Roo
7
+ class Railtie < ::Rails::Railtie
8
+ railtie_name :rseed_roo
9
+
10
+ rake_tasks do
11
+ load "tasks/rseed-roo.rake"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,10 @@
1
+ require 'colorize'
2
+
3
+ module Rseed
4
+ def from_excel(file, options = {})
5
+ options[:adapter] = :roo
6
+ from_file(file, options)
7
+ end
8
+
9
+ module_function :from_excel
10
+ end
@@ -0,0 +1,107 @@
1
+ require "roo"
2
+
3
+ module Rseed
4
+ class RooAdapter < Rseed::Adapter
5
+ attr_accessor :file
6
+
7
+ def preprocess
8
+ return false unless file
9
+ logger.info "Preprocessing Excel file: #{file.to_s.yellow}"
10
+
11
+ @excelx = false
12
+ case File.extname(file).downcase
13
+ when ".xls"
14
+ @roo = ::Roo::Excel.new(file)
15
+ when ".xlsx"
16
+ @excelx = true
17
+ @roo = ::Roo::Excelx.new(file)
18
+ end
19
+
20
+ result = find_excel_header_row
21
+
22
+ if result.nil?
23
+ logger.error "Could not find header row.".red
24
+ return false
25
+ end
26
+
27
+ @roo.default_sheet = result[:sheet]
28
+ @header_row = result[:row]
29
+ @headers = result[:headers]
30
+
31
+ true
32
+ end
33
+
34
+ def process &block
35
+ # Loop through the data
36
+ logger.info "Reading data from row #{@header_row + 1} to #{@roo.last_row}"
37
+ @estimated_rows =@roo.last_row - @header_row;
38
+ row_number = 0
39
+ (@header_row + 1).upto(@roo.last_row) do |row|
40
+ row_number += 1
41
+ import_row = {}
42
+ @headers.each_pair do |name, column|
43
+ if @excelx
44
+ value = @roo.cell(row, column).to_s
45
+ else
46
+ value = @roo.cell(row, column).to_s
47
+ end
48
+ import_row[name] = value
49
+ end
50
+ yield import_row, record_count: row_number, total_records: @estimated_rows
51
+ end
52
+ end
53
+
54
+ def all_headers_found(headers)
55
+ @missing_headers_mandatory = []
56
+ @missing_headers_optional = []
57
+ found_at_least_one = false
58
+
59
+ converter_attributes.each do |attribute|
60
+ if headers[attribute.name].nil?
61
+ unless mapping[:optional]
62
+ @missing_headers_mandatory << attribute.name
63
+ else
64
+ @missing_headers_optional << attribute.name
65
+ end
66
+ else
67
+ found_at_least_one = true
68
+ end
69
+ end
70
+ if found_at_least_one
71
+ logger.warning "Missing optional headers: #{@missing_headers_optional.join(',')}".yellow unless @missing_headers_optional.empty?
72
+ logger.warning "Missing mandatory headers: #{@missing_headers_mandatory.join(',')}".red unless @missing_headers_mandatory.empty?
73
+ end
74
+ return false unless @missing_headers_mandatory.empty?
75
+ true
76
+ end
77
+
78
+ def find_excel_header_row
79
+ @roo.sheets.each do |sheet|
80
+ logger.info "Looking for the header row in sheet #{sheet}".cyan
81
+ @roo.default_sheet = sheet
82
+ @roo.first_row.upto(@roo.last_row) do |row|
83
+ headers = {}
84
+ (@roo.first_column..@roo.last_column).each do |column|
85
+ converter_attributes.each do |attribute|
86
+
87
+ if attribute.matches? @roo.cell(row, column).to_s
88
+ logger.info "Found header for #{attribute.name.to_s.green} at column #{column} at row #{row}"
89
+ if (headers[attribute.name].nil?)
90
+ headers[attribute.name] = column
91
+ else
92
+ logger.warn "Found duplicate header '#{attribute.name.to_s.red}' on columns #{column} and #{headers[attribute.name]}."
93
+ end
94
+ end
95
+ end
96
+ end
97
+
98
+ if all_headers_found(headers)
99
+ logger.info "All headers found on row #{row}".green
100
+ return {:row => row, :sheet => sheet, :headers => headers}
101
+ end
102
+ end
103
+ end
104
+ return nil
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,5 @@
1
+ module Rseed
2
+ module Roo
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,18 @@
1
+ require "rseed"
2
+ require "colorize"
3
+
4
+ namespace :rseed do
5
+ desc "Seed an Excel file using the RooAdapter and a converter"
6
+ task :excel => :environment do
7
+ converter = ENV["converter"] || ENV["CONVERTER"]
8
+ converter_options = ENV["converter_options"] || ENV["CONVERTER_OPTIONS"]
9
+ file = ENV["file"] || ENV["FILE"]
10
+ if file && converter
11
+ options = {converter: converter}
12
+ options[:converter_options] = converter_options if converter_options
13
+ Rseed::from_excel file, options
14
+ else
15
+ puts "You must specify file=<file> and converter=<converter>".red
16
+ end
17
+ end
18
+ end
data/rseed-roo.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rseed/roo/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rseed-roo"
8
+ spec.version = Rseed::Roo::VERSION
9
+ spec.authors = ["David Monagle"]
10
+ spec.email = ["david.monagle@intrica.com.au"]
11
+ spec.description = %q{Provides a Roo based adapter for Rseed that allows the import of excel files.}
12
+ spec.summary = %q{Roo adapter for Rseed}
13
+ spec.homepage = ""
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_dependency "rseed"
22
+ spec.add_dependency "roo"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rseed-roo
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - David Monagle
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rseed
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: roo
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Provides a Roo based adapter for Rseed that allows the import of excel
70
+ files.
71
+ email:
72
+ - david.monagle@intrica.com.au
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.rdoc
81
+ - Rakefile
82
+ - lib/rseed/roo.rb
83
+ - lib/rseed/roo/helpers.rb
84
+ - lib/rseed/roo/roo_adapter.rb
85
+ - lib/rseed/roo/version.rb
86
+ - lib/tasks/rseed-roo.rake
87
+ - rseed-roo.gemspec
88
+ homepage: ''
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.1.9
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Roo adapter for Rseed
112
+ test_files: []