to_xls 0.1.2 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +16 -0
- data/{MIT-LICENSE → LICENSE.txt} +1 -1
- data/README.rdoc +55 -26
- data/Rakefile +50 -0
- data/VERSION +1 -0
- data/init.rb +0 -0
- data/lib/to_xls.rb +1 -71
- data/lib/to_xls/array_patch.rb +9 -0
- data/lib/to_xls/array_writer.rb +95 -0
- data/spec/array_spec.rb +19 -0
- data/spec/array_writer_spec.rb +164 -0
- data/spec/spec_helper.rb +50 -0
- metadata +104 -30
- data/to_xls.gemspec +0 -29
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
gem "spreadsheet", ">= 0"
|
7
|
+
|
8
|
+
# Add dependencies to develop your gem here.
|
9
|
+
# Include everything needed to run rake, tests, features, etc.
|
10
|
+
group :development do
|
11
|
+
gem "rspec", "~> 2.3.0"
|
12
|
+
gem "bundler", "~> 1.0.0"
|
13
|
+
gem "jeweler", "~> 1.5.2"
|
14
|
+
gem "rcov", ">= 0"
|
15
|
+
gem "factory_girl", ">= 0"
|
16
|
+
end
|
data/{MIT-LICENSE → LICENSE.txt}
RENAMED
data/README.rdoc
CHANGED
@@ -1,35 +1,20 @@
|
|
1
1
|
= to_xls gem
|
2
2
|
|
3
|
-
This gem transform an Array into a excel file using the spreadsheet gem.
|
3
|
+
This gem transform an Array or Hash into a excel file using the spreadsheet gem.
|
4
4
|
|
5
5
|
== Usage
|
6
6
|
|
7
7
|
@users = User.all
|
8
8
|
|
9
9
|
@users.to_xls
|
10
|
-
@users.to_xls(:
|
11
|
-
@users.to_xls(:
|
12
|
-
@users.to_xls(:columns => [:name,
|
13
|
-
@users.to_xls(:columns => [:name, {:company => [:name, :address]}]
|
10
|
+
@users.to_xls(:name => "Users") # specifies the Sheet name (by default Sheet1)
|
11
|
+
@users.to_xls(:headers => false) # don't include headers
|
12
|
+
@users.to_xls(:columns => [:name, :role]) # include only these columns, on this order
|
13
|
+
@users.to_xls(:columns => [:name, {:company => [:name, :address]}]) # able to pick associations/called methods
|
14
|
+
@users.to_xls(:columns => [:name, {:company => [:name, :address]}], :headers => [:name, :company, :address]) # provide better names for the associated columns
|
14
15
|
|
15
|
-
In order to send a file from the controller, you can save it on your server first:
|
16
16
|
|
17
|
-
|
18
|
-
send_file '/path/to/file/users.xls'
|
19
|
-
|
20
|
-
Alternatively you can use the method to_xls_data and send_data
|
21
|
-
|
22
|
-
send_data @users.to_xls_data, :filename => 'users.xls'
|
23
|
-
|
24
|
-
The method to_xls_data accepts the same parameters as to_xls.
|
25
|
-
|
26
|
-
== Requirements
|
27
|
-
|
28
|
-
In config/initializers/mime_types.rb register the custom mime type.
|
29
|
-
|
30
|
-
Mime::Type.register "application/vnd.ms-excel", :xls
|
31
|
-
|
32
|
-
== How to use
|
17
|
+
== Example of use in Rails
|
33
18
|
|
34
19
|
In the controller where you want to export to excel, add the format.xls line.
|
35
20
|
|
@@ -41,19 +26,63 @@ In the controller where you want to export to excel, add the format.xls line.
|
|
41
26
|
respond_to do |format|
|
42
27
|
format.html
|
43
28
|
format.xml { render :xml => @users }
|
44
|
-
format.xls { send_data @users.
|
29
|
+
format.xls { send_data @users.to_xls, :filename => 'users.xls' }
|
45
30
|
end
|
46
31
|
end
|
47
32
|
end
|
48
33
|
|
34
|
+
== Requirements
|
35
|
+
|
36
|
+
On rails, you might want to modify config/initializers/mime_types.rb and register a custom mime type there:
|
37
|
+
|
38
|
+
Mime::Type.register "application/vnd.ms-excel", :xls
|
49
39
|
|
50
40
|
== Dependencies
|
51
41
|
|
52
|
-
|
42
|
+
* spreadsheet gem (automatically included if you require to_xls)
|
53
43
|
|
54
|
-
==
|
44
|
+
== Installing on rails 2.x
|
55
45
|
|
56
|
-
|
46
|
+
Include next gems in your environment.rb config file:
|
57
47
|
|
58
48
|
config.gem 'to_xls'
|
59
49
|
|
50
|
+
Then execute
|
51
|
+
|
52
|
+
rake gems:install
|
53
|
+
|
54
|
+
|
55
|
+
== Installing on rails 3.x
|
56
|
+
|
57
|
+
In your Gemfile
|
58
|
+
|
59
|
+
gem 'to_xls'
|
60
|
+
|
61
|
+
Then execute
|
62
|
+
|
63
|
+
bundle install
|
64
|
+
|
65
|
+
== ToXml::ArrayWriter
|
66
|
+
|
67
|
+
You can export to a file or spreadsheet book using the internal ToXml::ArrayWriter class:
|
68
|
+
|
69
|
+
ToXls::ArrayWriter.new(users, :name => 'Users').write_io(file) # writes to a given file
|
70
|
+
ToXls::ArrayWriter.new(users, :name => 'Users').write_book(book) # writes to a spreadsheet book (adds a new sheet)
|
71
|
+
|
72
|
+
The options of ArrayWriter.new(array, options) are the same as in to_xls.
|
73
|
+
|
74
|
+
== Contributing to to_xls
|
75
|
+
|
76
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
77
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
78
|
+
* Fork the project
|
79
|
+
* Start a feature/bugfix branch
|
80
|
+
* Commit and push until you are happy with your contribution
|
81
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
82
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
83
|
+
|
84
|
+
== Copyright
|
85
|
+
|
86
|
+
Copyright (c) 2011 Enrique García Cota. See LICENSE.txt for
|
87
|
+
further details.
|
88
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "to_xls"
|
16
|
+
gem.homepage = "http://github.com/splendeo/to_xls"
|
17
|
+
gem.license = "MIT"
|
18
|
+
gem.summary = %Q{to_xls for Arrays and Hashes}
|
19
|
+
gem.description = %Q{Adds a to_xls method to arrays, which can be used to generate excel files conveniently. Can rely on ActiveRecord sugar for obtaining attribute names.}
|
20
|
+
gem.email = "egarcia@splendeo.es"
|
21
|
+
gem.authors = ["Enrique Garcia Cota", "Francisco de Juan"]
|
22
|
+
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
23
|
+
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
|
24
|
+
gem.add_runtime_dependency 'spreadsheet', '> 0'
|
25
|
+
gem.add_development_dependency 'rspec', '> 1.2.3'
|
26
|
+
end
|
27
|
+
Jeweler::RubygemsDotOrgTasks.new
|
28
|
+
|
29
|
+
require 'rspec/core'
|
30
|
+
require 'rspec/core/rake_task'
|
31
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
32
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
33
|
+
end
|
34
|
+
|
35
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
36
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
37
|
+
spec.rcov = true
|
38
|
+
end
|
39
|
+
|
40
|
+
task :default => :spec
|
41
|
+
|
42
|
+
require 'rake/rdoctask'
|
43
|
+
Rake::RDocTask.new do |rdoc|
|
44
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
45
|
+
|
46
|
+
rdoc.rdoc_dir = 'rdoc'
|
47
|
+
rdoc.title = "to_xls #{version}"
|
48
|
+
rdoc.rdoc_files.include('README*')
|
49
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
50
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
data/init.rb
CHANGED
File without changes
|
data/lib/to_xls.rb
CHANGED
@@ -1,71 +1 @@
|
|
1
|
-
require '
|
2
|
-
require 'spreadsheet'
|
3
|
-
require 'stringio'
|
4
|
-
|
5
|
-
class Array
|
6
|
-
# Options for to_xls: columns, name, header
|
7
|
-
def to_xls(options = {})
|
8
|
-
|
9
|
-
book = Spreadsheet::Workbook.new
|
10
|
-
sheet = book.create_worksheet
|
11
|
-
|
12
|
-
sheet.name = options[:name] || 'Sheet 1'
|
13
|
-
|
14
|
-
if self.any?
|
15
|
-
columns = options[:columns] || self.first.attributes.keys.sort
|
16
|
-
|
17
|
-
if columns.any?
|
18
|
-
line = 0
|
19
|
-
|
20
|
-
unless options[:headers] == false
|
21
|
-
if options[:headers].is_a?(Array)
|
22
|
-
sheet.row(0).concat options[:headers].collect(&:to_s)
|
23
|
-
else
|
24
|
-
aux_headers_to_xls(self.first, columns, sheet.row(0))
|
25
|
-
end
|
26
|
-
line = 1
|
27
|
-
end
|
28
|
-
|
29
|
-
self.each do |item|
|
30
|
-
row = sheet.row(line)
|
31
|
-
columns.each {|column| aux_to_xls(item, column, row)}
|
32
|
-
line += 1
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
return book
|
38
|
-
end
|
39
|
-
|
40
|
-
def to_xls_data(options = {})
|
41
|
-
data = StringIO.new('')
|
42
|
-
self.to_xls(options).write(data)
|
43
|
-
return data.string
|
44
|
-
end
|
45
|
-
|
46
|
-
private
|
47
|
-
def aux_to_xls(item, column, row)
|
48
|
-
if item.nil?
|
49
|
-
row.push(nil)
|
50
|
-
elsif column.is_a?(String) or column.is_a?(Symbol)
|
51
|
-
row.push(item.send(column))
|
52
|
-
elsif column.is_a?(Hash)
|
53
|
-
column.each{|key, values| aux_to_xls(item.send(key), values, row)}
|
54
|
-
elsif column.is_a?(Array)
|
55
|
-
column.each{|value| aux_to_xls(item, value, row)}
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
def aux_headers_to_xls(item, column, row)
|
60
|
-
if item.nil?
|
61
|
-
row.push(nil)
|
62
|
-
elsif column.is_a?(String) or column.is_a?(Symbol)
|
63
|
-
row.push("#{item.class.name.underscore}_#{column}")
|
64
|
-
elsif column.is_a?(Hash)
|
65
|
-
column.each{|key, values| aux_headers_to_xls(item.send(key), values, row)}
|
66
|
-
elsif column.is_a?(Array)
|
67
|
-
column.each{|value| aux_headers_to_xls(item, value, row)}
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
end
|
1
|
+
require 'to_xls/array_patch.rb'
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'stringio'
|
3
|
+
require 'spreadsheet'
|
4
|
+
|
5
|
+
module ToXls
|
6
|
+
|
7
|
+
class ArrayWriter
|
8
|
+
def initialize(array, options = {})
|
9
|
+
@array = array
|
10
|
+
@options = options
|
11
|
+
end
|
12
|
+
|
13
|
+
def write_string(string = '')
|
14
|
+
io = StringIO.new(string)
|
15
|
+
write_io(io)
|
16
|
+
io.string
|
17
|
+
end
|
18
|
+
|
19
|
+
def write_io(io)
|
20
|
+
book = Spreadsheet::Workbook.new
|
21
|
+
write_book(book)
|
22
|
+
book.write(io)
|
23
|
+
end
|
24
|
+
|
25
|
+
def write_book(book)
|
26
|
+
sheet = book.create_worksheet
|
27
|
+
sheet.name = @options[:name] || 'Sheet 1'
|
28
|
+
write_sheet(sheet)
|
29
|
+
return book
|
30
|
+
end
|
31
|
+
|
32
|
+
def write_sheet(sheet)
|
33
|
+
if columns.any?
|
34
|
+
row_index = 0
|
35
|
+
|
36
|
+
if headers_should_be_included?
|
37
|
+
fill_row(sheet.row(0), headers)
|
38
|
+
row_index = 1
|
39
|
+
end
|
40
|
+
|
41
|
+
@array.each do |model|
|
42
|
+
row = sheet.row(row_index)
|
43
|
+
fill_row(row, columns, model)
|
44
|
+
row_index += 1
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def columns
|
50
|
+
return @columns if @columns
|
51
|
+
@columns = @options[:columns]
|
52
|
+
raise ArgumentError.new(":columns (#{columns}) must be an array or nil") unless (@columns.nil? || @columns.is_a?(Array))
|
53
|
+
@columns ||= can_get_columns_from_first_element? ? get_columns_from_first_element : []
|
54
|
+
end
|
55
|
+
|
56
|
+
def can_get_columns_from_first_element?
|
57
|
+
@array.first &&
|
58
|
+
@array.first.respond_to?(:attributes) &&
|
59
|
+
@array.first.attributes.respond_to?(:keys) &&
|
60
|
+
@array.first.attributes.keys.is_a?(Array)
|
61
|
+
end
|
62
|
+
|
63
|
+
def get_columns_from_first_element
|
64
|
+
@array.first.attributes.keys.sort_by {|sym| sym.to_s}.collect.to_a
|
65
|
+
end
|
66
|
+
|
67
|
+
def headers
|
68
|
+
return @headers if @headers
|
69
|
+
@headers = @options[:headers] || columns
|
70
|
+
raise ArgumentError, ":headers (#{@headers.inspect}) must be an array" unless @headers.is_a? Array
|
71
|
+
@headers
|
72
|
+
end
|
73
|
+
|
74
|
+
def headers_should_be_included?
|
75
|
+
@options[:headers] != false
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
def fill_row(row, column, model=nil)
|
81
|
+
case column
|
82
|
+
when String, Symbol
|
83
|
+
row.push(model ? model.send(column) : column)
|
84
|
+
when Hash
|
85
|
+
column.each{|key, values| fill_row(row, values, model && model.send(key))}
|
86
|
+
when Array
|
87
|
+
column.each{|value| fill_row(row, value, model)}
|
88
|
+
else
|
89
|
+
raise ArgumentError, "column #{column} has an invalid class (#{ column.class })"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
data/spec/array_spec.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
3
|
+
|
4
|
+
describe Array do
|
5
|
+
|
6
|
+
describe :to_xls do
|
7
|
+
it "should throw no error without data" do
|
8
|
+
lambda { [].to_xls }.should_not raise_error
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should return the correct string representing a spreadsheet" do
|
12
|
+
s = StringIO.new
|
13
|
+
xls = make_book(mock_users)
|
14
|
+
xls.write(s)
|
15
|
+
s.string.bytes.to_a.should == mock_users.to_xls.bytes.to_a
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,164 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe ToXls::ArrayWriter do
|
4
|
+
|
5
|
+
it "throws no error without data" do
|
6
|
+
lambda { [].to_xls }.should_not raise_error
|
7
|
+
end
|
8
|
+
|
9
|
+
describe ":name option" do
|
10
|
+
it "defaults to 'Sheet 1' for sheets with no name" do
|
11
|
+
make_book([]).worksheets.first.name.should == 'Sheet 1'
|
12
|
+
end
|
13
|
+
|
14
|
+
it "uses the :name option" do
|
15
|
+
make_book([], :name => 'Empty').worksheets.first.name.should == 'Empty'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe ":columns option" do
|
20
|
+
it "throws no error without columns" do
|
21
|
+
lambda { make_book([1,2,3]) }.should_not raise_error
|
22
|
+
end
|
23
|
+
it "throws an error if columns isn't an array" do
|
24
|
+
lambda { make_book([1,2,3], :columns => :foo) }.should raise_error
|
25
|
+
end
|
26
|
+
it "uses the attribute keys as columns if it exists" do
|
27
|
+
xls = make_book(mock_users)
|
28
|
+
check_sheet( xls.worksheets.first,
|
29
|
+
[ [:age, :email, :name],
|
30
|
+
[ 20, 'peter@gmail.com', 'Peter'],
|
31
|
+
[ 25, 'john@gmail.com', 'John'],
|
32
|
+
[ 27, 'day9@day9tv.com', 'Day9']
|
33
|
+
]
|
34
|
+
)
|
35
|
+
end
|
36
|
+
it "allows re-sorting of the columns by using the :columns option" do
|
37
|
+
xls = make_book(mock_users, :columns => [:name, :email, :age])
|
38
|
+
check_sheet( xls.worksheets.first,
|
39
|
+
[ [:name, :email, :age],
|
40
|
+
['Peter', 'peter@gmail.com', 20],
|
41
|
+
['John', 'john@gmail.com', 25],
|
42
|
+
['Day9', 'day9@day9tv.com', 27]
|
43
|
+
]
|
44
|
+
)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "works properly when you provide it with both data and column names" do
|
48
|
+
xls = make_book([1,2,3], :columns => [:to_s])
|
49
|
+
check_sheet( xls.worksheets.first, [ [:to_s], ['1'], ['2'], ['3'] ] )
|
50
|
+
end
|
51
|
+
|
52
|
+
it "picks data from associations" do
|
53
|
+
xls = make_book(mock_users, :columns => [:name, {:company => [:name]}])
|
54
|
+
check_sheet( xls.worksheets.first,
|
55
|
+
[ [:name, :name],
|
56
|
+
['Peter', 'Acme'],
|
57
|
+
['John', 'Acme'],
|
58
|
+
['Day9', 'EADS']
|
59
|
+
]
|
60
|
+
)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe ":headers option" do
|
65
|
+
|
66
|
+
it "uses the headers option if it exists" do
|
67
|
+
xls = make_book( mock_users,
|
68
|
+
:columns => [:name, :email, :age],
|
69
|
+
:headers => ['Nombre', 'Correo', 'Edad']
|
70
|
+
)
|
71
|
+
check_sheet( xls.worksheets.first,
|
72
|
+
[ ['Nombre', 'Correo', 'Edad'],
|
73
|
+
['Peter', 'peter@gmail.com', 20],
|
74
|
+
['John', 'john@gmail.com', 25],
|
75
|
+
['Day9', 'day9@day9tv.com', 27]
|
76
|
+
]
|
77
|
+
)
|
78
|
+
end
|
79
|
+
|
80
|
+
it "includes no headers if the headers option is false" do
|
81
|
+
xls = make_book( mock_users,
|
82
|
+
:columns => [:name, :email, :age],
|
83
|
+
:headers => false
|
84
|
+
)
|
85
|
+
check_sheet( xls.worksheets.first,
|
86
|
+
[ ['Peter', 'peter@gmail.com', 20],
|
87
|
+
['John', 'john@gmail.com', 25],
|
88
|
+
['Day9', 'day9@day9tv.com', 27]
|
89
|
+
]
|
90
|
+
)
|
91
|
+
end
|
92
|
+
|
93
|
+
it "picks data from associations" do
|
94
|
+
book = make_book( mock_users,
|
95
|
+
:columns => [:name, {:company => [:name]}],
|
96
|
+
:headers => [:name, :company_name]
|
97
|
+
)
|
98
|
+
check_sheet( book.worksheets.first,
|
99
|
+
[ [:name, :company_name],
|
100
|
+
['Peter', 'Acme'],
|
101
|
+
['John', 'Acme'],
|
102
|
+
['Day9', 'EADS']
|
103
|
+
]
|
104
|
+
)
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
describe "#write_book" do
|
110
|
+
it "writes a new sheet in a book" do
|
111
|
+
book = Spreadsheet::Workbook.new
|
112
|
+
ToXls::ArrayWriter.new(mock_users).write_book(book)
|
113
|
+
check_sheet( book.worksheets.first,
|
114
|
+
[ [:age, :email, :name],
|
115
|
+
[ 20, 'peter@gmail.com', 'Peter'],
|
116
|
+
[ 25, 'john@gmail.com', 'John'],
|
117
|
+
[ 27, 'day9@day9tv.com', 'Day9']
|
118
|
+
]
|
119
|
+
)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
describe "#write_sheet" do
|
124
|
+
it "writes a new sheet in a book" do
|
125
|
+
book = Spreadsheet::Workbook.new
|
126
|
+
sheet = book.create_worksheet
|
127
|
+
ToXls::ArrayWriter.new(mock_users).write_sheet(sheet)
|
128
|
+
check_sheet( sheet,
|
129
|
+
[ [:age, :email, :name],
|
130
|
+
[ 20, 'peter@gmail.com', 'Peter'],
|
131
|
+
[ 25, 'john@gmail.com', 'John'],
|
132
|
+
[ 27, 'day9@day9tv.com', 'Day9']
|
133
|
+
]
|
134
|
+
)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
describe "#write_io" do
|
139
|
+
it "writes a new book in a stream" do
|
140
|
+
io1 = StringIO.new
|
141
|
+
ToXls::ArrayWriter.new(mock_users).write_io(io1)
|
142
|
+
io2 = StringIO.new
|
143
|
+
xls = make_book(mock_users, {})
|
144
|
+
xls.write(io2)
|
145
|
+
|
146
|
+
io1.string.bytes.to_a.should == io2.string.bytes.to_a
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
describe "#write_string" do
|
151
|
+
it "writes a new sheet in a string" do
|
152
|
+
str = ToXls::ArrayWriter.new(mock_users).write_string()
|
153
|
+
io = StringIO.new
|
154
|
+
xls = make_book(mock_users, {})
|
155
|
+
xls.write(io)
|
156
|
+
|
157
|
+
str.bytes.to_a.should == io.string.bytes.to_a
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
|
164
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'factory_girl'
|
5
|
+
require 'to_xls'
|
6
|
+
|
7
|
+
# Requires supporting files with custom matchers and macros, etc,
|
8
|
+
# in ./support/ and its subdirectories.
|
9
|
+
Dir["#{File.dirname(__FILE__)}/support/**.rb", ].each {|f| require f}
|
10
|
+
|
11
|
+
RSpec.configure do |config|
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
def mock_model(name, attributes)
|
16
|
+
attributes[:attributes] = attributes.clone
|
17
|
+
mock(name, attributes)
|
18
|
+
end
|
19
|
+
|
20
|
+
def mock_company(name, address)
|
21
|
+
mock_model( name, :name => name, :address => address )
|
22
|
+
end
|
23
|
+
|
24
|
+
def mock_user(name, age, email, company)
|
25
|
+
user = mock_model(name, :name => name, :age => age, :email => email)
|
26
|
+
user.stub!(:company).and_return(company)
|
27
|
+
user
|
28
|
+
end
|
29
|
+
|
30
|
+
def mock_users
|
31
|
+
acme = mock_company('Acme', 'One Road')
|
32
|
+
eads = mock_company('EADS', 'Another Road')
|
33
|
+
|
34
|
+
[ mock_user('Peter', 20, 'peter@gmail.com', acme),
|
35
|
+
mock_user('John', 25, 'john@gmail.com', acme),
|
36
|
+
mock_user('Day9', 27, 'day9@day9tv.com', eads)
|
37
|
+
]
|
38
|
+
end
|
39
|
+
|
40
|
+
def check_sheet(sheet, array)
|
41
|
+
sheet.rows.each_with_index do |row, i|
|
42
|
+
row.should == array[i]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def make_book(array, options={})
|
47
|
+
book = Spreadsheet::Workbook.new
|
48
|
+
ToXls::ArrayWriter.new(array, options).write_book(book)
|
49
|
+
book
|
50
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: to_xls
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
- 2
|
10
|
-
version: 0.1.2
|
4
|
+
prerelease:
|
5
|
+
version: 1.0.0
|
11
6
|
platform: ruby
|
12
7
|
authors:
|
13
8
|
- Enrique Garcia Cota
|
@@ -16,7 +11,7 @@ autorequire:
|
|
16
11
|
bindir: bin
|
17
12
|
cert_chain: []
|
18
13
|
|
19
|
-
date:
|
14
|
+
date: 2011-06-26 00:00:00 +02:00
|
20
15
|
default_executable:
|
21
16
|
dependencies:
|
22
17
|
- !ruby/object:Gem::Dependency
|
@@ -27,34 +22,117 @@ dependencies:
|
|
27
22
|
requirements:
|
28
23
|
- - ">="
|
29
24
|
- !ruby/object:Gem::Version
|
30
|
-
hash: 3
|
31
|
-
segments:
|
32
|
-
- 0
|
33
25
|
version: "0"
|
34
26
|
type: :runtime
|
35
27
|
version_requirements: *id001
|
36
|
-
|
37
|
-
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rspec
|
30
|
+
prerelease: false
|
31
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
32
|
+
none: false
|
33
|
+
requirements:
|
34
|
+
- - ~>
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: 2.3.0
|
37
|
+
type: :development
|
38
|
+
version_requirements: *id002
|
39
|
+
- !ruby/object:Gem::Dependency
|
40
|
+
name: bundler
|
41
|
+
prerelease: false
|
42
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.0.0
|
48
|
+
type: :development
|
49
|
+
version_requirements: *id003
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: jeweler
|
52
|
+
prerelease: false
|
53
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ~>
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: 1.5.2
|
59
|
+
type: :development
|
60
|
+
version_requirements: *id004
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rcov
|
63
|
+
prerelease: false
|
64
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: "0"
|
70
|
+
type: :development
|
71
|
+
version_requirements: *id005
|
72
|
+
- !ruby/object:Gem::Dependency
|
73
|
+
name: factory_girl
|
74
|
+
prerelease: false
|
75
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: "0"
|
81
|
+
type: :development
|
82
|
+
version_requirements: *id006
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: spreadsheet
|
85
|
+
prerelease: false
|
86
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ">"
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: "0"
|
92
|
+
type: :runtime
|
93
|
+
version_requirements: *id007
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: rspec
|
96
|
+
prerelease: false
|
97
|
+
requirement: &id008 !ruby/object:Gem::Requirement
|
98
|
+
none: false
|
99
|
+
requirements:
|
100
|
+
- - ">"
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: 1.2.3
|
103
|
+
type: :development
|
104
|
+
version_requirements: *id008
|
105
|
+
description: Adds a to_xls method to arrays, which can be used to generate excel files conveniently. Can rely on ActiveRecord sugar for obtaining attribute names.
|
106
|
+
email: egarcia@splendeo.es
|
38
107
|
executables: []
|
39
108
|
|
40
109
|
extensions: []
|
41
110
|
|
42
111
|
extra_rdoc_files:
|
43
|
-
-
|
112
|
+
- LICENSE.txt
|
44
113
|
- README.rdoc
|
45
114
|
files:
|
46
|
-
-
|
115
|
+
- .document
|
116
|
+
- .rspec
|
117
|
+
- Gemfile
|
118
|
+
- LICENSE.txt
|
47
119
|
- README.rdoc
|
48
|
-
-
|
120
|
+
- Rakefile
|
121
|
+
- VERSION
|
49
122
|
- init.rb
|
50
123
|
- lib/to_xls.rb
|
124
|
+
- lib/to_xls/array_patch.rb
|
125
|
+
- lib/to_xls/array_writer.rb
|
126
|
+
- spec/array_spec.rb
|
127
|
+
- spec/array_writer_spec.rb
|
128
|
+
- spec/spec_helper.rb
|
51
129
|
has_rdoc: true
|
52
130
|
homepage: http://github.com/splendeo/to_xls
|
53
|
-
licenses:
|
54
|
-
|
131
|
+
licenses:
|
132
|
+
- MIT
|
55
133
|
post_install_message:
|
56
|
-
rdoc_options:
|
57
|
-
|
134
|
+
rdoc_options: []
|
135
|
+
|
58
136
|
require_paths:
|
59
137
|
- lib
|
60
138
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -62,25 +140,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
62
140
|
requirements:
|
63
141
|
- - ">="
|
64
142
|
- !ruby/object:Gem::Version
|
65
|
-
hash: 3
|
66
|
-
segments:
|
67
|
-
- 0
|
68
143
|
version: "0"
|
69
144
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
145
|
none: false
|
71
146
|
requirements:
|
72
147
|
- - ">="
|
73
148
|
- !ruby/object:Gem::Version
|
74
|
-
hash: 3
|
75
|
-
segments:
|
76
|
-
- 0
|
77
149
|
version: "0"
|
78
150
|
requirements: []
|
79
151
|
|
80
152
|
rubyforge_project:
|
81
|
-
rubygems_version: 1.
|
153
|
+
rubygems_version: 1.6.2
|
82
154
|
signing_key:
|
83
155
|
specification_version: 3
|
84
|
-
summary:
|
85
|
-
test_files:
|
86
|
-
|
156
|
+
summary: to_xls for Arrays and Hashes
|
157
|
+
test_files:
|
158
|
+
- spec/array_spec.rb
|
159
|
+
- spec/array_writer_spec.rb
|
160
|
+
- spec/spec_helper.rb
|
data/to_xls.gemspec
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
Gem::Specification.new do |s|
|
2
|
-
s.name = %q{to_xls}
|
3
|
-
s.version = "0.1.2"
|
4
|
-
|
5
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
6
|
-
s.authors = ["Enrique Garcia Cota", "Francisco de Juan"]
|
7
|
-
s.date = %q{2010-05-18}
|
8
|
-
s.description = %q{Transform an Array into a excel file using the spreadsheet gem.}
|
9
|
-
s.email = %q{github@splendeo.es}
|
10
|
-
s.extra_rdoc_files = [
|
11
|
-
"MIT-LICENSE",
|
12
|
-
"README.rdoc"
|
13
|
-
]
|
14
|
-
s.files = [
|
15
|
-
"MIT-LICENSE",
|
16
|
-
"README.rdoc",
|
17
|
-
"to_xls.gemspec",
|
18
|
-
"init.rb",
|
19
|
-
"lib/to_xls.rb"
|
20
|
-
]
|
21
|
-
s.homepage = %q{http://github.com/splendeo/to_xls}
|
22
|
-
s.rdoc_options = ["--charset=UTF-8"]
|
23
|
-
s.require_paths = ["lib"]
|
24
|
-
s.rubygems_version = %q{1.3.5}
|
25
|
-
s.summary = %q{To xls}
|
26
|
-
|
27
|
-
s.add_dependency("spreadsheet", ">= 0")
|
28
|
-
|
29
|
-
end
|