cloudxls-rails 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/cloudxls-rails.gemspec +2 -2
- data/lib/cloudxls-rails/version.rb +2 -4
- data/lib/cloudxls-rails.rb +2 -2
- data/spec/csv_writer_spec.rb +1 -37
- metadata +3 -4
- data/lib/cloudxls-rails/csv_writer.rb +0 -108
data/.gitignore
CHANGED
data/cloudxls-rails.gemspec
CHANGED
@@ -13,9 +13,9 @@ Gem::Specification.new do |gem|
|
|
13
13
|
gem.files = `git ls-files`.split("\n")
|
14
14
|
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
15
15
|
gem.require_paths = ["lib"]
|
16
|
-
gem.version =
|
16
|
+
gem.version = CloudXLSRails::VERSION
|
17
17
|
|
18
|
-
gem.add_dependency('cloudxls', '~> 0.
|
18
|
+
gem.add_dependency('cloudxls', '~> 0.4.0')
|
19
19
|
|
20
20
|
gem.add_development_dependency "rake"
|
21
21
|
gem.add_development_dependency "webmock"
|
data/lib/cloudxls-rails.rb
CHANGED
data/spec/csv_writer_spec.rb
CHANGED
@@ -6,43 +6,7 @@ describe "CloudXLS::CSVWriter" do
|
|
6
6
|
end
|
7
7
|
|
8
8
|
describe "with array" do
|
9
|
-
|
10
|
-
expect( @writer.text([['foo','bar'],[1,2]]) ).to eq("foo,bar\n1,2")
|
11
|
-
end
|
12
|
-
|
13
|
-
it "should escape titles" do
|
14
|
-
expect( @writer.text([['bar"baz']]) ).to eq("\"bar\"\"baz\"")
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should escape rows" do
|
18
|
-
expect( @writer.text([['title'],['bar"baz']]) ).to eq("title\n\"bar\"\"baz\"")
|
19
|
-
end
|
20
|
-
|
21
|
-
it "should write YYYY-MM-DD for Date" do
|
22
|
-
expect( @writer.text([[Date.new(2012,12,24)]]) ).to eq("2012-12-24")
|
23
|
-
end
|
24
|
-
|
25
|
-
it "should write xmlschema for DateTime" do
|
26
|
-
# TODO: make UTC consistent
|
27
|
-
expect( @writer.text([[DateTime.new(2012,12,24,18,30,5,'+0000')]]) ).to eq("2012-12-24T18:30:05.000+0000")
|
28
|
-
expect( @writer.text([[DateTime.new(2012,12,24,18,30,5,'+0000').to_time.utc]]) ).to eq("2012-12-24T18:30:05.000+0000")
|
29
|
-
end
|
30
|
-
|
31
|
-
it "should write nothing for nil" do
|
32
|
-
expect( @writer.text([[nil,nil]]) ).to eq(",")
|
33
|
-
end
|
34
|
-
|
35
|
-
it "should write \"\" for empty string" do
|
36
|
-
expect( @writer.text([["",""]]) ).to eq('"",""')
|
37
|
-
end
|
38
|
-
|
39
|
-
it "should write integers" do
|
40
|
-
expect( @writer.text([[-1,0,1,1_000_000]]) ).to eq('-1,0,1,1000000')
|
41
|
-
end
|
42
|
-
|
43
|
-
it "should write floats" do
|
44
|
-
expect( @writer.text([[-1.0,0.0,1.0,1_000_000.0,1.234567]]) ).to eq('-1.0,0.0,1.0,1000000.0,1.234567')
|
45
|
-
end
|
9
|
+
# spec'ed in cloudlxs-ruby gem
|
46
10
|
end
|
47
11
|
|
48
12
|
describe "#text with AR" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cloudxls-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -18,7 +18,7 @@ dependencies:
|
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: 0.
|
21
|
+
version: 0.4.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -26,7 +26,7 @@ dependencies:
|
|
26
26
|
requirements:
|
27
27
|
- - ~>
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: 0.
|
29
|
+
version: 0.4.0
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
31
|
name: rake
|
32
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -121,7 +121,6 @@ files:
|
|
121
121
|
- cloudxls-rails.gemspec
|
122
122
|
- lib/cloudxls-rails.rb
|
123
123
|
- lib/cloudxls-rails/action_controller.rb
|
124
|
-
- lib/cloudxls-rails/csv_writer.rb
|
125
124
|
- lib/cloudxls-rails/version.rb
|
126
125
|
- spec/ci.sh
|
127
126
|
- spec/csv_writer_spec.rb
|
@@ -1,108 +0,0 @@
|
|
1
|
-
require 'csv'
|
2
|
-
|
3
|
-
module CloudXLS
|
4
|
-
class CSVWriter
|
5
|
-
DATETIME_FORMAT = "%FT%T.%L%z".freeze
|
6
|
-
DATE_FORMAT = "%F".freeze
|
7
|
-
|
8
|
-
# Generates CSV string.
|
9
|
-
#
|
10
|
-
# @param [Array<String/Symbol>] columns
|
11
|
-
# Method/attribute keys to for the export.
|
12
|
-
#
|
13
|
-
# @return [String]
|
14
|
-
# The full CSV as a string. Titleizes *columns* for the header.
|
15
|
-
#
|
16
|
-
def self.text(scope, options = {})
|
17
|
-
columns = options[:columns]
|
18
|
-
|
19
|
-
str = ::CSV.generate do |csv|
|
20
|
-
|
21
|
-
if options[:skip_headers] != true
|
22
|
-
if scope.respond_to?(:column_names)
|
23
|
-
columns ||= scope.column_names
|
24
|
-
end
|
25
|
-
if columns
|
26
|
-
csv << csv_titles(columns, :titleize)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
enum = scope_enumerator(scope)
|
31
|
-
scope.send(enum) do |record|
|
32
|
-
csv << csv_row(record, columns)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
str.strip!
|
36
|
-
end
|
37
|
-
|
38
|
-
# Example
|
39
|
-
#
|
40
|
-
# Post.csv_enumerator(Post.all, [:title, :author, :published_at])
|
41
|
-
#
|
42
|
-
# @param [ActiveRecord::Scope] scope
|
43
|
-
# An activerecord scope object for the records to be exported.
|
44
|
-
# Example: Post.all.limit(500).where(author: "foo")
|
45
|
-
#
|
46
|
-
# @return [Enumerator] enumerator to use for streaming response.
|
47
|
-
#
|
48
|
-
def self.enumerator(scope, options = {})
|
49
|
-
columns = options[:columns]
|
50
|
-
|
51
|
-
Enumerator.new do |row|
|
52
|
-
if options[:skip_headers] != true
|
53
|
-
if scope.respond_to?(:column_names)
|
54
|
-
columns ||= scope.column_names
|
55
|
-
end
|
56
|
-
if columns
|
57
|
-
row << csv_titles(columns, :titleize).to_csv
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
enum = scope_enumerator(scope)
|
62
|
-
scope.send(enum) do |record|
|
63
|
-
row << csv_row(record, columns).to_csv
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
private
|
69
|
-
|
70
|
-
|
71
|
-
def self.csv_row(obj, columns = [])
|
72
|
-
if obj.is_a?(Array)
|
73
|
-
obj.map{ |el| encode_for_csv(el) }
|
74
|
-
else
|
75
|
-
columns.map do |key|
|
76
|
-
encode_for_csv(obj.send(key))
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
|
82
|
-
def self.encode_for_csv(val)
|
83
|
-
case val
|
84
|
-
when DateTime,Time then val.strftime(DATETIME_FORMAT)
|
85
|
-
when Date then val.strftime(DATE_FORMAT)
|
86
|
-
else
|
87
|
-
val
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
def self.csv_titles(column_names, strategy = :titleize)
|
92
|
-
column_names.map do |c|
|
93
|
-
title = c.to_s
|
94
|
-
title = title.send(strategy) if title.respond_to?(strategy)
|
95
|
-
title
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
|
100
|
-
def self.scope_enumerator(scope)
|
101
|
-
if scope.respond_to?(:find_each)
|
102
|
-
:find_each
|
103
|
-
else
|
104
|
-
:each
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|
108
|
-
end
|