comma 4.1.0 → 4.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +5 -5
  2. data/.rubocop.yml +12 -1
  3. data/.rubocop_todo.yml +3 -364
  4. data/.travis.yml +20 -38
  5. data/Appraisals +20 -1
  6. data/Gemfile +5 -1
  7. data/Gemfile.lock +54 -36
  8. data/README.md +59 -0
  9. data/Rakefile +2 -0
  10. data/comma.gemspec +12 -15
  11. data/gemfiles/active5.0.7.2.gemfile +12 -0
  12. data/gemfiles/active5.0.7.2.gemfile.lock +113 -0
  13. data/gemfiles/active5.1.7.gemfile +12 -0
  14. data/gemfiles/active5.1.7.gemfile.lock +113 -0
  15. data/gemfiles/active5.2.4.3.gemfile +12 -0
  16. data/gemfiles/active5.2.4.3.gemfile.lock +113 -0
  17. data/gemfiles/active6.0.3.1.gemfile +12 -0
  18. data/gemfiles/active6.0.3.1.gemfile.lock +113 -0
  19. data/gemfiles/{rails4.2.8.gemfile → rails5.0.7.2.gemfile} +5 -2
  20. data/gemfiles/rails5.0.7.2.gemfile.lock +204 -0
  21. data/gemfiles/{rails4.1.16.gemfile → rails5.1.7.gemfile} +5 -2
  22. data/gemfiles/rails5.1.7.gemfile.lock +204 -0
  23. data/gemfiles/{rails5.0.1.gemfile → rails5.2.4.3.gemfile} +5 -2
  24. data/gemfiles/rails5.2.4.3.gemfile.lock +212 -0
  25. data/gemfiles/{rails4.0.13.gemfile → rails6.0.3.1.gemfile} +4 -2
  26. data/gemfiles/rails6.0.3.1.gemfile.lock +227 -0
  27. data/init.rb +2 -0
  28. data/lib/comma.rb +26 -36
  29. data/lib/comma/array.rb +2 -0
  30. data/lib/comma/data_extractor.rb +7 -9
  31. data/lib/comma/data_mapper_collection.rb +8 -4
  32. data/lib/comma/extractor.rb +2 -6
  33. data/lib/comma/generator.rb +12 -13
  34. data/lib/comma/header_extractor.rb +19 -15
  35. data/lib/comma/mongoid.rb +10 -7
  36. data/lib/comma/object.rb +5 -2
  37. data/lib/comma/relation.rb +16 -10
  38. data/lib/comma/version.rb +3 -1
  39. data/spec/comma/comma_spec.rb +71 -50
  40. data/spec/comma/data_extractor_spec.rb +13 -18
  41. data/spec/comma/header_extractor_spec.rb +8 -15
  42. data/spec/comma/rails/active_record_spec.rb +34 -34
  43. data/spec/comma/rails/data_mapper_collection_spec.rb +4 -4
  44. data/spec/comma/rails/mongoid_spec.rb +8 -8
  45. data/spec/controllers/users_controller_spec.rb +83 -70
  46. data/spec/non_rails_app/ruby_classes.rb +13 -6
  47. data/spec/rails_app/active_record/config.rb +2 -2
  48. data/spec/rails_app/active_record/models.rb +3 -3
  49. data/spec/rails_app/data_mapper/config.rb +1 -1
  50. data/spec/rails_app/mongoid/config.rb +3 -3
  51. data/spec/rails_app/rails_app.rb +13 -13
  52. data/spec/rails_app/tmp/.gitkeep +0 -0
  53. data/spec/spec_helper.rb +21 -5
  54. metadata +29 -46
  55. data/README.markdown +0 -353
  56. data/gemfiles/active4.0.13.gemfile +0 -10
  57. data/gemfiles/active4.0.13.gemfile.lock +0 -107
  58. data/gemfiles/active4.1.16.gemfile +0 -10
  59. data/gemfiles/active4.1.16.gemfile.lock +0 -106
  60. data/gemfiles/active4.2.8.gemfile +0 -10
  61. data/gemfiles/active4.2.8.gemfile.lock +0 -105
  62. data/gemfiles/active5.0.1.gemfile +0 -10
  63. data/gemfiles/active5.0.1.gemfile.lock +0 -104
  64. data/gemfiles/active5.1.0.gemfile +0 -10
  65. data/gemfiles/active5.1.0.gemfile.lock +0 -104
  66. data/gemfiles/rails4.0.13.gemfile.lock +0 -158
  67. data/gemfiles/rails4.1.16.gemfile.lock +0 -163
  68. data/gemfiles/rails4.2.8.gemfile.lock +0 -187
  69. data/gemfiles/rails5.0.1.gemfile.lock +0 -194
  70. data/gemfiles/rails5.1.0.gemfile +0 -11
  71. data/gemfiles/rails5.1.0.gemfile.lock +0 -194
@@ -1,17 +1,21 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Book
2
4
  attr_accessor :name, :description, :isbn
3
5
 
4
6
  def initialize(name, description, isbn)
5
- @name, @description, @isbn = name, description, isbn
7
+ @name = name
8
+ @description = description
9
+ @isbn = isbn
6
10
  end
7
11
 
8
12
  comma do
9
13
  name 'Title'
10
14
  description
11
15
 
12
- isbn :authority => :issuer
13
- isbn :number_10 => 'ISBN-10'
14
- isbn :number_13 => 'ISBN-13'
16
+ isbn authority: :issuer
17
+ isbn number_10: 'ISBN-10'
18
+ isbn number_13: 'ISBN-13'
15
19
  end
16
20
 
17
21
  comma :brief do
@@ -24,8 +28,11 @@ class Isbn
24
28
  attr_accessor :number_10, :number_13
25
29
 
26
30
  def initialize(isbn_10, isbn_13)
27
- @number_10, @number_13 = isbn_10, isbn_13
31
+ @number_10 = isbn_10
32
+ @number_13 = isbn_13
28
33
  end
29
34
 
30
- def authority; 'ISBN'; end
35
+ def authority
36
+ 'ISBN'
37
+ end
31
38
  end
@@ -1,4 +1,4 @@
1
- # -*- coding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
- ActiveRecord::Base.configurations = {'test' => {'adapter' => 'sqlite3', 'database' => ':memory:'}}
3
+ ActiveRecord::Base.configurations = { 'test' => { 'adapter' => 'sqlite3', 'database' => ':memory:' } }
4
4
  ActiveRecord::Base.establish_connection(:test)
@@ -1,4 +1,4 @@
1
- # -*- coding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
3
  class Post < ActiveRecord::Base
4
4
  has_one :user
@@ -15,7 +15,7 @@ class User < ActiveRecord::Base
15
15
  comma do
16
16
  first_name
17
17
  last_name
18
- full_name "Name"
18
+ full_name 'Name'
19
19
  end
20
20
 
21
21
  comma :shortened do
@@ -29,7 +29,7 @@ class User < ActiveRecord::Base
29
29
  end
30
30
 
31
31
  MIGRATION_CLASS =
32
- if Rails::VERSION::STRING =~ /^5.*/
32
+ if Rails::VERSION::STRING =~ /^[56].*/
33
33
  ActiveRecord::Migration[4.2]
34
34
  else
35
35
  ActiveRecord::Migration
@@ -1,3 +1,3 @@
1
- # -*- coding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
3
  DataMapper.setup(:default, 'sqlite::memory:')
@@ -1,9 +1,9 @@
1
- # -*- coding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
3
  Mongoid.configure do |config|
4
4
  config.sessions = {
5
- :default => {
6
- :hosts => ['localhost:27017'], :database => 'comma_test'
5
+ default: {
6
+ hosts: ['localhost:27017'], database: 'comma_test'
7
7
  }
8
8
  }
9
9
  end
@@ -1,13 +1,14 @@
1
- # -*- coding: utf-8 -*-
1
+ # frozen_string_literal: true
2
+
2
3
  require 'action_controller/railtie'
3
4
  require 'action_view/railtie'
4
5
 
5
6
  # orm configs
6
7
  require 'rails_app/active_record/config' if defined?(ActiveRecord)
7
8
 
8
- app = Class.new(Rails::Application)
9
+ app = CommaTestApp = Class.new(Rails::Application)
9
10
  app.config.secret_token = '6f6acf0443f74fd0aa8ff07a7c2fbe0a'
10
- app.config.session_store :cookie_store, :key => '_rails_app_session'
11
+ app.config.session_store :cookie_store, key: '_rails_app_session'
11
12
  app.config.active_support.deprecation = :log
12
13
  app.config.eager_load = false
13
14
  app.config.root = File.dirname(__FILE__)
@@ -15,10 +16,10 @@ Rails.backtrace_cleaner.remove_silencers!
15
16
  app.initialize!
16
17
 
17
18
  app.routes.draw do
18
- resources :users, :only => [:index]
19
- get 'with_custom_options', :to => 'users#with_custom_options'
20
- get 'with_custom_style', :to => 'users#with_custom_style'
21
- root :to => 'users#index'
19
+ resources :users, only: [:index]
20
+ get 'with_custom_options', to: 'users#with_custom_options'
21
+ get 'with_custom_style', to: 'users#with_custom_style'
22
+ root to: 'users#index'
22
23
  end
23
24
 
24
25
  # models
@@ -49,17 +50,17 @@ class UsersController < ApplicationController
49
50
  respond_to do |format|
50
51
  format.html do
51
52
  if is_rails_4?
52
- render :text => 'Users!'
53
+ render text: 'Users!'
53
54
  else
54
- render :plain => 'Users!'
55
+ render plain: 'Users!'
55
56
  end
56
57
  end
57
- format.csv { render :csv => User.all }
58
+ format.csv { render csv: User.all }
58
59
  end
59
60
  end
60
61
 
61
62
  def with_custom_options
62
- render_options = {:csv => User.all}.update(symbolize_param_keys(params[:custom_options]))
63
+ render_options = { csv: User.all }.update(symbolize_param_keys(params[:custom_options]))
63
64
 
64
65
  respond_to do |format|
65
66
  format.csv { render render_options }
@@ -68,10 +69,9 @@ class UsersController < ApplicationController
68
69
 
69
70
  def with_custom_style
70
71
  respond_to do |format|
71
- format.csv { render :csv => User.all, :style => :shortened }
72
+ format.csv { render csv: User.all, style: :shortened }
72
73
  end
73
74
  end
74
-
75
75
  end
76
76
 
77
77
  # helpers
File without changes
@@ -1,9 +1,23 @@
1
- # -*- coding: utf-8 -*-
1
+ # frozen_string_literal: true
2
+
2
3
  require 'rubygems'
3
- $LOAD_PATH.unshift(File.expand_path(File.join(*%w{.. .. lib}), __FILE__))
4
+ $LOAD_PATH.unshift(File.expand_path(File.join('..', '..', 'lib'), __FILE__))
4
5
 
6
+ require 'simplecov'
5
7
  require 'coveralls'
6
- Coveralls.wear!
8
+ SimpleCov.formatter = Coveralls::SimpleCov::Formatter
9
+ if defined? Rails
10
+ SimpleCov.start('rails') do
11
+ add_filter %r{^/spec/comma/rails/data_mapper_collection_spec\.rb$}
12
+ add_filter %r{^/spec/comma/rails/mongoid_spec\.rb$}
13
+ end
14
+ else
15
+ SimpleCov.start do
16
+ add_filter %r{^/spec/comma/rails/data_mapper_collection_spec\.rb}
17
+ add_filter %r{^/spec/comma/rails/mongoid_spec\.rb}
18
+ add_filter %r{^/spec/controllers/}
19
+ end
20
+ end
7
21
 
8
22
  require 'bundler/setup'
9
23
  Bundler.require
@@ -14,13 +28,15 @@ require 'rspec/its'
14
28
  begin
15
29
  require 'rails'
16
30
  rescue LoadError
31
+ warn 'rails not loaded'
17
32
  end
18
33
 
19
- %w{data_mapper mongoid active_record}.each do |orm|
34
+ %w[data_mapper mongoid active_record].each do |orm|
20
35
  begin
21
36
  require orm
22
37
  break
23
38
  rescue LoadError
39
+ warn "#{orm} not loaded"
24
40
  end
25
41
  end
26
42
 
@@ -35,4 +51,4 @@ end
35
51
 
36
52
  Dir[File.dirname(__FILE__) + '/support/**/*.rb'].each { |file| require file }
37
53
 
38
- require File.expand_path('../../spec/non_rails_app/ruby_classes' , __FILE__)
54
+ require File.expand_path('../spec/non_rails_app/ruby_classes', __dir__)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: comma
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.0
4
+ version: 4.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcus Crafter
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-05-07 00:00:00.000000000 Z
12
+ date: 2020-06-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -17,20 +17,20 @@ dependencies:
17
17
  requirements:
18
18
  - - ">="
19
19
  - !ruby/object:Gem::Version
20
- version: 4.0.0
20
+ version: 4.2.0
21
21
  - - "<"
22
22
  - !ruby/object:Gem::Version
23
- version: '5.2'
23
+ version: '6.1'
24
24
  type: :runtime
25
25
  prerelease: false
26
26
  version_requirements: !ruby/object:Gem::Requirement
27
27
  requirements:
28
28
  - - ">="
29
29
  - !ruby/object:Gem::Version
30
- version: 4.0.0
30
+ version: 4.2.0
31
31
  - - "<"
32
32
  - !ruby/object:Gem::Version
33
- version: '5.2'
33
+ version: '6.1'
34
34
  - !ruby/object:Gem::Dependency
35
35
  name: appraisal
36
36
  requirement: !ruby/object:Gem::Requirement
@@ -51,14 +51,14 @@ dependencies:
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 10.5.0
54
+ version: 13.0.1
55
55
  type: :development
56
56
  prerelease: false
57
57
  version_requirements: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: 10.5.0
61
+ version: 13.0.1
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: rspec
64
64
  requirement: !ruby/object:Gem::Requirement
@@ -101,20 +101,6 @@ dependencies:
101
101
  - - ">="
102
102
  - !ruby/object:Gem::Version
103
103
  version: '0'
104
- - !ruby/object:Gem::Dependency
105
- name: sqlite3
106
- requirement: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - "~>"
109
- - !ruby/object:Gem::Version
110
- version: 1.3.11
111
- type: :development
112
- prerelease: false
113
- version_requirements: !ruby/object:Gem::Requirement
114
- requirements:
115
- - - "~>"
116
- - !ruby/object:Gem::Version
117
- version: 1.3.11
118
104
  description: Ruby Comma Seperated Values generation library
119
105
  email:
120
106
  - crafterm@redartisan.com
@@ -133,29 +119,25 @@ files:
133
119
  - Gemfile
134
120
  - Gemfile.lock
135
121
  - MIT-LICENSE
136
- - README.markdown
122
+ - README.md
137
123
  - Rakefile
138
124
  - comma.gemspec
139
- - gemfiles/active4.0.13.gemfile
140
- - gemfiles/active4.0.13.gemfile.lock
141
- - gemfiles/active4.1.16.gemfile
142
- - gemfiles/active4.1.16.gemfile.lock
143
- - gemfiles/active4.2.8.gemfile
144
- - gemfiles/active4.2.8.gemfile.lock
145
- - gemfiles/active5.0.1.gemfile
146
- - gemfiles/active5.0.1.gemfile.lock
147
- - gemfiles/active5.1.0.gemfile
148
- - gemfiles/active5.1.0.gemfile.lock
149
- - gemfiles/rails4.0.13.gemfile
150
- - gemfiles/rails4.0.13.gemfile.lock
151
- - gemfiles/rails4.1.16.gemfile
152
- - gemfiles/rails4.1.16.gemfile.lock
153
- - gemfiles/rails4.2.8.gemfile
154
- - gemfiles/rails4.2.8.gemfile.lock
155
- - gemfiles/rails5.0.1.gemfile
156
- - gemfiles/rails5.0.1.gemfile.lock
157
- - gemfiles/rails5.1.0.gemfile
158
- - gemfiles/rails5.1.0.gemfile.lock
125
+ - gemfiles/active5.0.7.2.gemfile
126
+ - gemfiles/active5.0.7.2.gemfile.lock
127
+ - gemfiles/active5.1.7.gemfile
128
+ - gemfiles/active5.1.7.gemfile.lock
129
+ - gemfiles/active5.2.4.3.gemfile
130
+ - gemfiles/active5.2.4.3.gemfile.lock
131
+ - gemfiles/active6.0.3.1.gemfile
132
+ - gemfiles/active6.0.3.1.gemfile.lock
133
+ - gemfiles/rails5.0.7.2.gemfile
134
+ - gemfiles/rails5.0.7.2.gemfile.lock
135
+ - gemfiles/rails5.1.7.gemfile
136
+ - gemfiles/rails5.1.7.gemfile.lock
137
+ - gemfiles/rails5.2.4.3.gemfile
138
+ - gemfiles/rails5.2.4.3.gemfile.lock
139
+ - gemfiles/rails6.0.3.1.gemfile
140
+ - gemfiles/rails6.0.3.1.gemfile.lock
159
141
  - init.rb
160
142
  - lib/comma.rb
161
143
  - lib/comma/array.rb
@@ -181,8 +163,9 @@ files:
181
163
  - spec/rails_app/data_mapper/config.rb
182
164
  - spec/rails_app/mongoid/config.rb
183
165
  - spec/rails_app/rails_app.rb
166
+ - spec/rails_app/tmp/.gitkeep
184
167
  - spec/spec_helper.rb
185
- homepage: http://github.com/crafterm/comma
168
+ homepage: http://github.com/comma-csv/comma
186
169
  licenses:
187
170
  - MIT
188
171
  metadata: {}
@@ -201,8 +184,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
201
184
  - !ruby/object:Gem::Version
202
185
  version: '0'
203
186
  requirements: []
204
- rubyforge_project: comma
205
- rubygems_version: 2.5.2
187
+ rubygems_version: 3.1.2
206
188
  signing_key:
207
189
  specification_version: 4
208
190
  summary: Ruby Comma Seperated Values generation library
@@ -220,4 +202,5 @@ test_files:
220
202
  - spec/rails_app/data_mapper/config.rb
221
203
  - spec/rails_app/mongoid/config.rb
222
204
  - spec/rails_app/rails_app.rb
205
+ - spec/rails_app/tmp/.gitkeep
223
206
  - spec/spec_helper.rb
@@ -1,353 +0,0 @@
1
- #COMMA
2
-
3
- http://github.com/comma-csv/comma
4
-
5
- [![Gem Version](https://badge.fury.io/rb/comma.png)](http://badge.fury.io/rb/comma)
6
-
7
- ##COMPATIBILITY
8
- The mainline of this project builds gems to the 3.x version series, and is compatible and tested with :
9
-
10
- * Ruby 2.x
11
- * Rails 4.x, 5.0 (with ActiveRecord, DataMapper and Mongoid support)
12
-
13
- [![Build Status](https://travis-ci.org/comma-csv/comma.png?branch=master)](https://travis-ci.org/comma-csv/comma) [![Code Climate](https://codeclimate.com/github/comma-csv/comma.png)](https://codeclimate.com/github/comma-csv/comma)
14
-
15
- Comma is distributed as a gem, best installed via Bundler.
16
-
17
- Include the gem in your Gemfile:
18
-
19
- ```Ruby
20
- gem "comma", "~> 4.0.0"
21
- ```
22
-
23
- Or, if you want to live life on the edge, you can get master from the main comma repository:
24
-
25
- ```Ruby
26
- gem "comma", :git => "git://github.com/comma-csv/comma.git"
27
- ```
28
-
29
- ##DESCRIPTION:
30
-
31
- Comma is a CSV (i.e. comma separated values) generation extension for Ruby objects, that lets you seamlessly define a CSV output format via a small DSL. Comma works well on pure Ruby objects with attributes, as well as complex ones such as ActiveRecord objects with associations, extensions, etc. It doesn't distinguish between attributes, methods, associations, extensions, etc. - they all are considered equal and invoked identically via the Comma DSL description. Multiple different CSV output descriptions can also be defined.
32
-
33
- When multiple objects in an Array are converted to CSV, the output includes generation of a header row reflected from names of the properties requested, or specified via the DSL.
34
-
35
- CSV can be a bit of a boring format - the motivation behind Comma was to have a CSV extension that was simple, flexible, and would treat attributes, methods, associations, etc., all the same without the need for any complex configuration, and also work on Ruby objects, not just ActiveRecord or other base class derivatives.
36
-
37
- An example Comma CSV enabled ActiveRecord class:
38
-
39
- ```Ruby
40
-
41
- class Book < ActiveRecord::Base
42
-
43
- # ================
44
- # = Associations =
45
- # ================
46
- has_many :pages
47
- has_one :isbn
48
- belongs_to :publisher
49
-
50
- # ===============
51
- # = CSV support =
52
- # ===============
53
- comma do
54
-
55
- name
56
- description
57
-
58
- pages :size => 'Pages'
59
- publisher :name
60
- isbn :number_10 => 'ISBN-10', :number_13 => 'ISBN-13'
61
- blurb 'Summary'
62
-
63
- end
64
-
65
- end
66
-
67
- ```
68
-
69
- Annotated, the comma description is as follows:
70
-
71
- ```Ruby
72
- # starts a Comma description block, generating 2 methods #to_comma and
73
- # #to_comma_headers for this class.
74
- comma do
75
-
76
- # name, description are attributes of Book with the header being reflected as
77
- # 'Name', 'Description'
78
- name
79
- description
80
-
81
- # pages is an association returning an array, :size is called on the
82
- # association results, with the header name specified as 'Pages'
83
- pages :size => 'Pages'
84
-
85
- # publisher is an association returning an object, :name is called on the
86
- # associated object, with the reflected header 'Name'
87
- publisher :name
88
-
89
- # isbn is an association returning an object, :number_10 and :number_13 are
90
- # called on the object with the specified headers 'ISBN-10' and 'ISBN-13'
91
- isbn :number_10 => 'ISBN-10', :number_13 => 'ISBN-13'
92
-
93
- # blurb is an attribute of Book, with the header being specified directly
94
- # as 'Summary'
95
- blurb 'Summary'
96
-
97
- end
98
-
99
- ```
100
-
101
- In the above example, any of the declarations (name, description, pages, publisher, isbn, blurb, etc), could be methods, attributes, associations, etc - no distinction during configuration is required, as everything is invoked via Ruby's #send method.
102
-
103
- You can get the CSV representation of any object by calling the to_comma method, optionally providing a CSV description name to use.
104
-
105
- Object values are automatically converted to strings via to_s allowing you to reuse any existing to_s methods on your objects (instead of having to call particular properties or define CSV specific output methods). Header names are also automatically humanised when reflected (eg. Replacing _ characters with whitespace). The 'isbn' example above shows how multiple values can be added to the CSV output.
106
-
107
- Multiple CSV descriptions can also be specified for the same class, eg:
108
-
109
- ```Ruby
110
-
111
- class Book < ActiveRecord::Base
112
-
113
- # ================
114
- # = Associations =
115
- # ================
116
- has_many :pages
117
- has_one :isbn
118
- belongs_to :publisher
119
-
120
- # ===============
121
- # = CSV support =
122
- # ===============
123
- comma do
124
-
125
- name
126
- description
127
-
128
- pages :size => 'Pages'
129
- publisher :name
130
- isbn :number_10 => 'ISBN-10', :number_13 => 'ISBN-13'
131
- blurb 'Summary'
132
-
133
- end
134
-
135
- comma :brief do
136
-
137
- name
138
- description
139
- blurb 'Summary'
140
-
141
- end
142
-
143
- end
144
-
145
- ```
146
-
147
- You can specify which output format you would like to use via an optional parameter to to_comma:
148
-
149
- ```Ruby
150
- Book.limited(10).to_comma(:brief)
151
- ```
152
-
153
- Specifying no description name to to_comma is equivalent to specifying :default as the description name.
154
-
155
- You can pass all options for the CSV renderer (see : http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV.html ), e.g.
156
-
157
- ```Ruby
158
- Book.limited(10).to_comma(:style => :brief,
159
- :col_sep => ';',
160
- :force_quotes => true)
161
- ```
162
-
163
- You can pass the :filename option and have Comma writes the CSV output to this file:
164
-
165
- ```Ruby
166
- Book.limited(10).to_comma(:filename => 'books.csv')
167
- ```
168
-
169
- You also can pass the :write_header option to hide the header line (true is default):
170
-
171
- ```Ruby
172
- Book.limited(10).to_comma(:write_headers => false)
173
- ```
174
-
175
- ##Using blocks
176
-
177
- For more complex relationships you can pass blocks for calculated values, or related values. Following the previous example here is a comma set using blocks (both with and without labels for your CSV headings):
178
-
179
- ```Ruby
180
-
181
- class Publisher < ActiveRecord::Base
182
- # ================
183
- # = Associations =
184
- # ================
185
- has_one :primary_contact, :class_name => "User" #(basic user with a name)
186
- has_many :users
187
- end
188
-
189
- class Book < ActiveRecord::Base
190
-
191
- # ================
192
- # = Associations =
193
- # ================
194
- has_many :pages
195
- has_one :isbn
196
- belongs_to :publisher
197
-
198
- # ===============
199
- # = CSV support =
200
- # ===============
201
- comma do
202
- name
203
- description
204
-
205
- pages :size => 'Pages'
206
- publisher :name
207
- publisher { |publisher| publisher.primary_contact.name.to_s.titleize }
208
- publisher 'Number of publisher users' do |publisher| publisher.users.size end
209
- isbn :number_10 => 'ISBN-10', :number_13 => 'ISBN-13'
210
- blurb 'Summary'
211
-
212
- end
213
-
214
- end
215
-
216
- ```
217
-
218
- In the preceding example, the 2 new fields added (both based on the publisher relationship) mean that the following will be added:
219
-
220
- * the first example 'publishers_contact' is loaded straight as a block. The value returned by the lambda is displayed with a header value of 'Publisher'
221
- * the second example 'total_publishers_users' is sent via a hash and a custom label is set, if used in the first examples method the header would be 'Publisher', but sent as a hash the header is 'Number of publisher users'.
222
-
223
- ##Using special fields
224
-
225
- ###\_\_use\_\_
226
-
227
- With `__use__` field, you can reuse output formats that are defined in
228
- the same class. In the example below, default format (`:default`)
229
- includes `:minimum` format when `#to_comma` is called.
230
-
231
- ```ruby
232
- class Book < ActiveRecord::Base
233
- comma do
234
- __use__ :minimum
235
-
236
- description
237
-
238
- isbn :number_10 => 'ISBN-10', :number_13 => 'ISBN-13'
239
- end
240
-
241
- comma :minimum do
242
- name
243
- end
244
- end
245
- ```
246
-
247
- ###\_\_static_column\_\_
248
-
249
- When you want to have static value in your CSV output, you can use
250
- `__static__` field. You can provide values to output to blocks.
251
- Without block, field will become empty.
252
-
253
- ```ruby
254
- class Book < ActiveRecord::Base
255
- comma do
256
- __static_column__ 'Check' do ' ' end
257
- name
258
- __static_column__ 'Spacer'
259
- description
260
- end
261
- end
262
- ```
263
-
264
- ##USING WITH RAILS
265
-
266
- When used with Rails (ie. add 'comma' as a gem dependency), Comma automatically adds support for rendering CSV output in your controllers:
267
-
268
- ```Ruby
269
-
270
- class BooksController < ApplicationController
271
-
272
- def index
273
- respond_to do |format|
274
- format.csv { render :csv => Book.limited(50) }
275
- end
276
- end
277
-
278
- end
279
-
280
- ```
281
-
282
- You can specify which output format you would like to use by specifying a style parameter or adding any available CSV option:
283
-
284
- ```Ruby
285
-
286
- class BooksController < ApplicationController
287
-
288
- def index
289
- respond_to do |format|
290
- format.csv { render :csv => Book.limited(50), :style => :brief }
291
- end
292
- end
293
-
294
- end
295
-
296
- ```
297
-
298
- You can also specify a different file extension ('csv' by default)
299
-
300
- ```Ruby
301
-
302
- class BooksController < ApplicationController
303
-
304
- def index
305
- respond_to do |format|
306
- format.csv { render :csv => Book.limited(50), :extension => 'txt' }
307
- end
308
- end
309
-
310
- end
311
-
312
- ```
313
-
314
- With the Rails renderer you can supply any of the regular parameters that you would use with to_comma such as :filename,
315
- :write_headers, :force_quotes, etc. The parameters just need to be supplied after you specify the collection for the csv as demonstrated
316
- above.
317
-
318
- When used with Rails 3.+, Comma also adds support for exporting scopes:
319
-
320
- ```Ruby
321
-
322
- class Book < ActiveRecord::Base
323
- scope :recent,
324
- lambda { { :conditions => ['created_at > ?', 1.month.ago] } }
325
-
326
- # ...
327
- end
328
-
329
- ```
330
-
331
- Calling the to_comma method on the scope will internally use Rails' find_each method, instantiating only 1,000 ActiveRecord objects at a time:
332
-
333
- ```Ruby
334
-
335
- Book.recent.to_comma
336
-
337
- ```
338
-
339
- ##DEPENDENCIES
340
-
341
- If you have any questions or suggestions for Comma, please feel free to contact me at tom@venombytes.com, all feedback welcome!
342
-
343
- ##TESTING
344
-
345
- To run the test suite across multiple gem file sets, we're using [Appraisal](https://github.com/thoughtbot/appraisal), use the following commands :
346
-
347
- ```Bash
348
-
349
- bundle install
350
- bundle exec appraisal install
351
- bundle exec appraisal rake spec
352
-
353
- ```