comma 4.2.0 → 4.5.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.
- checksums.yaml +4 -4
- data/.rubocop.yml +12 -1
- data/.rubocop_todo.yml +3 -364
- data/.travis.yml +26 -57
- data/Appraisals +31 -1
- data/Gemfile +5 -1
- data/Gemfile.lock +54 -36
- data/README.md +3 -3
- data/Rakefile +2 -0
- data/comma.gemspec +13 -14
- data/gemfiles/active5.0.7.2.gemfile +12 -0
- data/gemfiles/active5.0.7.2.gemfile.lock +113 -0
- data/gemfiles/active5.1.7.gemfile +12 -0
- data/gemfiles/active5.1.7.gemfile.lock +113 -0
- data/gemfiles/active5.2.4.3.gemfile +12 -0
- data/gemfiles/{active5.2.0.gemfile.lock → active5.2.4.3.gemfile.lock} +48 -41
- data/gemfiles/active6.0.3.1.gemfile +12 -0
- data/gemfiles/active6.0.3.1.gemfile.lock +113 -0
- data/gemfiles/active6.1.0.gemfile +12 -0
- data/gemfiles/active6.1.0.gemfile.lock +112 -0
- data/gemfiles/{rails5.0.1.gemfile → rails5.0.7.2.gemfile} +5 -2
- data/gemfiles/rails5.0.7.2.gemfile.lock +204 -0
- data/gemfiles/{rails4.2.8.gemfile → rails5.1.7.gemfile} +5 -2
- data/gemfiles/{rails5.2.0.gemfile.lock → rails5.1.7.gemfile.lock} +97 -97
- data/gemfiles/rails5.2.4.3.gemfile +14 -0
- data/gemfiles/rails5.2.4.3.gemfile.lock +212 -0
- data/gemfiles/{rails4.1.16.gemfile → rails6.0.3.1.gemfile} +4 -2
- data/gemfiles/rails6.0.3.1.gemfile.lock +227 -0
- data/gemfiles/{rails4.0.13.gemfile → rails6.1.0.gemfile} +4 -2
- data/gemfiles/rails6.1.0.gemfile.lock +230 -0
- data/init.rb +2 -0
- data/lib/comma.rb +26 -35
- data/lib/comma/array.rb +2 -0
- data/lib/comma/data_extractor.rb +5 -5
- data/lib/comma/data_mapper_collection.rb +9 -3
- data/lib/comma/extractor.rb +3 -5
- data/lib/comma/generator.rb +11 -11
- data/lib/comma/header_extractor.rb +19 -14
- data/lib/comma/mongoid.rb +10 -7
- data/lib/comma/object.rb +5 -1
- data/lib/comma/relation.rb +16 -10
- data/lib/comma/version.rb +3 -1
- data/spec/comma/comma_spec.rb +71 -50
- data/spec/comma/data_extractor_spec.rb +13 -17
- data/spec/comma/header_extractor_spec.rb +7 -13
- data/spec/comma/rails/active_record_spec.rb +34 -33
- data/spec/comma/rails/data_mapper_collection_spec.rb +4 -3
- data/spec/comma/rails/mongoid_spec.rb +8 -7
- data/spec/controllers/users_controller_spec.rb +83 -70
- data/spec/non_rails_app/ruby_classes.rb +13 -6
- data/spec/rails_app/active_record/config.rb +3 -1
- data/spec/rails_app/active_record/models.rb +4 -2
- data/spec/rails_app/data_mapper/config.rb +2 -0
- data/spec/rails_app/mongoid/config.rb +4 -2
- data/spec/rails_app/rails_app.rb +12 -11
- data/spec/rails_app/tmp/.gitkeep +0 -0
- data/spec/spec_helper.rb +21 -4
- metadata +31 -48
- data/gemfiles/active4.0.13.gemfile +0 -10
- data/gemfiles/active4.0.13.gemfile.lock +0 -107
- data/gemfiles/active4.1.16.gemfile +0 -10
- data/gemfiles/active4.1.16.gemfile.lock +0 -106
- data/gemfiles/active4.2.8.gemfile +0 -10
- data/gemfiles/active4.2.8.gemfile.lock +0 -105
- data/gemfiles/active5.0.1.gemfile +0 -10
- data/gemfiles/active5.0.1.gemfile.lock +0 -104
- data/gemfiles/active5.1.0.gemfile +0 -10
- data/gemfiles/active5.1.0.gemfile.lock +0 -104
- data/gemfiles/active5.2.0.gemfile +0 -10
- data/gemfiles/rails4.0.13.gemfile.lock +0 -158
- data/gemfiles/rails4.1.16.gemfile.lock +0 -162
- data/gemfiles/rails4.2.8.gemfile.lock +0 -187
- data/gemfiles/rails5.0.1.gemfile.lock +0 -194
- data/gemfiles/rails5.1.0.gemfile +0 -11
- data/gemfiles/rails5.1.0.gemfile.lock +0 -194
- data/gemfiles/rails5.2.0.gemfile +0 -11
data/init.rb
CHANGED
data/lib/comma.rb
CHANGED
@@ -1,26 +1,13 @@
|
|
1
|
-
#
|
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
|
-
|
11
|
-
|
12
|
-
raise "Error : FasterCSV not installed, please `gem install fastercsv` for faster processing on <Ruby 1.9"
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
raise "Error - This Comma version only supports Rails 3.x. Please use a 2.x version of Comma for use with earlier rails versions." if
|
17
|
-
defined? Rails and (Rails.version.split('.').map(&:to_i).first < 3)
|
3
|
+
require 'csv'
|
4
|
+
CSV_HANDLER = CSV
|
18
5
|
|
19
6
|
module Comma
|
20
7
|
DEFAULT_OPTIONS = {
|
21
|
-
|
22
|
-
|
23
|
-
|
8
|
+
write_headers: true,
|
9
|
+
style: :default
|
10
|
+
}.freeze
|
24
11
|
end
|
25
12
|
|
26
13
|
require 'active_support/lazy_load_hooks'
|
@@ -38,30 +25,34 @@ require 'comma/generator'
|
|
38
25
|
require 'comma/array'
|
39
26
|
require 'comma/object'
|
40
27
|
|
41
|
-
#Load into Rails controllers
|
28
|
+
# Load into Rails controllers
|
42
29
|
ActiveSupport.on_load(:action_controller) do
|
43
30
|
if defined?(ActionController::Renderers) && ActionController::Renderers.respond_to?(:add)
|
44
31
|
ActionController::Renderers.add :csv do |obj, options|
|
45
32
|
filename = options[:filename] || 'data'
|
46
33
|
extension = options[:extension] || 'csv'
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
#Capture any CSV optional settings passed to comma or comma specific options
|
55
|
-
csv_options = options.slice(*CSV_HANDLER::DEFAULT_OPTIONS.merge(Comma::DEFAULT_OPTIONS).keys)
|
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|
|
56
44
|
# XXX: Convert string to boolean
|
57
45
|
h[k] = case k
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
46
|
+
when :write_headers
|
47
|
+
(v != 'false') if v.is_a?(String)
|
48
|
+
else
|
49
|
+
v
|
50
|
+
end
|
63
51
|
end
|
64
|
-
|
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
|
65
56
|
end
|
66
57
|
end
|
67
58
|
end
|
data/lib/comma/array.rb
CHANGED
data/lib/comma/data_extractor.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'comma/extractor'
|
2
4
|
|
3
5
|
module Comma
|
4
|
-
|
5
6
|
class DataExtractor < Extractor
|
6
|
-
|
7
7
|
class ExtractValueFromInstance
|
8
8
|
def initialize(instance)
|
9
9
|
@instance = instance
|
@@ -42,7 +42,7 @@ module Comma
|
|
42
42
|
|
43
43
|
def null_association
|
44
44
|
@null_association ||= Class.new(Class.const_defined?(:BasicObject) ? ::BasicObject : ::Object) do
|
45
|
-
def method_missing(
|
45
|
+
def method_missing(_symbol, *_args, &_block)
|
46
46
|
nil
|
47
47
|
end
|
48
48
|
end.new
|
@@ -56,7 +56,7 @@ module Comma
|
|
56
56
|
args.each do |arg|
|
57
57
|
case arg
|
58
58
|
when Hash
|
59
|
-
arg.each do |k,
|
59
|
+
arg.each do |k, _v|
|
60
60
|
@results << ExtractValueFromAssociationOfInstance.new(@instance, sym).extract(k, &block)
|
61
61
|
end
|
62
62
|
when Symbol
|
@@ -69,7 +69,7 @@ module Comma
|
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
72
|
-
def __static_column__(
|
72
|
+
def __static_column__(_header = nil, &block)
|
73
73
|
@results << (block ? yield(@instance) : nil)
|
74
74
|
end
|
75
75
|
end
|
@@ -1,5 +1,11 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
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
|
4
10
|
end
|
5
11
|
end
|
data/lib/comma/extractor.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
+
module Comma
|
3
4
|
class Extractor
|
4
|
-
|
5
5
|
def initialize(instance, style, formats)
|
6
6
|
@instance = instance
|
7
7
|
@style = style
|
@@ -10,7 +10,7 @@ module Comma
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def results
|
13
|
-
instance_eval
|
13
|
+
instance_eval(&@formats[@style])
|
14
14
|
@results.map { |r| convert_to_data_value(r) }
|
15
15
|
end
|
16
16
|
|
@@ -28,7 +28,5 @@ module Comma
|
|
28
28
|
def convert_to_data_value(result)
|
29
29
|
result.nil? ? result : result.to_s
|
30
30
|
end
|
31
|
-
|
32
31
|
end
|
33
|
-
|
34
32
|
end
|
data/lib/comma/generator.rb
CHANGED
@@ -1,24 +1,24 @@
|
|
1
|
-
|
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
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
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',
|
19
|
+
CSV_HANDLER.open(@filename, 'w', **@options) { |csv| append_csv(csv, iterator_method) } && (return true)
|
20
20
|
else
|
21
|
-
CSV_HANDLER.generate(
|
21
|
+
CSV_HANDLER.generate(**@options) { |csv| append_csv(csv, iterator_method) }
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
@@ -26,12 +26,12 @@ module Comma
|
|
26
26
|
|
27
27
|
def append_csv(csv, iterator_method)
|
28
28
|
return '' if @instance.empty?
|
29
|
+
|
29
30
|
csv << @instance.first.to_comma_headers(@style) unless
|
30
|
-
@options.
|
31
|
+
@options.key?(:write_headers) && !@options[:write_headers]
|
31
32
|
@instance.send(iterator_method) do |object|
|
32
33
|
csv << object.to_comma(@style)
|
33
34
|
end
|
34
35
|
end
|
35
|
-
|
36
36
|
end
|
37
37
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'comma/extractor'
|
2
4
|
require 'active_support/core_ext/class/attribute'
|
3
5
|
require 'active_support/core_ext/date_time/conversions'
|
@@ -6,44 +8,47 @@ require 'active_support/core_ext/string/inflections'
|
|
6
8
|
|
7
9
|
module Comma
|
8
10
|
class HeaderExtractor < Extractor
|
9
|
-
|
10
11
|
class_attribute :value_humanizer
|
11
12
|
|
12
|
-
DEFAULT_VALUE_HUMANIZER = lambda do |value,
|
13
|
+
DEFAULT_VALUE_HUMANIZER = lambda do |value, _model_class|
|
13
14
|
value.is_a?(String) ? value : value.to_s.humanize
|
14
15
|
end
|
15
16
|
self.value_humanizer = DEFAULT_VALUE_HUMANIZER
|
16
17
|
|
17
|
-
def method_missing(sym, *args, &
|
18
|
+
def method_missing(sym, *args, &_block)
|
18
19
|
model_class = @instance.class
|
19
|
-
@results <<
|
20
|
+
@results << value_humanizer.call(sym, model_class) if args.blank?
|
20
21
|
args.each do |arg|
|
21
22
|
case arg
|
22
23
|
when Hash
|
23
|
-
arg.each do |
|
24
|
-
@results <<
|
24
|
+
arg.each do |_k, v|
|
25
|
+
@results << value_humanizer.call(v, get_association_class(model_class, sym))
|
25
26
|
end
|
26
27
|
when Symbol
|
27
|
-
@results <<
|
28
|
+
@results << value_humanizer.call(arg, get_association_class(model_class, sym))
|
28
29
|
when String
|
29
|
-
@results <<
|
30
|
+
@results << value_humanizer.call(arg, model_class)
|
30
31
|
else
|
31
32
|
raise "Unknown header symbol #{arg.inspect}"
|
32
33
|
end
|
33
34
|
end
|
34
35
|
end
|
35
36
|
|
36
|
-
def __static_column__(header = '', &
|
37
|
+
def __static_column__(header = '', &_block)
|
37
38
|
@results << header
|
38
39
|
end
|
39
40
|
|
40
41
|
private
|
41
42
|
|
42
|
-
def get_association_class(model_class, association)
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
43
|
+
def get_association_class(model_class, association)
|
44
|
+
return unless model_class.respond_to?(:reflect_on_association)
|
45
|
+
|
46
|
+
begin
|
47
|
+
model_class.reflect_on_association(association)&.klass
|
48
|
+
rescue ArgumentError, NameError
|
49
|
+
# Since Rails 5.2, ArgumentError is raised.
|
50
|
+
nil
|
51
|
+
end
|
47
52
|
end
|
48
53
|
end
|
49
54
|
end
|
data/lib/comma/mongoid.rb
CHANGED
@@ -1,12 +1,15 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Conditionally set to_comma on Mongoid records if mongoid gem is installed
|
2
4
|
begin
|
3
5
|
require 'mongoid'
|
4
|
-
|
5
|
-
|
6
|
-
|
6
|
+
|
7
|
+
module Mongoid
|
8
|
+
class Criteria
|
9
|
+
def to_comma(style = :default)
|
10
|
+
Comma::Generator.new(self, style).run(:each)
|
11
|
+
end
|
7
12
|
end
|
8
13
|
end
|
9
|
-
rescue LoadError
|
14
|
+
rescue LoadError # rubocop:disable Lint/HandleExceptions
|
10
15
|
end
|
11
|
-
|
12
|
-
|
data/lib/comma/object.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'comma/data_extractor'
|
2
4
|
require 'comma/header_extractor'
|
3
5
|
|
@@ -31,6 +33,8 @@ class Object
|
|
31
33
|
end
|
32
34
|
|
33
35
|
def raise_unless_style_exists(style)
|
34
|
-
|
36
|
+
return if self.comma_formats && self.comma_formats[style]
|
37
|
+
|
38
|
+
raise "No comma format for class #{self.class} defined for style #{style}"
|
35
39
|
end
|
36
40
|
end
|
data/lib/comma/relation.rb
CHANGED
@@ -1,12 +1,18 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
class Relation
|
5
|
+
def to_comma(style = :default)
|
6
|
+
iterator_method =
|
7
|
+
if arel.ast.limit || !arel.ast.orders.empty?
|
8
|
+
Rails.logger.warn { <<~WARN } if defined?(Rails)
|
9
|
+
#to_comma is being used on a relation with limit or order clauses. Falling back to iterating with :each. This can cause performance issues.
|
10
|
+
WARN
|
11
|
+
:each
|
12
|
+
else
|
13
|
+
:find_each
|
14
|
+
end
|
15
|
+
Comma::Generator.new(self, style).run(iterator_method)
|
16
|
+
end
|
11
17
|
end
|
12
18
|
end
|
data/lib/comma/version.rb
CHANGED
data/spec/comma/comma_spec.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require File.dirname(__FILE__) + '/../spec_helper'
|
2
4
|
|
3
5
|
describe Comma do
|
4
|
-
|
5
6
|
it 'should extend object to add a comma method' do
|
6
7
|
expect(Object).to respond_to(:comma)
|
7
8
|
end
|
@@ -30,7 +31,6 @@ describe Comma do
|
|
30
31
|
end
|
31
32
|
|
32
33
|
describe Comma, 'generating CSV' do # rubocop:disable Metrics/BlockLength
|
33
|
-
|
34
34
|
before do
|
35
35
|
@isbn = Isbn.new('123123123', '321321321')
|
36
36
|
@book = Book.new('Smalltalk-80', 'Language and Implementation', @isbn)
|
@@ -40,53 +40,54 @@ describe Comma, 'generating CSV' do # rubocop:disable Metrics/BlockLength
|
|
40
40
|
end
|
41
41
|
|
42
42
|
it 'should extend Array to add a #to_comma method which will return CSV content for objects within the array' do
|
43
|
-
|
43
|
+
expected = "Title,Description,Issuer,ISBN-10,ISBN-13\nSmalltalk-80,Language and Implementation,ISBN,123123123,321321321\n" # rubocop:disable Metrics/LineLength
|
44
|
+
expect(@books.to_comma).to eq(expected)
|
44
45
|
end
|
45
46
|
|
46
47
|
it 'should return an empty string when generating CSV from an empty array' do
|
47
|
-
expect(
|
48
|
+
expect([].to_comma).to eq('')
|
48
49
|
end
|
49
50
|
|
50
|
-
it
|
51
|
+
it 'should change the style when specified' do
|
51
52
|
expect(@books.to_comma(:brief)).to eq("Name,Description\nSmalltalk-80,Language and Implementation\n")
|
52
53
|
end
|
53
54
|
|
54
55
|
describe 'with :filename specified' do
|
55
|
-
after{ File.delete('comma.csv') }
|
56
|
+
after { File.delete('comma.csv') }
|
56
57
|
|
57
|
-
it
|
58
|
-
@books.to_comma(:
|
59
|
-
|
58
|
+
it 'should write to the file' do
|
59
|
+
@books.to_comma(filename: 'comma.csv')
|
60
|
+
expected = "Title,Description,Issuer,ISBN-10,ISBN-13\nSmalltalk-80,Language and Implementation,ISBN,123123123,321321321\n" # rubocop:disable Metrics/LineLength
|
61
|
+
expect(File.read('comma.csv')).to eq(expected)
|
60
62
|
end
|
61
63
|
|
62
|
-
it
|
63
|
-
@books.to_comma(:
|
64
|
-
|
64
|
+
it 'should accept FasterCSV options' do
|
65
|
+
@books.to_comma(filename: 'comma.csv', col_sep: ';', force_quotes: true)
|
66
|
+
expected = "\"Title\";\"Description\";\"Issuer\";\"ISBN-10\";\"ISBN-13\"\n\"Smalltalk-80\";\"Language and Implementation\";\"ISBN\";\"123123123\";\"321321321\"\n" # rubocop:disable Metrics/LineLength
|
67
|
+
expect(File.read('comma.csv')).to eq(expected)
|
65
68
|
end
|
66
|
-
|
67
69
|
end
|
68
70
|
|
69
|
-
describe
|
70
|
-
it
|
71
|
-
|
71
|
+
describe 'with FasterCSV options' do
|
72
|
+
it 'should not change when options are empty' do
|
73
|
+
expected = "Title,Description,Issuer,ISBN-10,ISBN-13\nSmalltalk-80,Language and Implementation,ISBN,123123123,321321321\n" # rubocop:disable Metrics/LineLength
|
74
|
+
expect(@books.to_comma({})).to eq(expected)
|
72
75
|
end
|
73
76
|
|
74
77
|
it 'should accept the options in #to_comma and generate the appropriate CSV' do
|
75
|
-
|
76
|
-
|
78
|
+
expected = "\"Title\";\"Description\";\"Issuer\";\"ISBN-10\";\"ISBN-13\"\n\"Smalltalk-80\";\"Language and Implementation\";\"ISBN\";\"123123123\";\"321321321\"\n" # rubocop:disable Metrics/LineLength
|
79
|
+
expect(@books.to_comma(col_sep: ';', force_quotes: true)).to eq(expected)
|
77
80
|
end
|
78
81
|
|
79
|
-
it
|
80
|
-
expect(@books.to_comma(:
|
82
|
+
it 'should change the style when specified' do
|
83
|
+
expect(@books.to_comma(style: :brief, col_sep: ';', force_quotes: true))
|
81
84
|
.to eq("\"Name\";\"Description\"\n\"Smalltalk-80\";\"Language and Implementation\"\n")
|
82
85
|
end
|
83
86
|
end
|
84
87
|
end
|
85
88
|
|
86
89
|
describe Comma, 'defining CSV descriptions' do
|
87
|
-
|
88
90
|
describe 'with an unnamed description' do
|
89
|
-
|
90
91
|
before do
|
91
92
|
class Foo
|
92
93
|
comma do; end
|
@@ -100,7 +101,6 @@ describe Comma, 'defining CSV descriptions' do
|
|
100
101
|
end
|
101
102
|
|
102
103
|
describe 'with a named description' do
|
103
|
-
|
104
104
|
before do
|
105
105
|
class Bar
|
106
106
|
comma do; end
|
@@ -117,9 +117,7 @@ describe Comma, 'defining CSV descriptions' do
|
|
117
117
|
end
|
118
118
|
|
119
119
|
describe Comma, 'to_comma data/headers object extensions' do # rubocop:disable Metrics/BlockLength
|
120
|
-
|
121
120
|
describe 'with unnamed descriptions' do
|
122
|
-
|
123
121
|
before do
|
124
122
|
class Foo
|
125
123
|
attr_accessor :content
|
@@ -144,11 +142,9 @@ describe Comma, 'to_comma data/headers object extensions' do # rubocop:disable M
|
|
144
142
|
it 'should return the CSV representation including header and content when called on an array' do
|
145
143
|
expect(Array(@foo).to_comma).to eq("Content\ncontent\n")
|
146
144
|
end
|
147
|
-
|
148
145
|
end
|
149
146
|
|
150
147
|
describe 'with named descriptions' do
|
151
|
-
|
152
148
|
before do
|
153
149
|
class Foo
|
154
150
|
attr_accessor :content
|
@@ -175,11 +171,10 @@ describe Comma, 'to_comma data/headers object extensions' do # rubocop:disable M
|
|
175
171
|
end
|
176
172
|
|
177
173
|
it 'should raise an error if the requested description is not avaliable' do
|
178
|
-
expect { @foo.to_comma(:bad) }.to raise_error
|
179
|
-
expect { @foo.to_comma_headers(:bad) }.to raise_error
|
180
|
-
expect { Array(@foo).to_comma(:bad) }.to raise_error
|
174
|
+
expect { @foo.to_comma(:bad) }.to raise_error(RuntimeError)
|
175
|
+
expect { @foo.to_comma_headers(:bad) }.to raise_error(RuntimeError)
|
176
|
+
expect { Array(@foo).to_comma(:bad) }.to raise_error(RuntimeError)
|
181
177
|
end
|
182
|
-
|
183
178
|
end
|
184
179
|
|
185
180
|
describe 'with block' do # rubocop:disable BlockLength
|
@@ -188,11 +183,11 @@ describe Comma, 'to_comma data/headers object extensions' do # rubocop:disable M
|
|
188
183
|
attr_accessor :content, :created_at, :updated_at
|
189
184
|
comma do
|
190
185
|
content
|
191
|
-
content('Truncated Content') {|i| i && i.length > 10 ? i[0..10] : '---' }
|
192
|
-
created_at { |i| i
|
193
|
-
updated_at { |i| i
|
194
|
-
created_at 'Created Custom Label' do |i| i
|
195
|
-
updated_at 'Updated at Custom Label' do |i| i
|
186
|
+
content('Truncated Content') { |i| i && i.length > 10 ? i[0..10] : '---' }
|
187
|
+
created_at { |i| i&.to_s(:db) }
|
188
|
+
updated_at { |i| i&.to_s(:db) }
|
189
|
+
created_at 'Created Custom Label' do |i| i&.to_s(:short) end
|
190
|
+
updated_at 'Updated at Custom Label' do |i| i&.to_s(:short) end
|
196
191
|
end
|
197
192
|
|
198
193
|
def initialize(content, created_at = Time.now, updated_at = Time.now)
|
@@ -209,38 +204,66 @@ describe Comma, 'to_comma data/headers object extensions' do # rubocop:disable M
|
|
209
204
|
|
210
205
|
it 'should return yielded values by block' do
|
211
206
|
_header, foo = Array(@foo).to_comma.split("\n")
|
212
|
-
|
207
|
+
expected = [
|
208
|
+
@content,
|
209
|
+
@content[0..10],
|
210
|
+
@time.to_s(:db),
|
211
|
+
@time.to_s(:db),
|
212
|
+
@time.to_s(:short),
|
213
|
+
@time.to_s(:short)
|
214
|
+
].join(',')
|
215
|
+
expect(foo).to eq(expected)
|
213
216
|
end
|
214
217
|
|
215
218
|
it 'should return headers with custom labels from block' do
|
216
219
|
header, _foo = Array(@foo).to_comma.split("\n")
|
217
|
-
|
220
|
+
expected = [
|
221
|
+
'Content',
|
222
|
+
'Truncated Content',
|
223
|
+
'Created at',
|
224
|
+
'Updated at',
|
225
|
+
'Created Custom Label',
|
226
|
+
'Updated at Custom Label'
|
227
|
+
].join(',')
|
228
|
+
expect(header).to eq(expected)
|
218
229
|
end
|
219
230
|
|
220
231
|
it 'should put headers in place when forced' do
|
221
|
-
header, _foo = Array(@foo).to_comma(:
|
222
|
-
|
232
|
+
header, _foo = Array(@foo).to_comma(write_headers: true).split("\n")
|
233
|
+
expected = [
|
234
|
+
'Content',
|
235
|
+
'Truncated Content',
|
236
|
+
'Created at',
|
237
|
+
'Updated at',
|
238
|
+
'Created Custom Label',
|
239
|
+
'Updated at Custom Label'
|
240
|
+
].join(',')
|
241
|
+
expect(header).to eq(expected)
|
223
242
|
end
|
224
243
|
|
225
244
|
it 'should not write headers if specified' do
|
226
|
-
header, _foo = Array(@foo).to_comma(:
|
227
|
-
|
245
|
+
header, _foo = Array(@foo).to_comma(write_headers: false).split("\n")
|
246
|
+
expected = [
|
247
|
+
@content,
|
248
|
+
@content[0..10],
|
249
|
+
@time.to_s(:db),
|
250
|
+
@time.to_s(:db),
|
251
|
+
@time.to_s(:short),
|
252
|
+
@time.to_s(:short)
|
253
|
+
].join(',')
|
254
|
+
expect(header).to eq(expected)
|
228
255
|
end
|
229
|
-
|
230
256
|
end
|
231
257
|
|
232
|
-
|
233
258
|
describe 'on an object with no comma declaration' do
|
234
|
-
|
235
259
|
it 'should raise an error mentioning there is no comma description defined for that class' do
|
236
260
|
expect { 'a string'.to_comma }.to raise_error('No comma format for class String defined for style default')
|
237
|
-
expect { 'a string'.to_comma_headers }
|
261
|
+
expect { 'a string'.to_comma_headers }
|
262
|
+
.to raise_error('No comma format for class String defined for style default')
|
238
263
|
end
|
239
|
-
|
240
264
|
end
|
241
265
|
|
242
266
|
describe 'on objects using Single Table Inheritance' do
|
243
|
-
|
244
267
|
before do
|
245
268
|
class MySuperClass
|
246
269
|
attr_accessor :content
|
@@ -273,9 +296,7 @@ describe Comma, 'to_comma data/headers object extensions' do # rubocop:disable M
|
|
273
296
|
it 'should return and array of data content, as defined in comma block in super class, if not present in child' do
|
274
297
|
expect(@childNoComma.to_comma).to eq(%w[super-content])
|
275
298
|
end
|
276
|
-
|
277
299
|
end
|
278
|
-
|
279
300
|
end
|
280
301
|
|
281
302
|
describe Comma, '__use__ keyword' do
|