caxlsx_builder 1.0.0 → 1.1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 166ae65f2879639670efc80db83d33e769008aff2445b8f17a201ed859e739b0
4
- data.tar.gz: 22925363722171bdf7141d22e8f964c2f5a28344dff5b18bec7ea7f69942a026
3
+ metadata.gz: 99c1449eab24e473b70cfb60c6fb9ffb8750bcfd3331bd4e1bc4d11fa172c5ea
4
+ data.tar.gz: d5a2146247674eb9bd7abb422a0e5635997759ec205619e1518a6e322af1c535
5
5
  SHA512:
6
- metadata.gz: 94cf4c88f0c77fabc79e2e2699d7a1d2898abd1008901771fecb1d197e29692c9b473f3adf9775771c54e110d5075b07dbdfcfe8bcbff7ca6d73ec3f57557aa7
7
- data.tar.gz: bc70d29e37edc0a7d289e617238ef6785aa94484c969a166426ab47ba7a378f10cb00d0ee17e3534aeb542b7dd7ee8f3cf0d4d69fd5d9d0761289ceda12edcb9
6
+ metadata.gz: 6018bbabd3db00124d3657aee635c6c082c313d321c38345b6322e9ebad534318b73331f897508b1b2863a9933e50ae3b70d1cc2ef0f83783815411f1502c1c3
7
+ data.tar.gz: 0fe4e1c53f7d85a28cb00e7804f77310971d8fae32994a84a590de651283d20f45b32601bb11be2ef252c930fe55cbfef5945582ef995f4c5519295cf9e8d48b
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## [1.1.1] - 2022-12-26
2
+
3
+ - Fix 'rescue_errors' option
4
+
5
+ ## [1.1.0] - 2022-12-26
6
+
7
+ - Allow to rescue errors when calling procs to generate cell, style, type or footer
8
+
1
9
  ## [1.0.0] - 2022-12-26
2
10
 
3
11
  - Initial release
@@ -7,12 +7,11 @@ module CaxlsxBuilder
7
7
  CELL_TYPES = %i[date time float integer richtext string boolean iso_8601 text].freeze
8
8
  # rubocop:enable Naming/VariableNumber
9
9
 
10
- def initialize(sheets, &builder)
10
+ def initialize(sheets, options: { rescue_errors: false }, &builder)
11
11
  @sheets = sheets
12
12
  @builder = builder
13
+ @options = options || { rescue_errors: false }
13
14
  @styles = {}
14
-
15
- # raise ArgumentError, "`sheets` must be a Hash[String, Array[untyped]]"
16
15
  end
17
16
 
18
17
  # @return [Axlsx::Package]
@@ -104,7 +103,7 @@ module CaxlsxBuilder
104
103
  # @param worksheet [Axlsx::Worksheet]
105
104
  def add_data_rows(collection, sheet, worksheet)
106
105
  collection.each do |item|
107
- row = sheet.make_row(item)
106
+ row = sheet.make_row(item, rescue_errors: @options[:rescue_errors])
108
107
 
109
108
  # Nil row or empty row means no row
110
109
  compacted_row = row&.compact
@@ -113,7 +112,9 @@ module CaxlsxBuilder
113
112
  # Cells options returns an array which is logic: one option per sheet cell
114
113
  # But `sheet.add_row` does not handle arrays correctly, so I transform it to a valid
115
114
  # Hash using `cast_to_hash`.
116
- cells = sheet.cell_styles.map { |cell| cell_options(**cell.as_style(item)) }
115
+ cells = sheet.cell_styles.map do |cell|
116
+ cell_options(**cell.as_style(item, rescue_errors: @options[:rescue_errors]))
117
+ end
117
118
  options = cast_to_hash(cells)
118
119
  worksheet.add_row(row, options)
119
120
  end
@@ -124,18 +125,24 @@ module CaxlsxBuilder
124
125
 
125
126
  footer = sheet.footers.map.with_index do |cell, index|
126
127
  if cell.respond_to?(:call)
127
- cell.call(sheet.cells(index))
128
+ call_footer_proc(cell, sheet.cells(index))
128
129
  else
129
130
  cell
130
131
  end
131
- rescue StandardError
132
- nil
133
132
  end
134
133
 
135
134
  # Last row is the footer with the footer style applied
136
135
  worksheet.add_row(footer, style: @styles[:footer])
137
136
  end
138
137
 
138
+ def call_footer_proc(cell, cells)
139
+ cell.call(cells)
140
+ rescue StandardError => e
141
+ return nil if @options[:rescue_errors]
142
+
143
+ raise e
144
+ end
145
+
139
146
  # @return [Hash] The options to apply to a cell
140
147
  def cell_options(style: nil, type: nil)
141
148
  style = get_style(style)
@@ -7,13 +7,35 @@ module CaxlsxBuilder
7
7
  @type = type
8
8
  end
9
9
 
10
- def as_style(item)
11
- style = @style.respond_to?(:call) ? @style.call(item) : @style
12
- type = @type.respond_to?(:call) ? @type.call(item) : @type
10
+ def as_style(item, rescue_errors: false)
11
+ style = @style.respond_to?(:call) ? call_style_proc(item, rescue_errors:) : @style
12
+ type = @type.respond_to?(:call) ? call_type_proc(item, rescue_errors:) : @type
13
13
 
14
14
  { style:, type: }
15
15
  end
16
16
 
17
+ private
18
+
19
+ def call_style_proc(item, rescue_errors: false)
20
+ @style.call(item)
21
+ rescue StandardError => e
22
+ if rescue_errors
23
+ :default
24
+ else
25
+ raise e
26
+ end
27
+ end
28
+
29
+ def call_type_proc(item, rescue_errors: false)
30
+ @type.call(item)
31
+ rescue StandardError => e
32
+ if rescue_errors
33
+ :string
34
+ else
35
+ raise e
36
+ end
37
+ end
38
+
17
39
  end
18
40
 
19
41
  end
@@ -25,12 +25,16 @@ module CaxlsxBuilder
25
25
  @styles[name] = style
26
26
  end
27
27
 
28
- def make_row(item)
28
+ def make_row(item, rescue_errors: false)
29
29
  # Build the new row
30
30
  row = @cell_builders.map do |cell|
31
- cell.call(item)
32
- rescue StandardError
33
- nil
31
+ begin
32
+ cell.call(item)
33
+ rescue StandardError => e
34
+ next nil if rescue_errors
35
+
36
+ raise e
37
+ end
34
38
  end
35
39
 
36
40
  # Add the new row to the cache then return it
@@ -2,6 +2,6 @@
2
2
 
3
3
  module CaxlsxBuilder
4
4
 
5
- VERSION = '1.0.0'
5
+ VERSION = '1.1.1'
6
6
 
7
7
  end
@@ -16,7 +16,7 @@ module CaxlsxBuilder
16
16
  class Builder
17
17
  CELL_TYPES: Array[Cell::type_value]
18
18
 
19
- def initialize: (Hash[String, Array[untyped]] sheets) { (Sheet) -> void } -> void
19
+ def initialize: (Hash[String, Array[untyped]] sheets, options: { rescue_errors: bool }) { (Sheet) -> void } -> void
20
20
 
21
21
  def call: () -> Axlsx::Package
22
22
 
@@ -34,6 +34,8 @@ module CaxlsxBuilder
34
34
 
35
35
  def add_footer_row: (Sheet sheet, Axlsx::Worksheet worksheet) -> void
36
36
 
37
+ def call_footer_proc: (Sheet::footer_proc cell, Array[Sheet::cell_value]? cells) -> Sheet::cell_value?
38
+
37
39
  def cell_options: (style: String? | Symbol?, type: Cell::type_value?) -> { style: Integer, type: Cell::type_value }
38
40
 
39
41
  def cast_to_hash: (Array[{ style: Integer, type: Cell::type_value }] | { style: Array[Integer], types: Array[Cell::type_value] }) -> { style: Array[Integer], types: Array[Cell::type_value] }
@@ -9,6 +9,11 @@ module CaxlsxBuilder
9
9
 
10
10
  def initialize: (?style: Symbol | style_proc, ?type: type_value | type_proc) -> void
11
11
 
12
- def as_style: (untyped item) -> ({ style: Symbol, type: type_value })
12
+ def as_style: (untyped item, rescue_errors: bool) -> ({ style: Symbol, type: type_value })
13
+
14
+ private
15
+
16
+ def call_style_proc: (untyped item, rescue_errors: bool) -> Symbol
17
+ def call_type_proc: (untyped item, rescue_errors: bool) -> type_value
13
18
  end
14
19
  end
@@ -16,7 +16,7 @@ module CaxlsxBuilder
16
16
 
17
17
  def add_style: (String | Symbol name, Hash[Symbol, untyped] style) -> void
18
18
 
19
- def make_row: (untyped item) -> Array[cell_value]?
19
+ def make_row: (untyped item, rescue_errors: bool) -> Array[cell_value]?
20
20
 
21
21
  def cells: (Integer index) -> Array[cell_value]?
22
22
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: caxlsx_builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthieu CIAPPARA