as_csv 1.0.0 → 1.0.2
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.
- data/Gemfile.lock +1 -1
- data/README.md +6 -3
- data/as_csv.gemspec +1 -0
- data/lib/as_csv/csv_builder.rb +1 -1
- data/lib/as_csv/csv_proxy.rb +13 -0
- data/lib/as_csv/version.rb +1 -1
- data/lib/as_csv.rb +4 -1
- data/spec/core_ext/array_spec.rb +1 -1
- data/spec/dummy/app/controllers/render_widgets_controller.rb +2 -2
- data/spec/dummy/app/controllers/respond_with_widgets_controller.rb +1 -1
- data/spec/dummy/app/models/widget_with_options.rb +1 -1
- data/spec/dummy/config/initializers/session_store.rb +1 -1
- data/spec/dummy/config/initializers/wrap_parameters.rb +1 -1
- data/spec/dummy/db/seeds.rb +2 -2
- data/spec/lib/csv_builder_spec.rb +11 -11
- data/spec/models/widget_spec.rb +5 -5
- data/spec/models/widget_with_options_spec.rb +3 -3
- data/spec/spec_helper.rb +2 -0
- data/spec/support/widgets_controller_mixin.rb +6 -6
- metadata +2 -1
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,9 +1,15 @@
|
|
1
1
|
# AsCSV
|
2
2
|
|
3
|
+
[](https://travis-ci.org/danielfone/as_csv)
|
4
|
+
[](https://gemnasium.com/danielfone/as_csv)
|
5
|
+
[](https://codeclimate.com/github/danielfone/as_csv)
|
6
|
+
|
3
7
|
This gem allows you to expose CSV in your apps as you'd expose JSON or XML.
|
4
8
|
|
5
9
|
Rails is not strictly required, but currently the magic only works with rails 3.x.
|
6
10
|
|
11
|
+
Ruby 1.8 will work, but by default the order of the columns will not be guaranteed.
|
12
|
+
|
7
13
|
## Installation
|
8
14
|
|
9
15
|
Add this line to your application's Gemfile:
|
@@ -161,6 +167,3 @@ bar2,bar2-description,,xyz98765
|
|
161
167
|
6. Commit your changes (`git commit -am 'Add some feature'`)
|
162
168
|
7. Push to the branch (`git push origin my-new-feature`)
|
163
169
|
8. Create new Pull Request
|
164
|
-
|
165
|
-
[](https://travis-ci.org/danielfone/as_csv)
|
166
|
-
[](https://gemnasium.com/danielfone/as_csv)
|
data/as_csv.gemspec
CHANGED
data/lib/as_csv/csv_builder.rb
CHANGED
@@ -0,0 +1,13 @@
|
|
1
|
+
if RUBY_VERSION >= '1.9'
|
2
|
+
require 'csv'
|
3
|
+
AsCSV::CSVProxy = CSV
|
4
|
+
else
|
5
|
+
begin
|
6
|
+
gem 'fastercsv'
|
7
|
+
require 'fastercsv'
|
8
|
+
|
9
|
+
AsCSV::CSVProxy = FasterCSV
|
10
|
+
rescue LoadError => e
|
11
|
+
raise "Error : FasterCSV not installed, please `gem install fastercsv` for faster processing on Ruby 1.8"
|
12
|
+
end
|
13
|
+
end
|
data/lib/as_csv/version.rb
CHANGED
data/lib/as_csv.rb
CHANGED
data/spec/core_ext/array_spec.rb
CHANGED
@@ -6,7 +6,7 @@ describe Array do
|
|
6
6
|
it { should respond_to(:to_csv_without_builder) }
|
7
7
|
|
8
8
|
context 'with objects responding to `as_csv`' do
|
9
|
-
before { subject << stub(:foo, as_csv
|
9
|
+
before { subject << stub(:foo, :as_csv => {:header => 'value'})}
|
10
10
|
|
11
11
|
specify do
|
12
12
|
AsCSV::CSVBuilder.any_instance.should_receive(:to_csv).and_call_original
|
@@ -2,13 +2,13 @@ class RenderWidgetsController < ApplicationController
|
|
2
2
|
|
3
3
|
def index
|
4
4
|
respond_to do |format|
|
5
|
-
format.csv { render csv
|
5
|
+
format.csv { render :csv => WidgetWithOptions.scoped, :style => :full }
|
6
6
|
end
|
7
7
|
end
|
8
8
|
|
9
9
|
def show
|
10
10
|
respond_to do |format|
|
11
|
-
format.csv { render csv
|
11
|
+
format.csv { render :csv => Widget.find(params[:id]) }
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
@@ -3,7 +3,7 @@ class WidgetWithOptions < Widget
|
|
3
3
|
def as_csv(options={})
|
4
4
|
style = options[:style] || raise("options[:style] not set")
|
5
5
|
case style
|
6
|
-
when :full then attributes.slice('name', 'code').merge({full
|
6
|
+
when :full then attributes.slice('name', 'code').merge({:full => true})
|
7
7
|
when :short then attributes.slice('name', 'code')
|
8
8
|
else raise "options[:style] must be :full or :short"
|
9
9
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# Be sure to restart your server when you modify this file.
|
2
2
|
|
3
|
-
Dummy::Application.config.session_store :cookie_store, key
|
3
|
+
Dummy::Application.config.session_store :cookie_store, :key => '_dummy_session'
|
4
4
|
|
5
5
|
# Use the database for sessions instead of the cookie-based default,
|
6
6
|
# which shouldn't be used to store highly confidential information
|
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
|
7
7
|
ActiveSupport.on_load(:action_controller) do
|
8
|
-
wrap_parameters format
|
8
|
+
wrap_parameters :format => [:json]
|
9
9
|
end
|
10
10
|
|
11
11
|
# Disable root element in JSON by default.
|
data/spec/dummy/db/seeds.rb
CHANGED
@@ -3,5 +3,5 @@
|
|
3
3
|
#
|
4
4
|
# Examples:
|
5
5
|
#
|
6
|
-
# cities = City.create([{ name
|
7
|
-
# Mayor.create(name
|
6
|
+
# cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }])
|
7
|
+
# Mayor.create(:name => 'Emanuel', :city => cities.first)
|
@@ -9,7 +9,7 @@ describe AsCSV::CSVBuilder do
|
|
9
9
|
|
10
10
|
describe '#rows' do
|
11
11
|
subject(:rows) { csv_builder.rows }
|
12
|
-
before { csv_builder.stub headers
|
12
|
+
before { csv_builder.stub :headers => headers, :data_rows => data_rows, "valid?" => true }
|
13
13
|
let(:headers) { %w(h1 h2) }
|
14
14
|
let(:data_rows) { [%w(v11 v12), %w(v21 v22)] }
|
15
15
|
|
@@ -19,10 +19,10 @@ describe AsCSV::CSVBuilder do
|
|
19
19
|
|
20
20
|
context 'with single record' do
|
21
21
|
let(:records) do
|
22
|
-
stub(:foo, as_csv
|
23
|
-
first
|
24
|
-
second
|
25
|
-
third
|
22
|
+
stub(:foo, :as_csv => {
|
23
|
+
:first => '1',
|
24
|
+
:second => '2',
|
25
|
+
:third => '3'
|
26
26
|
})
|
27
27
|
end
|
28
28
|
|
@@ -40,10 +40,10 @@ first,second,third
|
|
40
40
|
let(:records) { [record1, record2] }
|
41
41
|
2.times do |i|
|
42
42
|
let("record#{i+1}") do
|
43
|
-
stub(:foo, as_csv
|
44
|
-
first
|
45
|
-
second
|
46
|
-
third
|
43
|
+
stub(:foo, :as_csv => {
|
44
|
+
:first => "1#{i}",
|
45
|
+
:second => "2#{i}",
|
46
|
+
:third => "3#{i}"
|
47
47
|
})
|
48
48
|
end
|
49
49
|
end
|
@@ -63,7 +63,7 @@ first,second,third
|
|
63
63
|
let(:records) { [record1, record2] }
|
64
64
|
2.times do |i|
|
65
65
|
let("record#{i+1}") do
|
66
|
-
stub(:foo, as_csv
|
66
|
+
stub(:foo, :as_csv => {
|
67
67
|
"first#{i}" => "1#{i}",
|
68
68
|
"second#{i}" => "2#{i}",
|
69
69
|
"third#{i}" => "3#{i}"
|
@@ -101,7 +101,7 @@ first0,second0,third0,first1,second1,third1
|
|
101
101
|
end
|
102
102
|
|
103
103
|
context 'with record `as_csv` != Hash' do
|
104
|
-
let(:records) { stub(:foo, as_csv
|
104
|
+
let(:records) { stub(:foo, :as_csv => 'test') }
|
105
105
|
|
106
106
|
message = "expected as_csv to return Hash"
|
107
107
|
([:valid?] + EXPOSED_METHODS).each do |method|
|
data/spec/models/widget_spec.rb
CHANGED
@@ -5,7 +5,7 @@ describe Widget do
|
|
5
5
|
it { should respond_to :as_csv }
|
6
6
|
|
7
7
|
describe 'dummy' do
|
8
|
-
subject(:dummy_widget) { Widget.create! name
|
8
|
+
subject(:dummy_widget) { Widget.create! :name => "widget-name", :description => 'widget-description', :code => 1234 }
|
9
9
|
|
10
10
|
describe :to_csv do
|
11
11
|
subject { dummy_widget.to_csv }
|
@@ -21,10 +21,10 @@ describe Widget do
|
|
21
21
|
describe 'collection' do
|
22
22
|
before do
|
23
23
|
Widget.create! [
|
24
|
-
{ name
|
25
|
-
{ name
|
26
|
-
{ name
|
27
|
-
{ name
|
24
|
+
{ :name => "widget-1", :description => 'widget-description-1', :code => 1001 },
|
25
|
+
{ :name => "widget-2", :description => 'widget-description-2', :code => 1002 },
|
26
|
+
{ :name => "widget-3", :description => 'widget-description-3', :code => 1003 },
|
27
|
+
{ :name => "widget-4", :description => 'widget-description-4', :code => 1004 },
|
28
28
|
]
|
29
29
|
end
|
30
30
|
|
@@ -1,13 +1,13 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe WidgetWithOptions do
|
4
|
-
subject(:widget) { described_class.new name
|
4
|
+
subject(:widget) { described_class.new :name => 'widget-name', :code => 1234 }
|
5
5
|
|
6
6
|
describe :to_csv do
|
7
7
|
subject { widget.to_csv(options) }
|
8
8
|
|
9
9
|
context 'with style = :full' do
|
10
|
-
let(:options) { {style
|
10
|
+
let(:options) { {:style => :full} }
|
11
11
|
it do
|
12
12
|
should == <<-CSV.strip_heredoc
|
13
13
|
name,code,full
|
@@ -17,7 +17,7 @@ describe WidgetWithOptions do
|
|
17
17
|
end
|
18
18
|
|
19
19
|
context 'with style = :short' do
|
20
|
-
let(:options) { {style
|
20
|
+
let(:options) { {:style => :short} }
|
21
21
|
it do
|
22
22
|
should == <<-CSV.strip_heredoc
|
23
23
|
name,code
|
data/spec/spec_helper.rb
CHANGED
@@ -2,15 +2,15 @@ module WidgetsControllerMixin
|
|
2
2
|
def exercise_controller
|
3
3
|
before do
|
4
4
|
Widget.create! [
|
5
|
-
{ name
|
6
|
-
{ name
|
7
|
-
{ name
|
8
|
-
{ name
|
5
|
+
{ :name => "widget-1", :description => 'widget-description-1', :code => 1001 },
|
6
|
+
{ :name => "widget-2", :description => 'widget-description-2', :code => 1002 },
|
7
|
+
{ :name => "widget-3", :description => 'widget-description-3', :code => 1003 },
|
8
|
+
{ :name => "widget-4", :description => 'widget-description-4', :code => 1004 },
|
9
9
|
]
|
10
10
|
end
|
11
11
|
|
12
12
|
context 'GET show/1.csv' do
|
13
|
-
before { get :show, id
|
13
|
+
before { get :show, :id => 1, :format => :csv }
|
14
14
|
describe 'response' do
|
15
15
|
subject { response }
|
16
16
|
its(:code) { should == "200" }
|
@@ -25,7 +25,7 @@ module WidgetsControllerMixin
|
|
25
25
|
end
|
26
26
|
|
27
27
|
context 'GET index.csv' do
|
28
|
-
before { get :index, format
|
28
|
+
before { get :index, :format => :csv }
|
29
29
|
describe 'response' do
|
30
30
|
subject { response }
|
31
31
|
its(:code) { should == "200" }
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: as_csv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -79,6 +79,7 @@ files:
|
|
79
79
|
- lib/as_csv/active_model.rb
|
80
80
|
- lib/as_csv/core_ext/array.rb
|
81
81
|
- lib/as_csv/csv_builder.rb
|
82
|
+
- lib/as_csv/csv_proxy.rb
|
82
83
|
- lib/as_csv/version.rb
|
83
84
|
- spec/controllers/render_widgets_controller_spec.rb
|
84
85
|
- spec/controllers/respond_with_widgets_controller_spec.rb
|