csb 0.7.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6cf72524c9c57bb6b6b8552ff67f34542f791bae8f1e395464a8d887157480ee
4
- data.tar.gz: 3450353def85946d04c04e238082200401b1bf2ee1010fc4ebb3f6478bbdfac1
3
+ metadata.gz: 838fb73c6e13f3232f1fac402667ff00e92ac6f7f814d8605d2b850f52e449cc
4
+ data.tar.gz: 8c0016c9bff8210a55882ba0afa442d5fd9efca701800cc031a13ecfbe6ef21b
5
5
  SHA512:
6
- metadata.gz: 570eb119ec084220bb78abfcc11dd99dfd7427904593b4c229e1e0d825c6b0b69dfdb4053cdaec18725b0ef4569764ca02b90c3d61e342501f52f3bcb44c91f6
7
- data.tar.gz: 1410d02f0dcfd112fc0799e1a2fdfde1edc44aa7a6c4f4bbe64fc7aea171bffc0a55fbea09dc6ff4999712c64d4586a5693c9fa1f1e2451dded04f863326358a
6
+ metadata.gz: 679a875a5174204aa3cfdc5713083c5a25be1c704049ff63a84e5969e9ecc6a582804d15177a3febd86fbb58e614772556d78730df10f3bef266a14644e968e4
7
+ data.tar.gz: abfb009c641e057a4d6b35992e409d9de8329d598dc0c61a2689a0cbef1564b247e813d1bf740d4572dedb38557bbd82f73f47b934ccf0fd82c2dbdd10424bcc
@@ -15,7 +15,7 @@ jobs:
15
15
  strategy:
16
16
  fail-fast: false
17
17
  matrix:
18
- ruby: ["3.0", "3.1", "3.2"]
18
+ ruby: ["3.0", "3.1", "3.2", "3.3"]
19
19
  gemfile: ["rails61", "rails70"]
20
20
  exclude:
21
21
  - ruby: "3.1"
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## 0.9.0
2
+
3
+ - Fixing Issue with config.streaming Enabled in Rails 7.1 (#17)
4
+
5
+ ## 0.8.0
6
+
7
+ - Add `csv_options` as an option to pass to `CSV.generate_line` (https://github.com/aki77/csb/pull/18)
8
+
1
9
  ## 0.7.0
2
10
 
3
11
  - Drop support for ruby 2.7
data/README.md CHANGED
@@ -37,6 +37,7 @@ csv.items = @reports
37
37
 
38
38
  # csv.filename = "reports_#{Time.current.to_i}.csv"
39
39
  # csv.streaming = false
40
+ # csv.csv_options = { col_sep: "\t" }
40
41
 
41
42
  csv.cols.add('Update date') { |r| l(r.updated_at.to_date) }
42
43
  csv.cols.add('Categories') { |r| r.categories.pluck(:name).join(' ') }
@@ -130,6 +131,7 @@ In `config/initializers/csb.rb`, you can configure the following values.
130
131
  Csb.configure do |config|
131
132
  config.utf8_bom = true # default: false
132
133
  config.streaming = false # default: true
134
+ config.csv_options = { col_sep: "\t" } # default: {}
133
135
  config.after_streaming_error = ->(error) do # default: nil
134
136
  Rails.logger.error(error)
135
137
  Bugsnag.notify(error)
data/lib/csb/builder.rb CHANGED
@@ -5,21 +5,22 @@ module Csb
5
5
  class Builder
6
6
  UTF8_BOM = "\xEF\xBB\xBF".freeze
7
7
 
8
- attr_reader :output, :utf8_bom, :items, :cols
8
+ attr_reader :output, :utf8_bom, :items, :cols, :csv_options
9
9
  attr_accessor :items
10
10
 
11
- def initialize(output = '', items: [], utf8_bom: false)
11
+ def initialize(output = '', items: [], utf8_bom: false, csv_options: {})
12
12
  @output = output
13
13
  @utf8_bom = utf8_bom
14
14
  @cols = Cols.new
15
15
  @items = items
16
+ @csv_options = csv_options
16
17
  end
17
18
 
18
19
  def build
19
20
  output << UTF8_BOM if utf8_bom
20
- output << CSV.generate_line(cols.headers)
21
+ output << CSV.generate_line(cols.headers, **csv_options)
21
22
  items.each do |item|
22
- output << CSV.generate_line(cols.values_by_item(item))
23
+ output << CSV.generate_line(cols.values_by_item(item), **csv_options)
23
24
  rescue => error
24
25
  break if Csb.configuration.ignore_class_names.include?(error.class.name)
25
26
 
@@ -1,10 +1,11 @@
1
1
  module Csb
2
2
  class Configuration
3
- attr_accessor :utf8_bom, :streaming, :after_streaming_error, :ignore_class_names
3
+ attr_accessor :utf8_bom, :streaming, :after_streaming_error, :ignore_class_names, :csv_options
4
4
 
5
5
  def initialize
6
6
  @utf8_bom = false
7
7
  @streaming = true
8
+ @csv_options = {}
8
9
  @ignore_class_names = %w[Puma::ConnectionError]
9
10
  end
10
11
  end
data/lib/csb/handler.rb CHANGED
@@ -14,6 +14,7 @@ module Csb
14
14
  csv = ::Csb::Template.new(
15
15
  utf8_bom: ::Csb.configuration.utf8_bom,
16
16
  streaming: ::Csb.configuration.streaming,
17
+ csv_options: ::Csb.configuration.csv_options,
17
18
  )
18
19
  #{source}
19
20
  controller.send(:send_file_headers!, type: 'text/csv', filename: csv.filename)
data/lib/csb/railtie.rb CHANGED
@@ -6,6 +6,34 @@ module Csb
6
6
  ActiveSupport.on_load :action_view do
7
7
  require 'csb/handler'
8
8
  ActionView::Template.register_template_handler :csb, Csb::Handler
9
+
10
+ # SEE: https://github.com/rails/rails/pull/51023
11
+ rails_version = Gem::Version.new(Rails.version)
12
+ if rails_version >= Gem::Version.new('7.1.0') && rails_version < Gem::Version.new('7.1.4')
13
+ ActionView::Template.prepend(Module.new do
14
+ # SEE: https://github.com/Shopify/rails/blob/0601929486398954a17b1985fcf7f9f0611d2d55/actionview/lib/action_view/template.rb#L262C5-L281C8
15
+ def render(view, locals, buffer = nil, implicit_locals: [], add_to_stack: true, &block)
16
+ instrument_render_template do
17
+ compile!(view)
18
+
19
+ if strict_locals? && @strict_local_keys && !implicit_locals.empty?
20
+ locals_to_ignore = implicit_locals - @strict_local_keys
21
+ locals.except!(*locals_to_ignore)
22
+ end
23
+
24
+ if buffer
25
+ view._run(method_name, self, locals, buffer, add_to_stack: add_to_stack, has_strict_locals: strict_locals?, &block)
26
+ nil
27
+ else
28
+ result = view._run(method_name, self, locals, ActionView::OutputBuffer.new, add_to_stack: add_to_stack, has_strict_locals: strict_locals?, &block)
29
+ result.is_a?(ActionView::OutputBuffer) ? result.to_s : result
30
+ end
31
+ end
32
+ rescue => e
33
+ handle_render_error(view, e)
34
+ end
35
+ end)
36
+ end
9
37
  end
10
38
  end
11
39
  end
data/lib/csb/template.rb CHANGED
@@ -1,10 +1,11 @@
1
1
  module Csb
2
2
  class Template
3
- attr_accessor :utf8_bom, :filename, :streaming, :items, :cols
3
+ attr_accessor :utf8_bom, :filename, :streaming, :items, :cols, :csv_options
4
4
 
5
- def initialize(utf8_bom:, streaming:)
5
+ def initialize(utf8_bom:, streaming:, csv_options:)
6
6
  @utf8_bom = utf8_bom
7
7
  @streaming = streaming
8
+ @csv_options = csv_options
8
9
  @cols = Cols.new
9
10
  @items = []
10
11
  end
@@ -20,7 +21,7 @@ module Csb
20
21
  private
21
22
 
22
23
  def build_string
23
- builder = Builder.new(utf8_bom: utf8_bom, items: items)
24
+ builder = Builder.new(utf8_bom: utf8_bom, items: items, csv_options: csv_options)
24
25
  builder.cols.copy!(cols)
25
26
  builder.build
26
27
  end
@@ -28,7 +29,7 @@ module Csb
28
29
  def build_enumerator
29
30
  Enumerator.new do |y|
30
31
  begin
31
- builder = Builder.new(y, utf8_bom: utf8_bom, items: items)
32
+ builder = Builder.new(y, utf8_bom: utf8_bom, items: items, csv_options: csv_options)
32
33
  builder.cols.copy!(cols)
33
34
  builder.build
34
35
  rescue => error
data/lib/csb/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Csb
2
- VERSION = '0.7.0'
2
+ VERSION = '0.9.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - aki77
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-05-13 00:00:00.000000000 Z
11
+ date: 2024-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails