as_csv 1.0.0 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- as_csv (0.0.1.pre)
4
+ as_csv (1.0.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,9 +1,15 @@
1
1
  # AsCSV
2
2
 
3
+ [![Build Status](https://travis-ci.org/danielfone/as_csv.png)](https://travis-ci.org/danielfone/as_csv)
4
+ [![Dependency Status](https://gemnasium.com/danielfone/as_csv.png)](https://gemnasium.com/danielfone/as_csv)
5
+ [![Code Climate](https://codeclimate.com/badge.png)](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
- [![Build Status](https://travis-ci.org/danielfone/as_csv.png)](https://travis-ci.org/danielfone/as_csv)
166
- [![Dependency Status](https://gemnasium.com/danielfone/as_csv.png)](https://gemnasium.com/danielfone/as_csv)
data/as_csv.gemspec CHANGED
@@ -20,4 +20,5 @@ Gem::Specification.new do |gem|
20
20
  gem.add_development_dependency "rails", "~> 3.2"
21
21
  gem.add_development_dependency "rspec-rails", "~> 2.12"
22
22
  gem.add_development_dependency "sqlite3"
23
+ gem.add_development_dependency "fastercsv" if RUBY_VERSION < '1.9'
23
24
  end
@@ -8,7 +8,7 @@ module AsCSV
8
8
  end
9
9
 
10
10
  def to_csv
11
- rows.collect { |row| CSV.generate_line row }.join if valid?
11
+ rows.collect { |row| CSVProxy.generate_line row }.join if valid?
12
12
  end
13
13
 
14
14
  def rows
@@ -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
@@ -1,3 +1,3 @@
1
1
  module AsCSV
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.2"
3
3
  end
data/lib/as_csv.rb CHANGED
@@ -1,6 +1,9 @@
1
- require "csv"
2
1
  require "as_csv/version"
2
+ require "as_csv/csv_proxy"
3
3
  require "as_csv/csv_builder"
4
4
  require "as_csv/core_ext/array"
5
5
  require "as_csv/active_model"
6
6
  require "as_csv/action_renderer"
7
+
8
+ module AsCSV
9
+ end
@@ -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: {header: 'value'})}
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: WidgetWithOptions.scoped, style: :full }
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: Widget.find(params[:id]) }
11
+ format.csv { render :csv => Widget.find(params[:id]) }
12
12
  end
13
13
  end
14
14
 
@@ -2,7 +2,7 @@ class RespondWithWidgetsController < ApplicationController
2
2
  respond_to :csv
3
3
 
4
4
  def index
5
- respond_with WidgetWithOptions.scoped, style: :full
5
+ respond_with WidgetWithOptions.scoped, :style => :full
6
6
  end
7
7
 
8
8
  def show
@@ -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: true})
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: '_dummy_session'
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: [:json]
8
+ wrap_parameters :format => [:json]
9
9
  end
10
10
 
11
11
  # Disable root element in JSON by default.
@@ -3,5 +3,5 @@
3
3
  #
4
4
  # Examples:
5
5
  #
6
- # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
7
- # Mayor.create(name: 'Emanuel', city: cities.first)
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: headers, data_rows: data_rows, "valid?" => true }
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: '1',
24
- second: '2',
25
- third: '3'
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: "1#{i}",
45
- second: "2#{i}",
46
- third: "3#{i}"
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: 'test') }
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|
@@ -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: "widget-name", description: 'widget-description', code: 1234 }
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: "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 },
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: 'widget-name', code: 1234 }
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: :full} }
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: :short} }
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
@@ -23,5 +23,7 @@ RSpec.configure do |config|
23
23
 
24
24
  config.treat_symbols_as_metadata_keys_with_true_values = true
25
25
 
26
+ config.order = "random"
27
+
26
28
  config.extend WidgetsControllerMixin, :widgets_controllers
27
29
  end
@@ -2,15 +2,15 @@ module WidgetsControllerMixin
2
2
  def exercise_controller
3
3
  before do
4
4
  Widget.create! [
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 },
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: 1, format: :csv }
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: :csv }
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.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