thinreports 0.9.1 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (179) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +41 -0
  3. data/.travis.yml +4 -6
  4. data/CHANGELOG.md +21 -2
  5. data/Gemfile +2 -4
  6. data/README.md +4 -5
  7. data/Rakefile +7 -8
  8. data/examples/character_spacing/character_spacing.rb +1 -1
  9. data/examples/dynamic_image/dynamic_image.rb +3 -3
  10. data/examples/dynamic_style/dynamic_style.rb +1 -1
  11. data/examples/eudc/eudc.rb +1 -1
  12. data/examples/helper.rb +2 -2
  13. data/examples/hidden_shapes/hidden_shapes.rb +1 -1
  14. data/examples/list_events/list_events.rb +1 -1
  15. data/examples/list_manual_generation/list_manual_generation.rb +1 -1
  16. data/examples/list_page_number/list_page_number.rb +1 -1
  17. data/examples/page_number/page_number.rb +1 -1
  18. data/examples/palleted_png/palleted_png.rb +1 -3
  19. data/examples/password_setting/password_setting.rb +1 -1
  20. data/examples/report_callbacks/report_callbacks.rb +1 -1
  21. data/examples/single_line_tblock/single_line_tblock.rb +1 -1
  22. data/examples/tblock_overflow/tblock_overflow.rb +1 -1
  23. data/examples/tblock_styles/tblock_styles.rb +1 -1
  24. data/examples/text_align/text_align.rb +1 -1
  25. data/examples/typeB_page_size/typeB_page_size.rb +1 -1
  26. data/examples/word_wrap/word_wrap.rb +1 -1
  27. data/lib/thinreports.rb +16 -15
  28. data/lib/thinreports/config.rb +2 -24
  29. data/lib/thinreports/core/errors.rb +6 -17
  30. data/lib/thinreports/core/format/base.rb +62 -61
  31. data/lib/thinreports/core/shape.rb +13 -14
  32. data/lib/thinreports/core/shape/base.rb +3 -12
  33. data/lib/thinreports/core/shape/base/interface.rb +35 -32
  34. data/lib/thinreports/core/shape/base/internal.rb +45 -43
  35. data/lib/thinreports/core/shape/basic.rb +12 -12
  36. data/lib/thinreports/core/shape/basic/block_format.rb +8 -6
  37. data/lib/thinreports/core/shape/basic/block_interface.rb +24 -22
  38. data/lib/thinreports/core/shape/basic/block_internal.rb +28 -26
  39. data/lib/thinreports/core/shape/basic/format.rb +11 -9
  40. data/lib/thinreports/core/shape/basic/interface.rb +79 -77
  41. data/lib/thinreports/core/shape/basic/internal.rb +17 -14
  42. data/lib/thinreports/core/shape/image_block.rb +9 -9
  43. data/lib/thinreports/core/shape/image_block/format.rb +7 -5
  44. data/lib/thinreports/core/shape/image_block/interface.rb +15 -13
  45. data/lib/thinreports/core/shape/image_block/internal.rb +10 -8
  46. data/lib/thinreports/core/shape/list.rb +13 -13
  47. data/lib/thinreports/core/shape/list/format.rb +83 -77
  48. data/lib/thinreports/core/shape/list/manager.rb +182 -176
  49. data/lib/thinreports/core/shape/list/page.rb +94 -93
  50. data/lib/thinreports/core/shape/list/page_state.rb +42 -40
  51. data/lib/thinreports/core/shape/list/section_format.rb +28 -26
  52. data/lib/thinreports/core/shape/list/section_interface.rb +42 -40
  53. data/lib/thinreports/core/shape/list/section_internal.rb +21 -19
  54. data/lib/thinreports/core/shape/manager.rb +4 -13
  55. data/lib/thinreports/core/shape/manager/format.rb +21 -19
  56. data/lib/thinreports/core/shape/manager/internal.rb +88 -84
  57. data/lib/thinreports/core/shape/manager/target.rb +93 -95
  58. data/lib/thinreports/core/shape/page_number.rb +9 -9
  59. data/lib/thinreports/core/shape/page_number/format.rb +20 -18
  60. data/lib/thinreports/core/shape/page_number/interface.rb +20 -18
  61. data/lib/thinreports/core/shape/page_number/internal.rb +47 -44
  62. data/lib/thinreports/core/shape/style.rb +5 -14
  63. data/lib/thinreports/core/shape/style/base.rb +121 -120
  64. data/lib/thinreports/core/shape/style/basic.rb +12 -10
  65. data/lib/thinreports/core/shape/style/graphic.rb +35 -33
  66. data/lib/thinreports/core/shape/style/text.rb +113 -107
  67. data/lib/thinreports/core/shape/text.rb +9 -9
  68. data/lib/thinreports/core/shape/text/format.rb +10 -9
  69. data/lib/thinreports/core/shape/text/interface.rb +13 -11
  70. data/lib/thinreports/core/shape/text/internal.rb +14 -12
  71. data/lib/thinreports/core/shape/text_block.rb +10 -10
  72. data/lib/thinreports/core/shape/text_block/format.rb +35 -33
  73. data/lib/thinreports/core/shape/text_block/formatter.rb +26 -24
  74. data/lib/thinreports/core/shape/text_block/formatter/basic.rb +25 -25
  75. data/lib/thinreports/core/shape/text_block/formatter/datetime.rb +15 -12
  76. data/lib/thinreports/core/shape/text_block/formatter/number.rb +45 -39
  77. data/lib/thinreports/core/shape/text_block/formatter/padding.rb +17 -14
  78. data/lib/thinreports/core/shape/text_block/interface.rb +25 -23
  79. data/lib/thinreports/core/shape/text_block/internal.rb +68 -68
  80. data/lib/thinreports/core/utils.rb +4 -3
  81. data/lib/thinreports/generator/pdf.rb +24 -25
  82. data/lib/thinreports/generator/pdf/document.rb +109 -110
  83. data/lib/thinreports/generator/pdf/document/draw_shape.rb +58 -51
  84. data/lib/thinreports/generator/pdf/document/draw_template_items.rb +51 -47
  85. data/lib/thinreports/generator/pdf/document/font.rb +62 -59
  86. data/lib/thinreports/generator/pdf/document/graphics.rb +32 -33
  87. data/lib/thinreports/generator/pdf/document/graphics/attributes.rb +122 -121
  88. data/lib/thinreports/generator/pdf/document/graphics/basic.rb +113 -114
  89. data/lib/thinreports/generator/pdf/document/graphics/image.rb +58 -150
  90. data/lib/thinreports/generator/pdf/document/graphics/text.rb +152 -137
  91. data/lib/thinreports/generator/pdf/document/page.rb +63 -64
  92. data/lib/thinreports/generator/pdf/document/parse_color.rb +37 -37
  93. data/lib/thinreports/generator/pdf/drawer/base.rb +41 -41
  94. data/lib/thinreports/generator/pdf/drawer/list.rb +48 -45
  95. data/lib/thinreports/generator/pdf/drawer/list_section.rb +42 -40
  96. data/lib/thinreports/generator/pdf/drawer/page.rb +101 -96
  97. data/lib/thinreports/generator/pdf/prawn_ext.rb +4 -30
  98. data/lib/thinreports/generator/pdf/prawn_ext/calc_image_dimensions.rb +34 -0
  99. data/lib/thinreports/generator/pdf/prawn_ext/width_of.rb +29 -0
  100. data/lib/thinreports/layout.rb +5 -7
  101. data/lib/thinreports/layout/base.rb +5 -7
  102. data/lib/thinreports/layout/format.rb +18 -13
  103. data/lib/thinreports/layout/legacy_schema.rb +18 -10
  104. data/lib/thinreports/layout/version.rb +22 -25
  105. data/lib/thinreports/report.rb +4 -6
  106. data/lib/thinreports/report/base.rb +24 -51
  107. data/lib/thinreports/report/internal.rb +9 -11
  108. data/lib/thinreports/report/page.rb +5 -6
  109. data/lib/thinreports/version.rb +2 -2
  110. data/test/schema_helper.rb +4 -3
  111. data/test/test_helper.rb +9 -3
  112. data/test/unit/core/format/test_base.rb +1 -1
  113. data/test/unit/core/shape/base/test_internal.rb +1 -1
  114. data/test/unit/core/shape/basic/test_block_format.rb +1 -1
  115. data/test/unit/core/shape/basic/test_block_interface.rb +1 -1
  116. data/test/unit/core/shape/basic/test_block_internal.rb +1 -1
  117. data/test/unit/core/shape/basic/test_format.rb +1 -1
  118. data/test/unit/core/shape/basic/test_interface.rb +1 -1
  119. data/test/unit/core/shape/basic/test_internal.rb +1 -1
  120. data/test/unit/core/shape/image_block/test_interface.rb +1 -1
  121. data/test/unit/core/shape/image_block/test_internal.rb +1 -1
  122. data/test/unit/core/shape/list/test_format.rb +1 -1
  123. data/test/unit/core/shape/list/test_manager.rb +4 -4
  124. data/test/unit/core/shape/list/test_page.rb +1 -1
  125. data/test/unit/core/shape/list/test_page_state.rb +1 -1
  126. data/test/unit/core/shape/list/test_section_format.rb +1 -1
  127. data/test/unit/core/shape/list/test_section_interface.rb +2 -2
  128. data/test/unit/core/shape/list/test_section_internal.rb +1 -1
  129. data/test/unit/core/shape/manager/test_format.rb +2 -2
  130. data/test/unit/core/shape/manager/test_internal.rb +3 -3
  131. data/test/unit/core/shape/manager/test_target.rb +1 -1
  132. data/test/unit/core/shape/page_number/test_format.rb +1 -1
  133. data/test/unit/core/shape/page_number/test_interface.rb +1 -1
  134. data/test/unit/core/shape/page_number/test_internal.rb +1 -1
  135. data/test/unit/core/shape/styles/test_base.rb +1 -1
  136. data/test/unit/core/shape/styles/test_basic.rb +1 -1
  137. data/test/unit/core/shape/styles/test_graphic.rb +1 -1
  138. data/test/unit/core/shape/styles/test_text.rb +1 -1
  139. data/test/unit/core/shape/text/test_format.rb +1 -1
  140. data/test/unit/core/shape/text/test_internal.rb +1 -1
  141. data/test/unit/core/shape/text_block/formatter/test_basic.rb +1 -1
  142. data/test/unit/core/shape/text_block/formatter/test_datetime.rb +1 -1
  143. data/test/unit/core/shape/text_block/formatter/test_number.rb +1 -1
  144. data/test/unit/core/shape/text_block/formatter/test_padding.rb +2 -2
  145. data/test/unit/core/shape/text_block/test_format.rb +1 -1
  146. data/test/unit/core/shape/text_block/test_formatter.rb +1 -1
  147. data/test/unit/core/shape/text_block/test_interface.rb +1 -1
  148. data/test/unit/core/shape/text_block/test_internal.rb +3 -3
  149. data/test/unit/core/test_shape.rb +1 -1
  150. data/test/unit/core/test_utils.rb +2 -2
  151. data/test/unit/generator/pdf/document/graphics/test_attributes.rb +1 -1
  152. data/test/unit/generator/pdf/document/graphics/test_basic.rb +2 -1
  153. data/test/unit/generator/pdf/document/graphics/test_image.rb +27 -131
  154. data/test/unit/generator/pdf/document/graphics/test_text.rb +1 -1
  155. data/test/unit/generator/pdf/document/test_font.rb +2 -2
  156. data/test/unit/generator/pdf/document/test_graphics.rb +1 -1
  157. data/test/unit/generator/pdf/document/test_page.rb +7 -7
  158. data/test/unit/generator/pdf/document/test_parse_color.rb +1 -1
  159. data/test/unit/generator/pdf/prawn_ext/test_calc_image_dimensions.rb +41 -0
  160. data/test/unit/generator/pdf/prawn_ext/test_width_of.rb +22 -0
  161. data/test/unit/generator/pdf/test_document.rb +8 -6
  162. data/test/unit/generator/test_pdf.rb +12 -10
  163. data/test/unit/layout/test_base.rb +1 -1
  164. data/test/unit/layout/test_format.rb +2 -2
  165. data/test/unit/layout/test_legacy_schema.rb +4 -4
  166. data/test/unit/layout/test_version.rb +11 -80
  167. data/test/unit/report/test_base.rb +45 -59
  168. data/test/unit/report/test_internal.rb +1 -1
  169. data/test/unit/test_config.rb +1 -16
  170. data/test/unit/test_layout.rb +1 -1
  171. data/test/unit/test_report.rb +1 -1
  172. data/thinreports.gemspec +11 -14
  173. metadata +42 -103
  174. data/lib/thinreports/generator.rb +0 -29
  175. data/lib/thinreports/generator/base.rb +0 -40
  176. data/lib/thinreports/generator/configuration.rb +0 -31
  177. data/lib/thinreports/report/events.rb +0 -32
  178. data/test/unit/generator/test_base.rb +0 -40
  179. data/test/unit/generator/test_configuration.rb +0 -21
@@ -1,15 +1,15 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Thinreports
4
- module Core::Shape
5
-
6
- module Text
7
- TYPE_NAME = 'text'
4
+ module Core
5
+ module Shape
6
+ module Text
7
+ TYPE_NAME = 'text'.freeze
8
+ end
8
9
  end
9
-
10
10
  end
11
11
  end
12
12
 
13
- require 'thinreports/core/shape/text/format'
14
- require 'thinreports/core/shape/text/internal'
15
- require 'thinreports/core/shape/text/interface'
13
+ require_relative 'text/format'
14
+ require_relative 'text/internal'
15
+ require_relative 'text/interface'
@@ -1,14 +1,15 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Thinreports
4
- module Core::Shape
5
-
6
- class Text::Format < Basic::Format
7
-
8
- config_reader :texts
9
- config_reader valign: %w( style vertical-align )
10
- config_reader line_height: %w( style line-height )
4
+ module Core
5
+ module Shape
6
+ module Text
7
+ class Format < Basic::Format
8
+ config_reader :texts
9
+ config_reader valign: %w[style vertical-align]
10
+ config_reader line_height: %w[style line-height]
11
+ end
12
+ end
11
13
  end
12
-
13
14
  end
14
15
  end
@@ -1,16 +1,18 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Thinreports
4
- module Core::Shape
5
-
6
- class Text::Interface < Basic::Interface
7
- private
8
-
9
- # @see Thinreports::Core::Shape::Base::Interface#init_internal
10
- def init_internal(parent, format)
11
- Text::Internal.new(parent, format)
4
+ module Core
5
+ module Shape
6
+ module Text
7
+ class Interface < Basic::Interface
8
+ private
9
+
10
+ # @see Thinreports::Core::Shape::Base::Interface#init_internal
11
+ def init_internal(parent, format)
12
+ Text::Internal.new(parent, format)
13
+ end
14
+ end
12
15
  end
13
16
  end
14
-
15
17
  end
16
- end
18
+ end
@@ -1,20 +1,22 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Thinreports
4
- module Core::Shape
4
+ module Core
5
+ module Shape
6
+ module Text
7
+ class Internal < Basic::Internal
8
+ # Delegate to Format's methods
9
+ format_delegators :texts, :box
5
10
 
6
- class Text::Internal < Basic::Internal
7
- # Delegate to Format's methods
8
- format_delegators :texts, :box
11
+ def style
12
+ @style ||= Style::Text.new(format)
13
+ end
9
14
 
10
- def style
11
- @style ||= Style::Text.new(format)
12
- end
13
-
14
- def type_of?(type_name)
15
- type_name == Text::TYPE_NAME
15
+ def type_of?(type_name)
16
+ type_name == Text::TYPE_NAME
17
+ end
18
+ end
16
19
  end
17
20
  end
18
-
19
21
  end
20
22
  end
@@ -1,16 +1,16 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Thinreports
4
- module Core::Shape
5
-
6
- module TextBlock
7
- TYPE_NAME = 'text-block'
4
+ module Core
5
+ module Shape
6
+ module TextBlock
7
+ TYPE_NAME = 'text-block'.freeze
8
+ end
8
9
  end
9
-
10
10
  end
11
11
  end
12
12
 
13
- require 'thinreports/core/shape/text_block/format'
14
- require 'thinreports/core/shape/text_block/internal'
15
- require 'thinreports/core/shape/text_block/interface'
16
- require 'thinreports/core/shape/text_block/formatter'
13
+ require_relative 'text_block/format'
14
+ require_relative 'text_block/internal'
15
+ require_relative 'text_block/interface'
16
+ require_relative 'text_block/formatter'
@@ -1,39 +1,41 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Thinreports
4
- module Core::Shape
5
-
6
- class TextBlock::Format < Basic::BlockFormat
7
- # For saving compatible 0.8.x format API
8
- config_reader ref_id: %w( reference-id )
9
- config_reader valign: %w( style vertical-align )
10
- config_reader overflow: %w( style overflow )
11
- config_reader line_height: %w( style line-height )
12
-
13
- config_reader format_base: %w( format base ),
14
- format_type: %w( format type ),
15
- format_datetime_format: %w( format datetime format ),
16
- format_number_delimiter: %w( format number delimiter ),
17
- format_number_precision: %w( format number precision ),
18
- format_padding_char: %w( format padding char ),
19
- format_padding_dir: %w( format padding direction )
20
-
21
- config_checker true, multiple: %w( multiple-line )
22
- config_checker 'R', format_padding_rdir: %w( format padding direction )
23
-
24
- config_reader format_padding_length: %w( format padding length ) do |len|
25
- blank_value?(len) ? 0 : len.to_i
26
- end
27
-
28
- config_reader :has_format? => %w( format type ) do |type|
29
- %w( datetime number padding ).include?(type)
30
- end
31
-
32
- # For saving compatible 0.8.x format API
33
- def has_reference?
34
- !blank_value?(ref_id)
4
+ module Core
5
+ module Shape
6
+ module TextBlock
7
+ class Format < Basic::BlockFormat
8
+ # For saving compatible 0.8.x format API
9
+ config_reader ref_id: %w[reference-id]
10
+ config_reader valign: %w[style vertical-align]
11
+ config_reader overflow: %w[style overflow]
12
+ config_reader line_height: %w[style line-height]
13
+
14
+ config_reader format_base: %w[format base],
15
+ format_type: %w[format type],
16
+ format_datetime_format: %w[format datetime format],
17
+ format_number_delimiter: %w[format number delimiter],
18
+ format_number_precision: %w[format number precision],
19
+ format_padding_char: %w[format padding char],
20
+ format_padding_dir: %w[format padding direction]
21
+
22
+ config_checker true, multiple: %w[multiple-line]
23
+ config_checker 'R', format_padding_rdir: %w[format padding direction]
24
+
25
+ config_reader format_padding_length: %w[format padding length] do |len|
26
+ blank_value?(len) ? 0 : len.to_i
27
+ end
28
+
29
+ config_reader has_format?: %w[format type] do |type|
30
+ %w[datetime number padding].include?(type)
31
+ end
32
+
33
+ # For saving compatible 0.8.x format API
34
+ def has_reference?
35
+ !blank_value?(ref_id)
36
+ end
37
+ end
35
38
  end
36
39
  end
37
-
38
40
  end
39
41
  end
@@ -1,32 +1,34 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Thinreports
4
- module Core::Shape::TextBlock
5
-
6
- module Formatter
7
- # @param [Thinreports::Core::Shape::TextBlock::Format] format
8
- # @return [Thinreports::Core::Shape::TextBlock::Formatter::Base]
9
- def self.setup(format)
10
- klass =
11
- if Thinreports.blank_value?(format.format_type)
12
- Basic
13
- else
14
- case format.format_type
15
- when 'number' then Number
16
- when 'datetime' then Datetime
17
- when 'padding' then Padding
18
- else
19
- raise Thinreports::Errors::UnknownFormatterType
20
- end
4
+ module Core
5
+ module Shape
6
+ module TextBlock
7
+ module Formatter
8
+ # @param [Thinreports::Core::Shape::TextBlock::Format] format
9
+ # @return [Thinreports::Core::Shape::TextBlock::Formatter::Base]
10
+ def self.setup(format)
11
+ klass =
12
+ if Thinreports.blank_value?(format.format_type)
13
+ Basic
14
+ else
15
+ case format.format_type
16
+ when 'number' then Number
17
+ when 'datetime' then Datetime
18
+ when 'padding' then Padding
19
+ else
20
+ raise Thinreports::Errors::UnknownFormatterType
21
+ end
22
+ end
23
+ klass.new(format)
21
24
  end
22
- klass.new(format)
25
+ end
23
26
  end
24
27
  end
25
-
26
28
  end
27
29
  end
28
30
 
29
- require 'thinreports/core/shape/text_block/formatter/basic'
30
- require 'thinreports/core/shape/text_block/formatter/datetime'
31
- require 'thinreports/core/shape/text_block/formatter/padding'
32
- require 'thinreports/core/shape/text_block/formatter/number'
31
+ require_relative 'formatter/basic'
32
+ require_relative 'formatter/datetime'
33
+ require_relative 'formatter/padding'
34
+ require_relative 'formatter/number'
@@ -1,39 +1,39 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Thinreports
4
- module Core::Shape::TextBlock
4
+ module Core
5
+ module Shape
6
+ module TextBlock
7
+ module Formatter
8
+ class Basic
9
+ include Utils
5
10
 
6
- class Formatter::Basic
7
- include Utils
11
+ attr_reader :format
8
12
 
9
- attr_reader :format
13
+ def initialize(format)
14
+ @format = format
15
+ end
10
16
 
11
- def initialize(format)
12
- @format = format
13
- end
17
+ def apply(value)
18
+ value = apply_format_to(value) if applicable?(value)
14
19
 
15
- def apply(value)
16
- if applicable?(value)
17
- value = apply_format_to(value)
18
- end
20
+ return value if blank_value?(format.format_base)
19
21
 
20
- unless blank_value?(format.format_base)
21
- format.format_base.gsub(/\{value\}/, value.to_s)
22
- else
23
- value
24
- end
25
- end
22
+ format.format_base.gsub(/\{value\}/, value.to_s)
23
+ end
26
24
 
27
- private
25
+ private
28
26
 
29
- def apply_format_to(value)
30
- value
31
- end
27
+ def apply_format_to(value)
28
+ value
29
+ end
32
30
 
33
- def applicable?(_value)
34
- true
31
+ def applicable?(_value)
32
+ true
33
+ end
34
+ end
35
+ end
35
36
  end
36
37
  end
37
-
38
38
  end
39
39
  end
@@ -1,20 +1,23 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Thinreports
4
- module Core::Shape::TextBlock
4
+ module Core
5
+ module Shape
6
+ module TextBlock
7
+ module Formatter
8
+ class Datetime < Formatter::Basic
9
+ private
5
10
 
6
- class Formatter::Datetime < Formatter::Basic
11
+ def apply_format_to(value)
12
+ value.strftime(format.format_datetime_format)
13
+ end
7
14
 
8
- private
9
-
10
- def apply_format_to(value)
11
- value.strftime(format.format_datetime_format)
12
- end
13
-
14
- def applicable?(value)
15
- !blank_value?(format.format_datetime_format) && value.respond_to?(:strftime)
15
+ def applicable?(value)
16
+ !blank_value?(format.format_datetime_format) && value.respond_to?(:strftime)
17
+ end
18
+ end
19
+ end
16
20
  end
17
21
  end
18
-
19
22
  end
20
23
  end
@@ -1,49 +1,55 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  require 'bigdecimal'
4
4
 
5
5
  module Thinreports
6
- module Core::Shape::TextBlock
7
-
8
- class Formatter::Number < Formatter::Basic
9
-
10
- private
11
-
12
- def apply_format_to(value)
13
- precision = format.format_number_precision
14
- delimiter = format.format_number_delimiter
15
-
16
- if_applicable value do |val|
17
- unless blank_value?(precision)
18
- val = number_with_precision(val, precision)
6
+ module Core
7
+ module Shape
8
+ module TextBlock
9
+ module Formatter
10
+ class Number < Formatter::Basic
11
+ private
12
+
13
+ def apply_format_to(value)
14
+ precision = format.format_number_precision
15
+ delimiter = format.format_number_delimiter
16
+
17
+ if_applicable value do |val|
18
+ unless blank_value?(precision)
19
+ val = number_with_precision(val, precision)
20
+ end
21
+ unless blank_value?(delimiter)
22
+ val = number_with_delimiter(val, delimiter)
23
+ end
24
+ val
25
+ end
26
+ end
27
+
28
+ def if_applicable(value, &block)
29
+ normalized_value = normalize(value)
30
+ normalized_value.nil? ? value : block.call(normalized_value)
31
+ end
32
+
33
+ def normalize(value)
34
+ if value.is_a?(String)
35
+ # rubocop:disable Style/RescueModifier
36
+ (Integer(value) rescue nil) || (Float(value) rescue nil)
37
+ else
38
+ value
39
+ end
40
+ end
41
+
42
+ def number_with_delimiter(value, delimiter = ',')
43
+ value.to_s.gsub(/(\d)(?=(\d{3})+(?!\d))/) { "#{$1}#{delimiter}" }
44
+ end
45
+
46
+ def number_with_precision(value, precision = 3)
47
+ value = BigDecimal(value.to_s).round(precision)
48
+ sprintf("%.#{precision}f", value)
49
+ end
19
50
  end
20
- unless blank_value?(delimiter)
21
- val = number_with_delimiter(val, delimiter)
22
- end
23
- val
24
51
  end
25
52
  end
26
-
27
- def if_applicable(value, &block)
28
- val =
29
- case value
30
- when Numeric then value
31
- when String
32
- (Integer(value) rescue nil) || (Float(value) rescue nil)
33
- else nil
34
- end
35
- val.nil? ? value : block.call(val)
36
- end
37
-
38
- def number_with_delimiter(value, delimiter = ',')
39
- value.to_s.gsub(/(\d)(?=(\d{3})+(?!\d))/){ "#{$1}#{delimiter}" }
40
- end
41
-
42
- def number_with_precision(value, precision = 3)
43
- value = BigDecimal(value.to_s).round(precision)
44
- sprintf("%.#{precision}f", value)
45
- end
46
53
  end
47
-
48
54
  end
49
55
  end