comma 4.1.0 → 4.4.0

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.
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
@@ -0,0 +1,227 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ comma (4.4.0)
5
+ activesupport (>= 4.2.0, < 6.1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actioncable (6.0.3.1)
11
+ actionpack (= 6.0.3.1)
12
+ nio4r (~> 2.0)
13
+ websocket-driver (>= 0.6.1)
14
+ actionmailbox (6.0.3.1)
15
+ actionpack (= 6.0.3.1)
16
+ activejob (= 6.0.3.1)
17
+ activerecord (= 6.0.3.1)
18
+ activestorage (= 6.0.3.1)
19
+ activesupport (= 6.0.3.1)
20
+ mail (>= 2.7.1)
21
+ actionmailer (6.0.3.1)
22
+ actionpack (= 6.0.3.1)
23
+ actionview (= 6.0.3.1)
24
+ activejob (= 6.0.3.1)
25
+ mail (~> 2.5, >= 2.5.4)
26
+ rails-dom-testing (~> 2.0)
27
+ actionpack (6.0.3.1)
28
+ actionview (= 6.0.3.1)
29
+ activesupport (= 6.0.3.1)
30
+ rack (~> 2.0, >= 2.0.8)
31
+ rack-test (>= 0.6.3)
32
+ rails-dom-testing (~> 2.0)
33
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
34
+ actiontext (6.0.3.1)
35
+ actionpack (= 6.0.3.1)
36
+ activerecord (= 6.0.3.1)
37
+ activestorage (= 6.0.3.1)
38
+ activesupport (= 6.0.3.1)
39
+ nokogiri (>= 1.8.5)
40
+ actionview (6.0.3.1)
41
+ activesupport (= 6.0.3.1)
42
+ builder (~> 3.1)
43
+ erubi (~> 1.4)
44
+ rails-dom-testing (~> 2.0)
45
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
46
+ activejob (6.0.3.1)
47
+ activesupport (= 6.0.3.1)
48
+ globalid (>= 0.3.6)
49
+ activemodel (6.0.3.1)
50
+ activesupport (= 6.0.3.1)
51
+ activerecord (6.0.3.1)
52
+ activemodel (= 6.0.3.1)
53
+ activesupport (= 6.0.3.1)
54
+ activestorage (6.0.3.1)
55
+ actionpack (= 6.0.3.1)
56
+ activejob (= 6.0.3.1)
57
+ activerecord (= 6.0.3.1)
58
+ marcel (~> 0.3.1)
59
+ activesupport (6.0.3.1)
60
+ concurrent-ruby (~> 1.0, >= 1.0.2)
61
+ i18n (>= 0.7, < 2)
62
+ minitest (~> 5.1)
63
+ tzinfo (~> 1.1)
64
+ zeitwerk (~> 2.2, >= 2.2.2)
65
+ appraisal (1.0.3)
66
+ bundler
67
+ rake
68
+ thor (>= 0.14.0)
69
+ ast (2.4.1)
70
+ builder (3.2.4)
71
+ concurrent-ruby (1.1.6)
72
+ coveralls (0.8.23)
73
+ json (>= 1.8, < 3)
74
+ simplecov (~> 0.16.1)
75
+ term-ansicolor (~> 1.3)
76
+ thor (>= 0.19.4, < 2.0)
77
+ tins (~> 1.6)
78
+ crass (1.0.6)
79
+ diff-lcs (1.3)
80
+ docile (1.3.2)
81
+ erubi (1.9.0)
82
+ globalid (0.4.2)
83
+ activesupport (>= 4.2.0)
84
+ i18n (1.8.3)
85
+ concurrent-ruby (~> 1.0)
86
+ jaro_winkler (1.5.4)
87
+ json (2.3.0)
88
+ loofah (2.5.0)
89
+ crass (~> 1.0.2)
90
+ nokogiri (>= 1.5.9)
91
+ mail (2.7.1)
92
+ mini_mime (>= 0.1.1)
93
+ marcel (0.3.3)
94
+ mimemagic (~> 0.3.2)
95
+ method_source (1.0.0)
96
+ mimemagic (0.3.5)
97
+ mini_mime (1.0.2)
98
+ mini_portile2 (2.4.0)
99
+ minitest (5.14.1)
100
+ nio4r (2.5.2)
101
+ nokogiri (1.10.9)
102
+ mini_portile2 (~> 2.4.0)
103
+ parallel (1.19.1)
104
+ parser (2.7.1.3)
105
+ ast (~> 2.4.0)
106
+ power_assert (1.2.0)
107
+ psych (3.1.0)
108
+ rack (2.2.3)
109
+ rack-test (1.1.0)
110
+ rack (>= 1.0, < 3)
111
+ rails (6.0.3.1)
112
+ actioncable (= 6.0.3.1)
113
+ actionmailbox (= 6.0.3.1)
114
+ actionmailer (= 6.0.3.1)
115
+ actionpack (= 6.0.3.1)
116
+ actiontext (= 6.0.3.1)
117
+ actionview (= 6.0.3.1)
118
+ activejob (= 6.0.3.1)
119
+ activemodel (= 6.0.3.1)
120
+ activerecord (= 6.0.3.1)
121
+ activestorage (= 6.0.3.1)
122
+ activesupport (= 6.0.3.1)
123
+ bundler (>= 1.3.0)
124
+ railties (= 6.0.3.1)
125
+ sprockets-rails (>= 2.0.0)
126
+ rails-dom-testing (2.0.3)
127
+ activesupport (>= 4.2.0)
128
+ nokogiri (>= 1.6)
129
+ rails-html-sanitizer (1.3.0)
130
+ loofah (~> 2.3)
131
+ railties (6.0.3.1)
132
+ actionpack (= 6.0.3.1)
133
+ activesupport (= 6.0.3.1)
134
+ method_source
135
+ rake (>= 0.8.7)
136
+ thor (>= 0.20.3, < 2.0)
137
+ rainbow (3.0.0)
138
+ rake (13.0.1)
139
+ rspec (3.5.0)
140
+ rspec-core (~> 3.5.0)
141
+ rspec-expectations (~> 3.5.0)
142
+ rspec-mocks (~> 3.5.0)
143
+ rspec-activemodel-mocks (1.1.0)
144
+ activemodel (>= 3.0)
145
+ activesupport (>= 3.0)
146
+ rspec-mocks (>= 2.99, < 4.0)
147
+ rspec-core (3.5.4)
148
+ rspec-support (~> 3.5.0)
149
+ rspec-expectations (3.5.0)
150
+ diff-lcs (>= 1.2.0, < 2.0)
151
+ rspec-support (~> 3.5.0)
152
+ rspec-its (1.3.0)
153
+ rspec-core (>= 3.0.0)
154
+ rspec-expectations (>= 3.0.0)
155
+ rspec-mocks (3.5.0)
156
+ diff-lcs (>= 1.2.0, < 2.0)
157
+ rspec-support (~> 3.5.0)
158
+ rspec-rails (3.5.2)
159
+ actionpack (>= 3.0)
160
+ activesupport (>= 3.0)
161
+ railties (>= 3.0)
162
+ rspec-core (~> 3.5.0)
163
+ rspec-expectations (~> 3.5.0)
164
+ rspec-mocks (~> 3.5.0)
165
+ rspec-support (~> 3.5.0)
166
+ rspec-support (3.5.0)
167
+ rubocop (0.67.2)
168
+ jaro_winkler (~> 1.5.1)
169
+ parallel (~> 1.10)
170
+ parser (>= 2.5, != 2.5.1.1)
171
+ psych (>= 3.1.0)
172
+ rainbow (>= 2.2.2, < 4.0)
173
+ ruby-progressbar (~> 1.7)
174
+ unicode-display_width (>= 1.4.0, < 1.6)
175
+ rubocop-performance (1.1.0)
176
+ rubocop (>= 0.67.0)
177
+ ruby-progressbar (1.10.1)
178
+ simplecov (0.16.1)
179
+ docile (~> 1.1)
180
+ json (>= 1.8, < 3)
181
+ simplecov-html (~> 0.10.0)
182
+ simplecov-html (0.10.2)
183
+ sprockets (3.7.2)
184
+ concurrent-ruby (~> 1.0)
185
+ rack (> 1, < 3)
186
+ sprockets-rails (3.2.1)
187
+ actionpack (>= 4.0)
188
+ activesupport (>= 4.0)
189
+ sprockets (>= 3.0.0)
190
+ sqlite3 (1.4.2)
191
+ sync (0.5.0)
192
+ term-ansicolor (1.7.1)
193
+ tins (~> 1.0)
194
+ test-unit (3.3.6)
195
+ power_assert
196
+ thor (1.0.1)
197
+ thread_safe (0.3.6)
198
+ tins (1.25.0)
199
+ sync
200
+ tzinfo (1.2.7)
201
+ thread_safe (~> 0.1)
202
+ unicode-display_width (1.5.0)
203
+ websocket-driver (0.7.2)
204
+ websocket-extensions (>= 0.1.0)
205
+ websocket-extensions (0.1.5)
206
+ zeitwerk (2.3.0)
207
+
208
+ PLATFORMS
209
+ ruby
210
+
211
+ DEPENDENCIES
212
+ appraisal (~> 1.0.0)
213
+ comma!
214
+ coveralls
215
+ rails (= 6.0.3.1)
216
+ rake (~> 13.0.1)
217
+ rspec (~> 3.5.0)
218
+ rspec-activemodel-mocks
219
+ rspec-its
220
+ rspec-rails
221
+ rubocop (~> 0.67.2)
222
+ rubocop-performance
223
+ sqlite3
224
+ test-unit
225
+
226
+ BUNDLED WITH
227
+ 1.17.3
data/init.rb CHANGED
@@ -1 +1,3 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'comma'
@@ -1,27 +1,13 @@
1
- # load the right csv library
2
- if RUBY_VERSION >= '1.9'
3
- require 'csv'
4
- CSV_HANDLER = CSV
5
- else
6
- begin
7
- gem 'fastercsv'
8
- require 'fastercsv'
1
+ # frozen_string_literal: true
9
2
 
10
- CSV_HANDLER = FasterCSV
11
- rescue LoadError => e
12
- raise "Error : FasterCSV not installed, please `gem install fastercsv` for faster processing on <Ruby 1.9"
13
- end
14
- end
15
-
16
- if defined? Rails and (Rails.version.split('.').map(&:to_i).first < 3)
17
- raise "Error - This Comma version only supports Rails 3.x. Please use a 2.x version of Comma for use with earlier rails versions."
18
- end
3
+ require 'csv'
4
+ CSV_HANDLER = CSV
19
5
 
20
6
  module Comma
21
7
  DEFAULT_OPTIONS = {
22
- :write_headers => true,
23
- :style => :default
24
- }
8
+ write_headers: true,
9
+ style: :default
10
+ }.freeze
25
11
  end
26
12
 
27
13
  require 'active_support/lazy_load_hooks'
@@ -39,30 +25,34 @@ require 'comma/generator'
39
25
  require 'comma/array'
40
26
  require 'comma/object'
41
27
 
42
- #Load into Rails controllers
28
+ # Load into Rails controllers
43
29
  ActiveSupport.on_load(:action_controller) do
44
30
  if defined?(ActionController::Renderers) && ActionController::Renderers.respond_to?(:add)
45
31
  ActionController::Renderers.add :csv do |obj, options|
46
32
  filename = options[:filename] || 'data'
47
33
  extension = options[:extension] || 'csv'
48
-
49
- if Rails.version >= "5.0.0"
50
- mime_type = options[:mime_type] || Mime[:csv]
51
- else
52
- mime_type = options[:mime_type] || Mime::CSV
53
- end
54
-
55
- #Capture any CSV optional settings passed to comma or comma specific options
56
- csv_options = options.slice(*CSV_HANDLER::DEFAULT_OPTIONS.merge(Comma::DEFAULT_OPTIONS).keys).each_with_object({}) do |(k, v), h|
34
+ mime_type = if Rails.version >= '5.0.0'
35
+ options[:mime_type] || Mime[:csv]
36
+ else
37
+ options[:mime_type] || Mime::CSV
38
+ end
39
+ with_bom = options.delete(:with_bom) || false
40
+
41
+ # Capture any CSV optional settings passed to comma or comma specific options
42
+ csv_options = options.slice(*CSV_HANDLER::DEFAULT_OPTIONS.merge(Comma::DEFAULT_OPTIONS).keys)
43
+ csv_options = csv_options.each_with_object({}) do |(k, v), h|
57
44
  # XXX: Convert string to boolean
58
45
  h[k] = case k
59
- when :write_headers
60
- v = (v != 'false') if v.is_a?(String)
61
- else
62
- v
63
- end
46
+ when :write_headers
47
+ (v != 'false') if v.is_a?(String)
48
+ else
49
+ v
50
+ end
64
51
  end
65
- send_data obj.to_comma(csv_options), :type => mime_type, :disposition => "attachment; filename=\"#{filename}.#{extension}\""
52
+ data = obj.to_comma(csv_options)
53
+ data = "\xEF\xBB\xBF#{data}" if with_bom
54
+ disposition = "attachment; filename=\"#{filename}.#{extension}\""
55
+ send_data data, type: mime_type, disposition: disposition
66
56
  end
67
57
  end
68
58
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Array
2
4
  def to_comma(style = :default)
3
5
  Comma::Generator.new(self, style).run(:each)
@@ -1,10 +1,9 @@
1
- # -*- coding: utf-8 -*-
1
+ # frozen_string_literal: true
2
+
2
3
  require 'comma/extractor'
3
4
 
4
5
  module Comma
5
-
6
6
  class DataExtractor < Extractor
7
-
8
7
  class ExtractValueFromInstance
9
8
  def initialize(instance)
10
9
  @instance = instance
@@ -43,7 +42,7 @@ module Comma
43
42
 
44
43
  def null_association
45
44
  @null_association ||= Class.new(Class.const_defined?(:BasicObject) ? ::BasicObject : ::Object) do
46
- def method_missing(symbol, *args, &block)
45
+ def method_missing(_symbol, *_args, &_block)
47
46
  nil
48
47
  end
49
48
  end.new
@@ -51,14 +50,13 @@ module Comma
51
50
  end
52
51
 
53
52
  def method_missing(sym, *args, &block)
54
- if args.blank?
55
- @results << ExtractValueFromInstance.new(@instance).extract(sym, &block)
56
- end
53
+ @results << ExtractValueFromInstance.new(@instance).extract(sym, &block) if
54
+ args.blank?
57
55
 
58
56
  args.each do |arg|
59
57
  case arg
60
58
  when Hash
61
- arg.each do |k, v|
59
+ arg.each do |k, _v|
62
60
  @results << ExtractValueFromAssociationOfInstance.new(@instance, sym).extract(k, &block)
63
61
  end
64
62
  when Symbol
@@ -71,7 +69,7 @@ module Comma
71
69
  end
72
70
  end
73
71
 
74
- def __static_column__(header = nil, &block)
72
+ def __static_column__(_header = nil, &block)
75
73
  @results << (block ? yield(@instance) : nil)
76
74
  end
77
75
  end
@@ -1,7 +1,11 @@
1
- # -*- coding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
- class DataMapper::Collection
4
- def to_comma(style = :default)
5
- Comma::Generator.new(self, style).run(:each)
3
+ if defined?(DataMapper)
4
+ module DataMapper
5
+ class Collection
6
+ def to_comma(style = :default)
7
+ Comma::Generator.new(self, style).run(:each)
8
+ end
9
+ end
6
10
  end
7
11
  end
@@ -1,9 +1,7 @@
1
- # -*- coding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Comma
4
-
5
4
  class Extractor
6
-
7
5
  def initialize(instance, style, formats)
8
6
  @instance = instance
9
7
  @style = style
@@ -12,7 +10,7 @@ module Comma
12
10
  end
13
11
 
14
12
  def results
15
- instance_eval &@formats[@style]
13
+ instance_eval(&@formats[@style])
16
14
  @results.map { |r| convert_to_data_value(r) }
17
15
  end
18
16
 
@@ -30,7 +28,5 @@ module Comma
30
28
  def convert_to_data_value(result)
31
29
  result.nil? ? result : result.to_s
32
30
  end
33
-
34
31
  end
35
-
36
32
  end
@@ -1,24 +1,24 @@
1
- module Comma
1
+ # frozen_string_literal: true
2
2
 
3
+ module Comma
3
4
  class Generator
4
-
5
5
  def initialize(instance, style)
6
6
  @instance = instance
7
7
  @style = style
8
8
  @options = {}
9
9
 
10
- if @style.is_a? Hash
11
- @options = @style.clone
12
- @style = @options.delete(:style) || Comma::DEFAULT_OPTIONS[:style]
13
- @filename = @options.delete(:filename)
14
- end
10
+ return unless @style.is_a?(Hash)
11
+
12
+ @options = @style.clone
13
+ @style = @options.delete(:style) || Comma::DEFAULT_OPTIONS[:style]
14
+ @filename = @options.delete(:filename)
15
15
  end
16
16
 
17
17
  def run(iterator_method)
18
18
  if @filename
19
- CSV_HANDLER.open(@filename, 'w', @options){ |csv| append_csv(csv, iterator_method) } and return true
19
+ CSV_HANDLER.open(@filename, 'w', **@options) { |csv| append_csv(csv, iterator_method) } && (return true)
20
20
  else
21
- CSV_HANDLER.generate(@options){ |csv| append_csv(csv, iterator_method) }
21
+ CSV_HANDLER.generate(**@options) { |csv| append_csv(csv, iterator_method) }
22
22
  end
23
23
  end
24
24
 
@@ -26,13 +26,12 @@ module Comma
26
26
 
27
27
  def append_csv(csv, iterator_method)
28
28
  return '' if @instance.empty?
29
- unless @options.has_key?(:write_headers) && !@options[:write_headers]
30
- csv << @instance.first.to_comma_headers(@style)
31
- end
29
+
30
+ csv << @instance.first.to_comma_headers(@style) unless
31
+ @options.key?(:write_headers) && !@options[:write_headers]
32
32
  @instance.send(iterator_method) do |object|
33
33
  csv << object.to_comma(@style)
34
34
  end
35
35
  end
36
-
37
36
  end
38
37
  end