thinreports 0.12.1 → 0.13.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 (177) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +3 -3
  3. data/.github/workflows/test.yml +28 -36
  4. data/.gitignore +1 -1
  5. data/CHANGELOG.md +19 -1
  6. data/Gemfile +11 -0
  7. data/README.md +12 -17
  8. data/Rakefile +17 -11
  9. data/lib/thinreports/basic_report/core/errors.rb +62 -0
  10. data/lib/thinreports/basic_report/core/format/base.rb +91 -0
  11. data/lib/thinreports/basic_report/core/shape/base/interface.rb +42 -0
  12. data/lib/thinreports/basic_report/core/shape/base/internal.rb +53 -0
  13. data/lib/thinreports/basic_report/core/shape/basic/block_format.rb +15 -0
  14. data/lib/thinreports/basic_report/core/shape/basic/block_interface.rb +34 -0
  15. data/lib/thinreports/basic_report/core/shape/basic/block_internal.rb +36 -0
  16. data/lib/thinreports/basic_report/core/shape/basic/format.rb +24 -0
  17. data/lib/thinreports/basic_report/core/shape/basic/interface.rb +87 -0
  18. data/lib/thinreports/basic_report/core/shape/basic/internal.rb +28 -0
  19. data/lib/thinreports/{core → basic_report/core}/shape/basic.rb +6 -4
  20. data/lib/thinreports/basic_report/core/shape/image_block/format.rb +14 -0
  21. data/lib/thinreports/basic_report/core/shape/image_block/interface.rb +25 -0
  22. data/lib/thinreports/basic_report/core/shape/image_block/internal.rb +19 -0
  23. data/lib/thinreports/{core → basic_report/core}/shape/image_block.rb +6 -4
  24. data/lib/thinreports/basic_report/core/shape/list/format.rb +92 -0
  25. data/lib/thinreports/basic_report/core/shape/list/manager.rb +231 -0
  26. data/lib/thinreports/basic_report/core/shape/list/page.rb +112 -0
  27. data/lib/thinreports/basic_report/core/shape/list/page_state.rb +50 -0
  28. data/lib/thinreports/basic_report/core/shape/list/section_format.rb +37 -0
  29. data/lib/thinreports/basic_report/core/shape/list/section_interface.rb +51 -0
  30. data/lib/thinreports/basic_report/core/shape/list/section_internal.rb +33 -0
  31. data/lib/thinreports/{core → basic_report/core}/shape/list.rb +6 -4
  32. data/lib/thinreports/basic_report/core/shape/manager/format.rb +33 -0
  33. data/lib/thinreports/basic_report/core/shape/manager/internal.rb +109 -0
  34. data/lib/thinreports/basic_report/core/shape/manager/target.rb +114 -0
  35. data/lib/thinreports/basic_report/core/shape/page_number/format.rb +32 -0
  36. data/lib/thinreports/basic_report/core/shape/page_number/interface.rb +31 -0
  37. data/lib/thinreports/basic_report/core/shape/page_number/internal.rb +57 -0
  38. data/lib/thinreports/{core → basic_report/core}/shape/page_number.rb +6 -4
  39. data/lib/thinreports/basic_report/core/shape/stack_view/format.rb +29 -0
  40. data/lib/thinreports/basic_report/core/shape/stack_view/interface.rb +19 -0
  41. data/lib/thinreports/basic_report/core/shape/stack_view/internal.rb +24 -0
  42. data/lib/thinreports/basic_report/core/shape/stack_view/row_format.rb +41 -0
  43. data/lib/thinreports/{core → basic_report/core}/shape/stack_view.rb +6 -4
  44. data/lib/thinreports/basic_report/core/shape/style/base.rb +149 -0
  45. data/lib/thinreports/basic_report/core/shape/style/basic.rb +24 -0
  46. data/lib/thinreports/basic_report/core/shape/style/graphic.rb +43 -0
  47. data/lib/thinreports/basic_report/core/shape/style/text.rb +122 -0
  48. data/lib/thinreports/basic_report/core/shape/text/format.rb +17 -0
  49. data/lib/thinreports/basic_report/core/shape/text/interface.rb +20 -0
  50. data/lib/thinreports/basic_report/core/shape/text/internal.rb +24 -0
  51. data/lib/thinreports/{core → basic_report/core}/shape/text.rb +6 -4
  52. data/lib/thinreports/basic_report/core/shape/text_block/format.rb +43 -0
  53. data/lib/thinreports/basic_report/core/shape/text_block/formatter/basic.rb +41 -0
  54. data/lib/thinreports/basic_report/core/shape/text_block/formatter/datetime.rb +25 -0
  55. data/lib/thinreports/basic_report/core/shape/text_block/formatter/number.rb +69 -0
  56. data/lib/thinreports/basic_report/core/shape/text_block/formatter/padding.rb +27 -0
  57. data/lib/thinreports/basic_report/core/shape/text_block/formatter.rb +38 -0
  58. data/lib/thinreports/basic_report/core/shape/text_block/interface.rb +37 -0
  59. data/lib/thinreports/basic_report/core/shape/text_block/internal.rb +75 -0
  60. data/lib/thinreports/{core → basic_report/core}/shape/text_block.rb +6 -4
  61. data/lib/thinreports/basic_report/core/shape.rb +44 -0
  62. data/lib/thinreports/basic_report/core/utils.rb +47 -0
  63. data/lib/thinreports/basic_report/generator/pdf/document/draw_shape.rb +108 -0
  64. data/lib/thinreports/basic_report/generator/pdf/document/draw_template_items.rb +71 -0
  65. data/lib/thinreports/basic_report/generator/pdf/document/font.rb +90 -0
  66. data/lib/thinreports/basic_report/generator/pdf/document/graphics/attributes.rb +149 -0
  67. data/lib/thinreports/basic_report/generator/pdf/document/graphics/basic.rb +137 -0
  68. data/lib/thinreports/basic_report/generator/pdf/document/graphics/image.rb +99 -0
  69. data/lib/thinreports/basic_report/generator/pdf/document/graphics/text.rb +180 -0
  70. data/lib/thinreports/basic_report/generator/pdf/document/graphics.rb +42 -0
  71. data/lib/thinreports/basic_report/generator/pdf/document/page.rb +96 -0
  72. data/lib/thinreports/basic_report/generator/pdf/document/parse_color.rb +51 -0
  73. data/lib/thinreports/basic_report/generator/pdf/document.rb +132 -0
  74. data/lib/thinreports/basic_report/generator/pdf/drawer/base.rb +50 -0
  75. data/lib/thinreports/basic_report/generator/pdf/drawer/list.rb +65 -0
  76. data/lib/thinreports/basic_report/generator/pdf/drawer/list_section.rb +51 -0
  77. data/lib/thinreports/basic_report/generator/pdf/drawer/page.rb +127 -0
  78. data/lib/thinreports/basic_report/generator/pdf/prawn_ext/calc_image_dimensions.rb +34 -0
  79. data/lib/thinreports/basic_report/generator/pdf/prawn_ext/width_of.rb +31 -0
  80. data/lib/thinreports/basic_report/generator/pdf.rb +66 -0
  81. data/lib/thinreports/basic_report/layout/base.rb +48 -0
  82. data/lib/thinreports/basic_report/layout/format.rb +67 -0
  83. data/lib/thinreports/basic_report/layout/legacy_schema.rb +383 -0
  84. data/lib/thinreports/basic_report/layout/version.rb +41 -0
  85. data/lib/thinreports/{layout.rb → basic_report/layout.rb} +6 -4
  86. data/lib/thinreports/basic_report/report/base.rb +173 -0
  87. data/lib/thinreports/basic_report/report/internal.rb +107 -0
  88. data/lib/thinreports/basic_report/report/page.rb +97 -0
  89. data/lib/thinreports/basic_report/report.rb +26 -0
  90. data/lib/thinreports/basic_report.rb +15 -0
  91. data/lib/thinreports/config.rb +1 -1
  92. data/lib/thinreports/section_report.rb +12 -0
  93. data/lib/thinreports/version.rb +1 -1
  94. data/lib/thinreports.rb +2 -13
  95. data/thinreports.gemspec +3 -7
  96. metadata +97 -154
  97. data/gemfiles/prawn-2.2.gemfile +0 -5
  98. data/gemfiles/prawn-2.3.gemfile +0 -5
  99. data/gemfiles/prawn-2.4.gemfile +0 -5
  100. data/lib/thinreports/core/errors.rb +0 -60
  101. data/lib/thinreports/core/format/base.rb +0 -89
  102. data/lib/thinreports/core/shape/base/interface.rb +0 -40
  103. data/lib/thinreports/core/shape/base/internal.rb +0 -51
  104. data/lib/thinreports/core/shape/basic/block_format.rb +0 -13
  105. data/lib/thinreports/core/shape/basic/block_interface.rb +0 -32
  106. data/lib/thinreports/core/shape/basic/block_internal.rb +0 -34
  107. data/lib/thinreports/core/shape/basic/format.rb +0 -22
  108. data/lib/thinreports/core/shape/basic/interface.rb +0 -85
  109. data/lib/thinreports/core/shape/basic/internal.rb +0 -26
  110. data/lib/thinreports/core/shape/image_block/format.rb +0 -12
  111. data/lib/thinreports/core/shape/image_block/interface.rb +0 -23
  112. data/lib/thinreports/core/shape/image_block/internal.rb +0 -17
  113. data/lib/thinreports/core/shape/list/format.rb +0 -90
  114. data/lib/thinreports/core/shape/list/manager.rb +0 -229
  115. data/lib/thinreports/core/shape/list/page.rb +0 -110
  116. data/lib/thinreports/core/shape/list/page_state.rb +0 -48
  117. data/lib/thinreports/core/shape/list/section_format.rb +0 -35
  118. data/lib/thinreports/core/shape/list/section_interface.rb +0 -49
  119. data/lib/thinreports/core/shape/list/section_internal.rb +0 -31
  120. data/lib/thinreports/core/shape/manager/format.rb +0 -31
  121. data/lib/thinreports/core/shape/manager/internal.rb +0 -107
  122. data/lib/thinreports/core/shape/manager/target.rb +0 -112
  123. data/lib/thinreports/core/shape/page_number/format.rb +0 -30
  124. data/lib/thinreports/core/shape/page_number/interface.rb +0 -29
  125. data/lib/thinreports/core/shape/page_number/internal.rb +0 -55
  126. data/lib/thinreports/core/shape/stack_view/format.rb +0 -27
  127. data/lib/thinreports/core/shape/stack_view/interface.rb +0 -17
  128. data/lib/thinreports/core/shape/stack_view/internal.rb +0 -22
  129. data/lib/thinreports/core/shape/stack_view/row_format.rb +0 -39
  130. data/lib/thinreports/core/shape/style/base.rb +0 -146
  131. data/lib/thinreports/core/shape/style/basic.rb +0 -22
  132. data/lib/thinreports/core/shape/style/graphic.rb +0 -41
  133. data/lib/thinreports/core/shape/style/text.rb +0 -120
  134. data/lib/thinreports/core/shape/text/format.rb +0 -15
  135. data/lib/thinreports/core/shape/text/interface.rb +0 -18
  136. data/lib/thinreports/core/shape/text/internal.rb +0 -22
  137. data/lib/thinreports/core/shape/text_block/format.rb +0 -41
  138. data/lib/thinreports/core/shape/text_block/formatter/basic.rb +0 -39
  139. data/lib/thinreports/core/shape/text_block/formatter/datetime.rb +0 -23
  140. data/lib/thinreports/core/shape/text_block/formatter/number.rb +0 -67
  141. data/lib/thinreports/core/shape/text_block/formatter/padding.rb +0 -25
  142. data/lib/thinreports/core/shape/text_block/formatter.rb +0 -34
  143. data/lib/thinreports/core/shape/text_block/interface.rb +0 -35
  144. data/lib/thinreports/core/shape/text_block/internal.rb +0 -73
  145. data/lib/thinreports/core/shape.rb +0 -42
  146. data/lib/thinreports/core/utils.rb +0 -45
  147. data/lib/thinreports/generate.rb +0 -11
  148. data/lib/thinreports/generator/pdf/document/draw_shape.rb +0 -106
  149. data/lib/thinreports/generator/pdf/document/draw_template_items.rb +0 -69
  150. data/lib/thinreports/generator/pdf/document/font.rb +0 -88
  151. data/lib/thinreports/generator/pdf/document/graphics/attributes.rb +0 -147
  152. data/lib/thinreports/generator/pdf/document/graphics/basic.rb +0 -135
  153. data/lib/thinreports/generator/pdf/document/graphics/image.rb +0 -98
  154. data/lib/thinreports/generator/pdf/document/graphics/text.rb +0 -178
  155. data/lib/thinreports/generator/pdf/document/graphics.rb +0 -40
  156. data/lib/thinreports/generator/pdf/document/page.rb +0 -94
  157. data/lib/thinreports/generator/pdf/document/parse_color.rb +0 -49
  158. data/lib/thinreports/generator/pdf/document.rb +0 -130
  159. data/lib/thinreports/generator/pdf/drawer/base.rb +0 -48
  160. data/lib/thinreports/generator/pdf/drawer/list.rb +0 -63
  161. data/lib/thinreports/generator/pdf/drawer/list_section.rb +0 -49
  162. data/lib/thinreports/generator/pdf/drawer/page.rb +0 -125
  163. data/lib/thinreports/generator/pdf/prawn_ext/calc_image_dimensions.rb +0 -32
  164. data/lib/thinreports/generator/pdf/prawn_ext/width_of.rb +0 -29
  165. data/lib/thinreports/generator/pdf.rb +0 -63
  166. data/lib/thinreports/layout/base.rb +0 -46
  167. data/lib/thinreports/layout/format.rb +0 -65
  168. data/lib/thinreports/layout/legacy_schema.rb +0 -381
  169. data/lib/thinreports/layout/version.rb +0 -39
  170. data/lib/thinreports/report/base.rb +0 -169
  171. data/lib/thinreports/report/internal.rb +0 -105
  172. data/lib/thinreports/report/page.rb +0 -95
  173. data/lib/thinreports/report.rb +0 -24
  174. /data/lib/thinreports/{core → basic_report/core}/shape/base.rb +0 -0
  175. /data/lib/thinreports/{core → basic_report/core}/shape/manager.rb +0 -0
  176. /data/lib/thinreports/{core → basic_report/core}/shape/style.rb +0 -0
  177. /data/lib/thinreports/{generator → basic_report/generator}/pdf/prawn_ext.rb +0 -0
@@ -0,0 +1,149 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Thinreports
4
+ module BasicReport
5
+ module Core
6
+ module Shape
7
+ module Style
8
+ class Base
9
+ include Utils
10
+
11
+ class << self
12
+ # @param [Symbol] style_method
13
+ # @param [String] style
14
+ # @return [void]
15
+ def style_accessor(style_method, style)
16
+ style_reader(style_method, style)
17
+ style_writer(style_method, style)
18
+ end
19
+
20
+ # @see .style_accessor
21
+ def style_reader(style_method, style)
22
+ define_method(style_method) do
23
+ read_internal_style(style)
24
+ end
25
+ end
26
+
27
+ # @see .style_accessor
28
+ def style_writer(style_method, style)
29
+ define_method(:"#{style_method}=") do |value|
30
+ write_internal_style(style, value)
31
+ end
32
+ end
33
+
34
+ # @param [Array<Symbol>] style_methods
35
+ def style_accessible(*style_methods)
36
+ accessible_styles.concat(style_methods)
37
+ end
38
+
39
+ # @return [Array<Symbol>]
40
+ def accessible_styles
41
+ @accessible_styles ||= []
42
+ end
43
+
44
+ def inherited(s)
45
+ s.accessible_styles.concat(accessible_styles.dup)
46
+ end
47
+ end
48
+
49
+ # @return [Hash]
50
+ attr_accessor :styles
51
+ # @see .accessible_styles
52
+ attr_reader :accessible_styles
53
+
54
+ # @param [Thinreports::BasicReport::Core::Format::Base] format
55
+ # @param [Hash] default_styles ({})
56
+ def initialize(format, default_styles = {})
57
+ @format = format
58
+ @styles = default_styles
59
+ @base_styles = format.style || {}
60
+
61
+ @accessible_styles = self.class.accessible_styles.dup
62
+ end
63
+
64
+ # @param [Symbol] style_method
65
+ # @return [Object]
66
+ def [](style_method)
67
+ verify_style_method(style_method)
68
+ send(style_method.to_sym)
69
+ end
70
+
71
+ # @param [Symbol] style_method
72
+ # @param [String, Number, Array<String, Number>] value
73
+ def []=(style_method, value)
74
+ verify_style_method(style_method)
75
+ send(:"#{style_method}=", value)
76
+ end
77
+
78
+ # @return [String]
79
+ def identifier
80
+ create_identifier(@styles)
81
+ end
82
+
83
+ # @return [self]
84
+ def copy
85
+ self.class.new(@format, @styles.empty? ? {} : deep_copy(@styles))
86
+ end
87
+
88
+ # @param [String, Symbol] style
89
+ # @return [Object]
90
+ def read_internal_style(style)
91
+ style = style.to_s
92
+ @styles.key?(style) ? @styles[style] : @base_styles[style]
93
+ end
94
+
95
+ # @param [String, Symbol] style
96
+ # @param [Object] value
97
+ def write_internal_style(style, value)
98
+ @styles[style.to_s] = value
99
+ end
100
+
101
+ # @param [Symbol] style_method
102
+ # @return [Boolean]
103
+ def has_style?(style_method)
104
+ accessible_styles.include?(style_method)
105
+ end
106
+
107
+ # @return [Hash]
108
+ def finalized_styles
109
+ @finalized_styles ||=
110
+ if @styles.empty?
111
+ @base_styles.dup
112
+ else
113
+ @base_styles.merge(@styles)
114
+ end
115
+ end
116
+
117
+ private
118
+
119
+ # @param [Hash] s
120
+ # @return [String]
121
+ def create_identifier(s)
122
+ s.empty? ? '' : s.hash.to_s
123
+ end
124
+
125
+ # @param [Symbol] style_method
126
+ # @raise Thinreports::BasicReport::Errors::UnknownShapeStyleName
127
+ def verify_style_method(style_method)
128
+ return if has_style?(style_method)
129
+
130
+ raise Thinreports::BasicReport::Errors::UnknownShapeStyleName.new(
131
+ style_method,
132
+ accessible_styles
133
+ )
134
+ end
135
+
136
+ # @param [Object] value
137
+ # @param [Array<Object>] allows
138
+ # @param [String] msg (nil)
139
+ # @raise ArgumentError
140
+ def verify_style_value(value, allows, msg = nil)
141
+ raise ArgumentError, msg unless allows.include?(value)
142
+ end
143
+ end
144
+ end
145
+ end
146
+ end
147
+ end
148
+ end
149
+
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Thinreports
4
+ module BasicReport
5
+ module Core
6
+ module Shape
7
+ module Style
8
+ class Basic < Style::Base
9
+ style_accessible :visible, :offset_x, :offset_y
10
+ attr_accessor :visible
11
+
12
+ style_accessor :offset_x, 'offset-x'
13
+ style_accessor :offset_y, 'offset-y'
14
+
15
+ def initialize(*args)
16
+ super
17
+ @visible = @format.display?
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Thinreports
4
+ module BasicReport
5
+ module Core
6
+ module Shape
7
+ module Style
8
+ class Graphic < Style::Basic
9
+ style_accessible :border_color, :border_width, :fill_color, :border
10
+
11
+ # @method border_color
12
+ # @return [String]
13
+ # @method border_color=(color)
14
+ # @param [String] color
15
+ style_accessor :border_color, 'border-color'
16
+
17
+ # @method border_width
18
+ # @return [Number]
19
+ style_accessor :border_width, 'border-width'
20
+
21
+ # @method fill_color
22
+ # @return [String]
23
+ # @method fill_color=(color)
24
+ # @param [String] color
25
+ style_accessor :fill_color, 'fill-color'
26
+
27
+ # @return [Array<String, Number>]
28
+ def border
29
+ [border_width, border_color]
30
+ end
31
+
32
+ # @param [Array<String, Number>] width_and_color
33
+ def border=(width_and_color)
34
+ w, c = width_and_color
35
+ self.border_width = w
36
+ self.border_color = c
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,122 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Thinreports
4
+ module BasicReport
5
+ module Core
6
+ module Shape
7
+ module Style
8
+ class Text < Style::Basic
9
+ style_accessible :bold, :italic, :underline, :linethrough,
10
+ :align, :valign, :color, :font_size
11
+
12
+ # @method color
13
+ # @return [String]
14
+ # @method color=(v)
15
+ # @param [String] v
16
+ style_accessor :color, 'color'
17
+
18
+ # @method font_size
19
+ # @return [Numeric, String]
20
+ # @method font_size=(v)
21
+ # @param [Numeric, String] v
22
+ style_accessor :font_size, 'font-size'
23
+
24
+ def initialize(*)
25
+ super
26
+ initialize_font_style
27
+ end
28
+
29
+ # @return [Boolean]
30
+ def bold
31
+ read_internal_style('font-style').include?('bold')
32
+ end
33
+
34
+ # @param [Boolean] enable
35
+ def bold=(enable)
36
+ write_font_style('bold', enable)
37
+ end
38
+
39
+ # @return [Boolean]
40
+ def italic
41
+ read_internal_style('font-style').include?('italic')
42
+ end
43
+
44
+ # @param [Boolean] enable
45
+ def italic=(enable)
46
+ write_font_style('italic', enable)
47
+ end
48
+
49
+ # @return [Boolean]
50
+ def underline
51
+ read_internal_style('font-style').include?('underline')
52
+ end
53
+
54
+ # @param [Boolean] enable
55
+ def underline=(enable)
56
+ write_font_style('underline', enable)
57
+ end
58
+
59
+ # @return [Boolean]
60
+ def linethrough
61
+ read_internal_style('font-style').include?('linethrough')
62
+ end
63
+
64
+ # @param [Boolean] enable
65
+ def linethrough=(enable)
66
+ write_font_style('linethrough', enable)
67
+ end
68
+
69
+ # @return [:left, :center, :right]
70
+ def align
71
+ read_internal_style('text-align').to_sym
72
+ end
73
+
74
+ # @param [:left, :center, :right] align_name
75
+ def align=(align_name)
76
+ verify_style_value(align_name, %i[left center right],
77
+ 'Only :left or :center, :right can be specified as align.')
78
+ write_internal_style('text-align', align_name.to_s)
79
+ end
80
+
81
+ # @return [:top, :middle, :bottom]
82
+ def valign
83
+ vertical_align = read_internal_style('vertical-align')
84
+ blank_value?(vertical_align) ? :top : vertical_align.to_sym
85
+ end
86
+
87
+ # @param [:top, :center, :middle, :bottom] valign_name
88
+ def valign=(valign_name)
89
+ if valign_name == :center
90
+ warn '[DEPRECATION] :center value for valign style is deprecated' \
91
+ ' and will be removed in thinreports-generator 1.0.' \
92
+ ' Please use :middle instead of :center.'
93
+ valign_name = :middle
94
+ end
95
+
96
+ verify_style_value(
97
+ valign_name,
98
+ %i[top middle bottom],
99
+ 'Only :top or :middle (:center), :bottom can be specified as valign.'
100
+ )
101
+ write_internal_style('vertical-align', valign_name.to_s)
102
+ end
103
+
104
+ private
105
+
106
+ def initialize_font_style
107
+ styles['font-style'] ||= (@base_styles['font-style'] || []).dup
108
+ end
109
+
110
+ def write_font_style(style_name, enable)
111
+ if enable
112
+ styles['font-style'].push(style_name).uniq!
113
+ else
114
+ styles['font-style'].delete(style_name)
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Thinreports
4
+ module BasicReport
5
+ module Core
6
+ module Shape
7
+ module Text
8
+ class Format < Basic::Format
9
+ config_reader :texts
10
+ config_reader valign: %w[style vertical-align]
11
+ config_reader line_height: %w[style line-height]
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Thinreports
4
+ module BasicReport
5
+ module Core
6
+ module Shape
7
+ module Text
8
+ class Interface < Basic::Interface
9
+ private
10
+
11
+ # @see Thinreports::BasicReport::Core::Shape::Base::Interface#init_internal
12
+ def init_internal(parent, format)
13
+ Text::Internal.new(parent, format)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Thinreports
4
+ module BasicReport
5
+ module Core
6
+ module Shape
7
+ module Text
8
+ class Internal < Basic::Internal
9
+ # Delegate to Format's methods
10
+ format_delegators :texts, :box
11
+
12
+ def style
13
+ @style ||= Style::Text.new(format)
14
+ end
15
+
16
+ def type_of?(type_name)
17
+ type_name == Text::TYPE_NAME
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,10 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Thinreports
4
- module Core
5
- module Shape
6
- module Text
7
- TYPE_NAME = 'text'
4
+ module BasicReport
5
+ module Core
6
+ module Shape
7
+ module Text
8
+ TYPE_NAME = 'text'
9
+ end
8
10
  end
9
11
  end
10
12
  end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Thinreports
4
+ module BasicReport
5
+ module Core
6
+ module Shape
7
+ module TextBlock
8
+ class Format < Basic::BlockFormat
9
+ # For saving compatible 0.8.x format API
10
+ config_reader ref_id: %w[reference-id]
11
+ config_reader valign: %w[style vertical-align]
12
+ config_reader overflow: %w[style overflow]
13
+ config_reader line_height: %w[style line-height]
14
+
15
+ config_reader format_base: %w[format base],
16
+ format_type: %w[format type],
17
+ format_datetime_format: %w[format datetime format],
18
+ format_number_delimiter: %w[format number delimiter],
19
+ format_number_precision: %w[format number precision],
20
+ format_padding_char: %w[format padding char],
21
+ format_padding_dir: %w[format padding direction]
22
+
23
+ config_checker true, multiple: %w[multiple-line]
24
+ config_checker 'R', format_padding_rdir: %w[format padding direction]
25
+
26
+ config_reader format_padding_length: %w[format padding length] do |len|
27
+ blank_value?(len) ? 0 : len.to_i
28
+ end
29
+
30
+ config_reader has_format?: %w[format type] do |type|
31
+ %w[datetime number padding].include?(type)
32
+ end
33
+
34
+ # For saving compatible 0.8.x format API
35
+ def has_reference?
36
+ !blank_value?(ref_id)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Thinreports
4
+ module BasicReport
5
+ module Core
6
+ module Shape
7
+ module TextBlock
8
+ module Formatter
9
+ class Basic
10
+ include Utils
11
+
12
+ attr_reader :format
13
+
14
+ def initialize(format)
15
+ @format = format
16
+ end
17
+
18
+ def apply(value)
19
+ value = apply_format_to(value) if applicable?(value)
20
+
21
+ return value if blank_value?(format.format_base)
22
+
23
+ format.format_base.gsub(/\{value\}/, value.to_s)
24
+ end
25
+
26
+ private
27
+
28
+ def apply_format_to(value)
29
+ value
30
+ end
31
+
32
+ def applicable?(_value)
33
+ true
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Thinreports
4
+ module BasicReport
5
+ module Core
6
+ module Shape
7
+ module TextBlock
8
+ module Formatter
9
+ class Datetime < Formatter::Basic
10
+ private
11
+
12
+ def apply_format_to(value)
13
+ value.strftime(format.format_datetime_format)
14
+ end
15
+
16
+ def applicable?(value)
17
+ !blank_value?(format.format_datetime_format) && value.respond_to?(:strftime)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bigdecimal'
4
+
5
+ module Thinreports
6
+ module BasicReport
7
+ module Core
8
+ module Shape
9
+ module TextBlock
10
+ module Formatter
11
+ class Number < Formatter::Basic
12
+ private
13
+
14
+ def apply_format_to(value)
15
+ precision = format.format_number_precision
16
+ delimiter = format.format_number_delimiter
17
+
18
+ if_applicable value do |val|
19
+ val = number_with_precision(val, precision) unless blank_value?(precision)
20
+ val = number_with_delimiter(val, delimiter) unless blank_value?(delimiter)
21
+ val
22
+ end
23
+ end
24
+
25
+ def if_applicable(value, &block)
26
+ normalized_value = normalize(value)
27
+ normalized_value.nil? ? value : block.call(normalized_value)
28
+ end
29
+
30
+ def normalize(value)
31
+ if value.is_a?(String)
32
+ convert_to_integer(value) || convert_to_float(value)
33
+ else
34
+ value
35
+ end
36
+ end
37
+
38
+ def number_with_delimiter(value, delimiter = ',')
39
+ value_int, value_float = value.to_s.split('.')
40
+
41
+ [
42
+ value_int.gsub(/(\d)(?=(\d{3})+(?!\d))/) { "#{$1}#{delimiter}" },
43
+ value_float
44
+ ].compact.join('.')
45
+ end
46
+
47
+ def number_with_precision(value, precision = 3)
48
+ value = BigDecimal(value.to_s).round(precision)
49
+ sprintf("%.#{precision}f", value)
50
+ end
51
+
52
+ def convert_to_integer(value)
53
+ Integer(value)
54
+ rescue ArgumentError
55
+ nil
56
+ end
57
+
58
+ def convert_to_float(value)
59
+ Float(value)
60
+ rescue ArgumentError
61
+ nil
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Thinreports
4
+ module BasicReport
5
+ module Core
6
+ module Shape
7
+ module TextBlock
8
+ module Formatter
9
+ class Padding < Formatter::Basic
10
+ private
11
+
12
+ def apply_format_to(value)
13
+ value.to_s.send(format.format_padding_rdir? ? :ljust : :rjust,
14
+ format.format_padding_length,
15
+ format.format_padding_char)
16
+ end
17
+
18
+ def applicable?(_value)
19
+ !blank_value?(format.format_padding_char) && format.format_padding_length > 0
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Thinreports
4
+ module BasicReport
5
+ module Core
6
+ module Shape
7
+ module TextBlock
8
+ module Formatter
9
+ include Utils
10
+
11
+ # @param [Thinreports::BasicReport::Core::Shape::TextBlock::Format] format
12
+ # @return [Thinreports::BasicReport::Core::Shape::TextBlock::Formatter::Base]
13
+ def self.setup(format)
14
+ klass =
15
+ if blank_value?(format.format_type)
16
+ Basic
17
+ else
18
+ case format.format_type
19
+ when 'number' then Number
20
+ when 'datetime' then Datetime
21
+ when 'padding' then Padding
22
+ else
23
+ raise Thinreports::BasicReport::Errors::UnknownFormatterType
24
+ end
25
+ end
26
+ klass.new(format)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ require_relative 'formatter/basic'
36
+ require_relative 'formatter/datetime'
37
+ require_relative 'formatter/padding'
38
+ require_relative 'formatter/number'