thinreports 0.7.6 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (277) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +3 -5
  3. data/.travis.yml +8 -4
  4. data/CHANGELOG.md +220 -0
  5. data/Gemfile +2 -9
  6. data/MIT-LICENSE +1 -1
  7. data/README.md +286 -0
  8. data/Rakefile +38 -8
  9. data/examples/character_spacing/character_spacing.rb +8 -0
  10. data/examples/character_spacing/character_spacing.tlf +1 -0
  11. data/examples/dynamic_image/dynamic_image.rb +31 -0
  12. data/examples/dynamic_image/dynamic_image.tlf +1 -0
  13. data/examples/dynamic_style/dynamic_style.rb +150 -0
  14. data/examples/dynamic_style/dynamic_style.tlf +1 -0
  15. data/examples/dynamic_style/dynamic_style_in_list.tlf +1 -0
  16. data/examples/eudc/eudc.rb +20 -0
  17. data/examples/eudc/eudc.tlf +1 -0
  18. data/examples/helper.rb +50 -0
  19. data/examples/hidden_shapes/hidden_shapes.rb +9 -0
  20. data/examples/hidden_shapes/hidden_shapes.tlf +1 -0
  21. data/examples/list_events/list_events.rb +59 -0
  22. data/examples/list_events/list_events_0_7_7.tlf +1 -0
  23. data/examples/list_events/list_events_0_8.tlf +1 -0
  24. data/examples/list_manual_generation/list_manual_generation.rb +22 -0
  25. data/examples/list_manual_generation/list_manual_generation.tlf +1 -0
  26. data/examples/list_page_number/list_page_number.rb +17 -0
  27. data/examples/list_page_number/list_page_number.tlf +1 -0
  28. data/examples/page_number/page_number.rb +33 -0
  29. data/examples/page_number/page_number.tlf +1 -0
  30. data/examples/password_setting/password_setting.rb +10 -0
  31. data/examples/password_setting/password_setting.tlf +1 -0
  32. data/examples/report_callbacks/report_callbacks.rb +31 -0
  33. data/examples/report_callbacks/report_callbacks.tlf +1 -0
  34. data/examples/single_line_tblock/single_line_tblock.rb +13 -0
  35. data/examples/single_line_tblock/single_line_tblock.tlf +1 -0
  36. data/examples/tblock_overflow/tblock_overflow.rb +20 -0
  37. data/examples/tblock_overflow/tblock_overflow.tlf +1 -0
  38. data/examples/tblock_styles/font_size.tlf +1 -0
  39. data/examples/tblock_styles/tblock_styles.rb +43 -0
  40. data/examples/tblock_styles/tblock_styles.tlf +1 -0
  41. data/examples/text_align/text_align.rb +8 -0
  42. data/examples/text_align/text_align.tlf +1 -0
  43. data/examples/typeB_page_size/B4_ISO.tlf +1 -0
  44. data/examples/typeB_page_size/B4_JIS.tlf +1 -0
  45. data/examples/typeB_page_size/typeB_page_size.rb +17 -0
  46. data/examples/word_wrap/word_wrap.rb +26 -0
  47. data/examples/word_wrap/word_wrap.tlf +1 -0
  48. data/lib/thinreports/config.rb +30 -10
  49. data/lib/thinreports/core/errors.rb +23 -20
  50. data/lib/thinreports/core/events.rb +18 -19
  51. data/lib/thinreports/core/format/base.rb +20 -25
  52. data/lib/thinreports/core/format/builder.rb +19 -27
  53. data/lib/thinreports/core/shape/base/interface.rb +15 -15
  54. data/lib/thinreports/core/shape/base/internal.rb +15 -20
  55. data/lib/thinreports/core/shape/base.rb +4 -5
  56. data/lib/thinreports/core/shape/basic/block_format.rb +3 -4
  57. data/lib/thinreports/core/shape/basic/block_interface.rb +9 -4
  58. data/lib/thinreports/core/shape/basic/block_internal.rb +9 -10
  59. data/lib/thinreports/core/shape/basic/format.rb +11 -10
  60. data/lib/thinreports/core/shape/basic/interface.rb +3 -3
  61. data/lib/thinreports/core/shape/basic/internal.rb +9 -6
  62. data/lib/thinreports/core/shape/basic.rb +1 -1
  63. data/lib/thinreports/core/shape/image_block/format.rb +5 -6
  64. data/lib/thinreports/core/shape/image_block/interface.rb +9 -7
  65. data/lib/thinreports/core/shape/image_block/internal.rb +5 -6
  66. data/lib/thinreports/core/shape/image_block.rb +1 -1
  67. data/lib/thinreports/core/shape/list/configuration.rb +37 -16
  68. data/lib/thinreports/core/shape/list/events.rb +11 -11
  69. data/lib/thinreports/core/shape/list/format.rb +27 -28
  70. data/lib/thinreports/core/shape/list/manager.rb +135 -95
  71. data/lib/thinreports/core/shape/list/page.rb +57 -51
  72. data/lib/thinreports/core/shape/list/page_state.rb +19 -17
  73. data/lib/thinreports/core/shape/list/section_format.rb +14 -15
  74. data/lib/thinreports/core/shape/list/section_interface.rb +15 -16
  75. data/lib/thinreports/core/shape/list/section_internal.rb +7 -8
  76. data/lib/thinreports/core/shape/list/store.rb +10 -11
  77. data/lib/thinreports/core/shape/list.rb +1 -1
  78. data/lib/thinreports/core/shape/manager/format.rb +10 -11
  79. data/lib/thinreports/core/shape/manager/internal.rb +22 -21
  80. data/lib/thinreports/core/shape/manager/target.rb +63 -34
  81. data/lib/thinreports/core/shape/manager.rb +1 -1
  82. data/lib/thinreports/core/shape/page_number/format.rb +25 -0
  83. data/lib/thinreports/core/shape/page_number/interface.rb +27 -0
  84. data/lib/thinreports/core/shape/page_number/internal.rb +57 -0
  85. data/lib/thinreports/core/shape/page_number.rb +15 -0
  86. data/lib/thinreports/core/shape/style/base.rb +29 -29
  87. data/lib/thinreports/core/shape/style/basic.rb +1 -1
  88. data/lib/thinreports/core/shape/style/graphic.rb +10 -25
  89. data/lib/thinreports/core/shape/style/text.rb +41 -31
  90. data/lib/thinreports/core/shape/style.rb +1 -1
  91. data/lib/thinreports/core/shape/text/format.rb +8 -9
  92. data/lib/thinreports/core/shape/text/interface.rb +2 -2
  93. data/lib/thinreports/core/shape/text/internal.rb +6 -7
  94. data/lib/thinreports/core/shape/text.rb +1 -1
  95. data/lib/thinreports/core/shape/text_block/format.rb +17 -17
  96. data/lib/thinreports/core/shape/text_block/formatter/basic.rb +13 -12
  97. data/lib/thinreports/core/shape/text_block/formatter/datetime.rb +8 -9
  98. data/lib/thinreports/core/shape/text_block/formatter/number.rb +11 -12
  99. data/lib/thinreports/core/shape/text_block/formatter/padding.rb +9 -32
  100. data/lib/thinreports/core/shape/text_block/formatter.rb +7 -8
  101. data/lib/thinreports/core/shape/text_block/interface.rb +2 -2
  102. data/lib/thinreports/core/shape/text_block/internal.rb +19 -20
  103. data/lib/thinreports/core/shape/text_block.rb +1 -1
  104. data/lib/thinreports/core/shape.rb +11 -13
  105. data/lib/thinreports/core/utils.rb +33 -32
  106. data/lib/thinreports/generator/base.rb +15 -24
  107. data/lib/thinreports/generator/configuration.rb +10 -9
  108. data/lib/thinreports/generator/pdf/configuration.rb +11 -6
  109. data/lib/thinreports/generator/pdf/document/draw_shape.rb +41 -32
  110. data/lib/thinreports/generator/pdf/document/font.rb +53 -44
  111. data/lib/thinreports/generator/pdf/document/graphics/attributes.rb +34 -25
  112. data/lib/thinreports/generator/pdf/document/graphics/basic.rb +25 -25
  113. data/lib/thinreports/generator/pdf/document/graphics/image.rb +15 -15
  114. data/lib/thinreports/generator/pdf/document/graphics/text.rb +43 -41
  115. data/lib/thinreports/generator/pdf/document/graphics.rb +8 -9
  116. data/lib/thinreports/generator/pdf/document/page.rb +37 -23
  117. data/lib/thinreports/generator/pdf/document/parse_color.rb +9 -10
  118. data/lib/thinreports/generator/pdf/document/parse_svg.rb +14 -15
  119. data/lib/thinreports/generator/pdf/document.rb +28 -28
  120. data/lib/thinreports/generator/pdf/drawer/base.rb +16 -22
  121. data/lib/thinreports/generator/pdf/drawer/list.rb +39 -21
  122. data/lib/thinreports/generator/pdf/drawer/list_section.rb +17 -18
  123. data/lib/thinreports/generator/pdf/drawer/page.rb +37 -26
  124. data/lib/thinreports/generator/pdf/prawn_ext.rb +18 -17
  125. data/lib/thinreports/generator/pdf.rb +28 -31
  126. data/lib/thinreports/generator.rb +6 -8
  127. data/lib/thinreports/layout/base.rb +24 -36
  128. data/lib/thinreports/layout/configuration.rb +11 -9
  129. data/lib/thinreports/layout/format.rb +26 -31
  130. data/lib/thinreports/layout/version.rb +8 -9
  131. data/lib/thinreports/layout.rb +2 -2
  132. data/lib/thinreports/report/base.rb +117 -109
  133. data/lib/thinreports/report/events.rb +5 -5
  134. data/lib/thinreports/report/internal.rb +48 -38
  135. data/lib/thinreports/{core → report}/page.rb +42 -32
  136. data/lib/thinreports/report.rb +10 -14
  137. data/lib/thinreports/version.rb +2 -3
  138. data/lib/thinreports.rb +14 -2
  139. data/test/data/font.ttf +0 -0
  140. data/test/data/layout_block.tlf +1 -0
  141. data/test/data/layout_list.tlf +1 -0
  142. data/test/data/layout_list_noheader.tlf +1 -0
  143. data/test/data/layout_text1.tlf +1 -0
  144. data/test/data/layout_text2.tlf +1 -0
  145. data/test/test_helper.rb +74 -0
  146. data/test/tmp/.gitkeep +0 -0
  147. data/test/unit/core/format/test_base.rb +14 -14
  148. data/test/unit/core/format/test_builder.rb +20 -25
  149. data/test/unit/core/shape/base/test_internal.rb +34 -37
  150. data/test/unit/core/shape/basic/test_basic_format.rb +4 -4
  151. data/test/unit/core/shape/basic/test_basic_interface.rb +15 -13
  152. data/test/unit/core/shape/basic/test_basic_internal.rb +19 -16
  153. data/test/unit/core/shape/basic/test_format.rb +4 -4
  154. data/test/unit/core/shape/basic/test_interface.rb +36 -46
  155. data/test/unit/core/shape/basic/test_internal.rb +29 -18
  156. data/test/unit/core/shape/image_block/test_format.rb +5 -5
  157. data/test/unit/core/shape/image_block/test_interface.rb +17 -16
  158. data/test/unit/core/shape/image_block/test_internal.rb +19 -16
  159. data/test/unit/core/shape/list/test_configuration.rb +50 -33
  160. data/test/unit/core/shape/list/test_events.rb +7 -7
  161. data/test/unit/core/shape/list/test_format.rb +16 -18
  162. data/test/unit/core/shape/list/test_manager.rb +15 -5
  163. data/test/unit/core/shape/list/test_page.rb +80 -22
  164. data/test/unit/core/shape/list/test_page_state.rb +13 -13
  165. data/test/unit/core/shape/list/test_section_format.rb +16 -17
  166. data/test/unit/core/shape/list/test_section_interface.rb +30 -22
  167. data/test/unit/core/shape/list/test_section_internal.rb +21 -19
  168. data/test/unit/core/shape/list/test_store.rb +7 -7
  169. data/test/unit/core/shape/manager/test_format.rb +11 -15
  170. data/test/unit/core/shape/manager/test_internal.rb +9 -9
  171. data/test/unit/core/shape/manager/test_target.rb +85 -57
  172. data/test/unit/core/shape/page_number/test_format.rb +73 -0
  173. data/test/unit/core/shape/page_number/test_interface.rb +33 -0
  174. data/test/unit/core/shape/page_number/test_internal.rb +81 -0
  175. data/test/unit/core/shape/styles/test_base.rb +9 -9
  176. data/test/unit/core/shape/styles/test_basic.rb +5 -5
  177. data/test/unit/core/shape/styles/test_graphic.rb +19 -33
  178. data/test/unit/core/shape/styles/test_text.rb +29 -7
  179. data/test/unit/core/shape/text/test_format.rb +6 -6
  180. data/test/unit/core/shape/text/test_internal.rb +11 -12
  181. data/test/unit/core/shape/text_block/formatter/test_basic.rb +16 -16
  182. data/test/unit/core/shape/text_block/formatter/test_datetime.rb +22 -20
  183. data/test/unit/core/shape/text_block/formatter/test_number.rb +34 -34
  184. data/test/unit/core/shape/text_block/formatter/test_padding.rb +44 -44
  185. data/test/unit/core/shape/text_block/test_format.rb +13 -8
  186. data/test/unit/core/shape/text_block/test_formatter.rb +17 -17
  187. data/test/unit/core/shape/text_block/test_interface.rb +37 -20
  188. data/test/unit/core/shape/text_block/test_internal.rb +38 -38
  189. data/test/unit/core/test_events.rb +10 -8
  190. data/test/unit/core/test_shape.rb +11 -7
  191. data/test/unit/core/test_utils.rb +68 -0
  192. data/test/unit/generator/pdf/document/graphics/test_attributes.rb +10 -4
  193. data/test/unit/generator/pdf/document/graphics/test_text.rb +60 -53
  194. data/test/unit/generator/pdf/document/test_draw_shape.rb +14 -6
  195. data/test/unit/generator/pdf/document/test_font.rb +79 -58
  196. data/test/unit/generator/pdf/document/test_graphics.rb +18 -19
  197. data/test/unit/generator/pdf/document/test_page.rb +68 -43
  198. data/test/unit/generator/pdf/document/test_parse_color.rb +5 -5
  199. data/test/unit/generator/pdf/test_configuration.rb +16 -9
  200. data/test/unit/generator/pdf/test_document.rb +8 -16
  201. data/test/unit/generator/test_base.rb +31 -42
  202. data/test/unit/generator/test_configuration.rb +6 -6
  203. data/test/unit/generator/test_pdf.rb +15 -21
  204. data/test/unit/layout/test_base.rb +34 -79
  205. data/test/unit/layout/test_configuration.rb +12 -37
  206. data/test/unit/layout/test_format.rb +36 -39
  207. data/test/unit/layout/test_version.rb +4 -4
  208. data/test/unit/report/test_base.rb +160 -192
  209. data/test/unit/report/test_events.rb +4 -4
  210. data/test/unit/report/test_internal.rb +102 -82
  211. data/test/unit/test_config.rb +31 -12
  212. data/test/unit/test_layout.rb +7 -7
  213. data/test/unit/test_report.rb +12 -24
  214. data/thinreports.gemspec +23 -26
  215. metadata +118 -181
  216. data/.yardopts +0 -1
  217. data/LICENSE +0 -1
  218. data/README.rdoc +0 -167
  219. data/doc/yardoc_templates/default/layout/html/footer.erb +0 -10
  220. data/lib/thinreports/core/ext/array.rb +0 -19
  221. data/lib/thinreports/core/ext/hash.rb +0 -19
  222. data/lib/thinreports/core/ext/object.rb +0 -25
  223. data/lib/thinreports/core/ext.rb +0 -5
  224. data/lib/thinreports/core/format.rb +0 -14
  225. data/lib/thinreports/core/ordered_hash.rb +0 -39
  226. data/lib/thinreports/core.rb +0 -20
  227. data/lib/thinreports/generator/pdf/drawer.rb +0 -16
  228. data/tasks/doc.rake +0 -13
  229. data/tasks/test.rake +0 -56
  230. data/test/benchmark/basic_estimate.tlf +0 -1
  231. data/test/benchmark/bench_basic_estimate.rb +0 -95
  232. data/test/case/character_spacing/character_spacing.rb +0 -7
  233. data/test/case/character_spacing/character_spacing.tlf +0 -1
  234. data/test/case/dynamic_image/dynamic_image.rb +0 -41
  235. data/test/case/dynamic_image/dynamic_image.tlf +0 -1
  236. data/test/case/dynamic_style/dynamic_style.rb +0 -154
  237. data/test/case/dynamic_style/dynamic_style.tlf +0 -1
  238. data/test/case/dynamic_style/dynamic_style_in_list.tlf +0 -1
  239. data/test/case/eudc/eudc.rb +0 -20
  240. data/test/case/eudc/eudc.tlf +0 -1
  241. data/test/case/helper.rb +0 -29
  242. data/test/case/hidden_shapes/hidden_shapes.rb +0 -13
  243. data/test/case/hidden_shapes/hidden_shapes.tlf +0 -1
  244. data/test/case/list_events/list_events.rb +0 -32
  245. data/test/case/list_events/list_events.tlf +0 -1
  246. data/test/case/list_header_inheriting/list_header_inheriting.rb +0 -17
  247. data/test/case/list_header_inheriting/list_header_inheriting.tlf +0 -1
  248. data/test/case/list_manual_generation/list_manual_generation.rb +0 -22
  249. data/test/case/list_manual_generation/list_manual_generation.tlf +0 -1
  250. data/test/case/single_line_tblock/single_line_tblock.rb +0 -15
  251. data/test/case/single_line_tblock/single_line_tblock.tlf +0 -1
  252. data/test/case/tblock_overflow/tblock_overflow.rb +0 -20
  253. data/test/case/tblock_overflow/tblock_overflow.tlf +0 -1
  254. data/test/case/tblock_styles/tblock_styles.rb +0 -27
  255. data/test/case/tblock_styles/tblock_styles.tlf +0 -1
  256. data/test/case/text_align/text_align.rb +0 -9
  257. data/test/case/text_align/text_align.tlf +0 -1
  258. data/test/unit/core/ext/array_spec.rb +0 -29
  259. data/test/unit/core/ext/hash_spec.rb +0 -29
  260. data/test/unit/core/ext/object_spec.rb +0 -30
  261. data/test/unit/core/ordered_hash_spec.rb +0 -51
  262. data/test/unit/core/shape/base/test_interface.rb +0 -52
  263. data/test/unit/core/utils_spec.rb +0 -56
  264. data/test/unit/data/basic_layout1.tlf +0 -1
  265. data/test/unit/data/basic_layout2.tlf +0 -1
  266. data/test/unit/data/basic_list_layout.tlf +0 -1
  267. data/test/unit/data/basic_list_noheader_layout.tlf +0 -1
  268. data/test/unit/helper.rb +0 -65
  269. /data/{test/case → examples}/dynamic_image/img200x100.png +0 -0
  270. /data/{test/case → examples}/dynamic_image/img50x50.png +0 -0
  271. /data/{test/case → examples}/dynamic_style/image.png +0 -0
  272. /data/{test/case → examples}/eudc/eudc.ttf +0 -0
  273. /data/{resources/fonts → fonts}/IPA_Font_License_Agreement_v1.0.txt +0 -0
  274. /data/{resources/fonts → fonts}/ipag.ttf +0 -0
  275. /data/{resources/fonts → fonts}/ipagp.ttf +0 -0
  276. /data/{resources/fonts → fonts}/ipam.ttf +0 -0
  277. /data/{resources/fonts → fonts}/ipamp.ttf +0 -0
@@ -1,30 +1,29 @@
1
1
  # coding: utf-8
2
2
 
3
- module ThinReports
3
+ module Thinreports
4
4
  module Report
5
-
5
+
6
6
  class Base
7
- # @return [ThinReports::Report::Internal]
8
- # @private
9
- attr_reader :internal
10
-
7
+ extend Forwardable
8
+ include Utils
9
+
11
10
  class << self
12
11
  # @param options (see #initialize)
13
12
  # @option options (see #initialize)
14
13
  # @yield [report]
15
- # @yieldparam [ThinReports::Report::Base] report
16
- # @return [ThinReports::Report::Base]
14
+ # @yieldparam [Thinreports::Report::Base] report
15
+ # @return [Thinreports::Report::Base]
17
16
  def create(options = {}, &block)
18
17
  unless block_given?
19
18
  raise ArgumentError, '#create requires a block'
20
19
  end
21
20
  report = new(options)
22
- block_exec_on(report, &block)
21
+ call_block_in(report, &block)
23
22
  report.finalize
24
-
23
+
25
24
  report
26
25
  end
27
-
26
+
28
27
  # @overload generate(type, options = {}, &block)
29
28
  # @param [Symbol] type
30
29
  # @param [Hash] options
@@ -39,40 +38,22 @@ module ThinReports
39
38
  # @return [String]
40
39
  def generate(*args, &block)
41
40
  raise ArgumentError, '#generate requires a block' unless block_given?
42
-
41
+
43
42
  report_opts, generator_opts = extract_options!(args)
44
-
43
+
45
44
  report = create(report_opts, &block)
46
45
  report.generate(*args.push(generator_opts))
47
46
  end
48
-
49
- # @overload generate_file(type, filename, options = {}, &block)
50
- # @overload generate_file(filename, options = {}, &block)
51
- # @param filename (see #generate_file)
52
- # @yield (see .create)
53
- # @yieldparam (see .create)
54
- # @see .generate
55
- # @return [void]
56
- def generate_file(*args, &block)
57
- raise ArgumentError, '#generate_file requires a block' unless block_given?
58
-
59
- report_opts, generator_opts = extract_options!(args)
60
-
61
- report = create(report_opts, &block)
62
- report.generate_file(*args.push(generator_opts))
63
- end
64
-
65
- private
66
-
47
+
67
48
  # @param [Array] args
68
49
  # @return [Array<Hash>]
69
50
  def extract_options!(args)
70
51
  if args.last.is_a?(::Hash)
71
52
  options = args.pop
72
-
53
+
73
54
  generator = options.delete(:generator) || {}
74
55
  report = options.delete(:report) || {}
75
-
56
+
76
57
  if options.key?(:layout)
77
58
  report[:layout] = options.delete(:layout)
78
59
  end
@@ -82,124 +63,151 @@ module ThinReports
82
63
  end
83
64
  end
84
65
  end
85
-
66
+
67
+ # @return [Thinreports::Report::Internal]
68
+ attr_reader :internal
69
+
70
+ # @return [Integer]
71
+ attr_reader :start_page_number
72
+
73
+ # @return [Thinreports::Report::Page]
74
+ def_delegator :internal, :page
75
+
76
+ # @return [Integer]
77
+ def_delegator :internal, :page_count
78
+
79
+ # @return [Array<Thinreports::Report::Page>]
80
+ def_delegator :internal, :pages
81
+
82
+ # @return [Thinreports::Layout::Base]
83
+ def_delegator :internal, :default_layout
84
+
86
85
  # @param [Hash] options
87
86
  # @option options [String, nil] :layout (nil)
88
87
  def initialize(options = {})
89
88
  @internal = Report::Internal.new(self, options)
89
+ @start_page_number = 1
90
+
91
+ @page_create_handler = nil
92
+ @generate_handler = nil
93
+ end
94
+
95
+ # @yield [page]
96
+ # @yieldparam [Thinreports::Report::Page] page
97
+ # @example
98
+ # report.on_page_create do |page|
99
+ # page.item(:header_title).value = 'Title'
100
+ # end
101
+ def on_page_create(&block)
102
+ internal.page_create_handler = block
90
103
  end
91
-
92
- # @param [String] layout path to layout-file.
104
+
105
+ # @param [Integer] page_number
106
+ def start_page_number_from(page_number)
107
+ @start_page_number = page_number
108
+ end
109
+
110
+ # @param [String] layout filename of layout file
93
111
  # @param [Hash] options
94
112
  # @option options [Boolean] :default (true)
95
113
  # @option options [Symbol] :id (nil)
96
114
  # @yield [config]
97
- # @yieldparam [ThinReports::Layout::Configuration] config
98
- # @return [void]
115
+ # @yieldparam [Thinreports::Layout::Configuration] config
116
+ # @example
117
+ # report.use_layout '/path/to/default_layout.tlf' # Default layout
118
+ # report.use_layout '/path/to/default_layout.tlf', default: true
119
+ # report.use_layout '/path/to/other_layout', id: :other_layout
99
120
  def use_layout(layout, options = {}, &block)
100
121
  internal.register_layout(layout, options, &block)
101
122
  end
102
-
123
+
124
+ # @example
125
+ # page = report.start_new_page
126
+ #
127
+ # report.start_new_page do |page|
128
+ # page.item(:text).value = 'value'
129
+ # end
130
+ #
131
+ # report.use_layout 'foo.tlf', default: true
132
+ # report.use_layout 'bar.tlf', id: :bar
133
+ #
134
+ # report.start_new_page # Use 'foo.tlf'
135
+ # report.start_new_page layout: :bar # Use 'bar.tlf'
136
+ # report.start_new_page layout: 'boo.tlf' # Use 'boo.tlf'
103
137
  # @param [Hash] options
104
138
  # @option options [String, Symbol] :layout (nil)
139
+ # @option options [Boolean] :count (true)
105
140
  # @yield [page]
106
- # @yieldparam [ThinReports::Core::Page] page
107
- # @return [ThinReports::Core::Page]
141
+ # @yieldparam [Thinreports::Report::Page] page
142
+ # @return [Thinreports::Report::Page]
108
143
  def start_new_page(options = {}, &block)
109
- unless layout = internal.load_layout(options[:layout])
110
- raise ThinReports::Errors::NoRegisteredLayoutFound
144
+ unless layout = internal.load_layout(options.delete(:layout))
145
+ raise Thinreports::Errors::NoRegisteredLayoutFound
111
146
  end
112
-
113
- page = internal.add_page(layout.init_new_page(self))
114
- block_exec_on(page, &block)
147
+
148
+ page = internal.add_page(layout.new_page(self, options))
149
+ call_block_in(page, &block)
115
150
  end
116
-
151
+
117
152
  # @param [Hash] options
118
153
  # @option options [Boolean] :count (true)
119
- # @return [ThinReports::Core::BlankPage]
154
+ # @return [Thinreports::Report::BlankPage]
120
155
  def add_blank_page(options = {})
121
- internal.add_page(Core::BlankPage.new(options[:count]))
156
+ internal.add_page(Report::BlankPage.new(options[:count]))
122
157
  end
123
158
  alias_method :blank_page, :add_blank_page
124
-
125
- # @param [Symbol, nil] id Return the default layout
126
- # if nil (see #default_layout).
127
- # @return [ThinReports::Layout::Base]
159
+
160
+ # @param [Symbol, nil] id
161
+ # @return [Thinreports::Layout::Base]
128
162
  def layout(id = nil)
129
163
  if id
130
164
  internal.layout_registry[id] ||
131
- raise(ThinReports::Errors::UnknownLayoutId)
165
+ raise(Thinreports::Errors::UnknownLayoutId)
132
166
  else
133
167
  internal.default_layout
134
168
  end
135
169
  end
136
-
137
- # @return [ThinReports::Layout::Base]
138
- def default_layout
139
- internal.default_layout
140
- end
141
-
142
- # @overload generate(type, options = {})
143
- # Specify the generator type.
144
- # @param [Symbol] type
145
- # @return [String]
170
+
146
171
  # @overload generate(options = {})
147
- # Using the default generator type.
148
172
  # @param [Hash] options ({})
149
173
  # @return [String]
174
+ # @overload generate(type, options = {})
175
+ # This way has been DEPRECATED. Use instead #generate(options = {}).
176
+ # @param [Symbol] type
177
+ # @return [String]
178
+ # @example Generate PDF data
179
+ # report.generate # => "%PDF-1.4...."
180
+ # @example Create a PDF file
181
+ # report.generate(filename: 'foo.pdf')
150
182
  def generate(*args)
151
183
  options = args.last.is_a?(::Hash) ? args.pop : {}
152
- type = args.first || ThinReports.config.generator.default
153
- ThinReports::Generator.new(type, self, options).generate
154
- end
155
-
156
- # @overload generate_file(type, filename, options = {})
157
- # @param type (#generate)
158
- # @return [void]
159
- # @overload generate_file(filename, options = {})
160
- # @param [String] filename
161
- # @param options (see #generate)
162
- # @return [void]
163
- def generate_file(*args)
164
- options = args.last.is_a?(::Hash) ? args.pop : {}
165
- args.unshift(ThinReports.config.generator.default) if args.size == 1
166
- type, filename = args
167
- ThinReports::Generator.new(type, self, options).generate_file(filename)
184
+ type = args.first || Thinreports.config.generator.default
185
+ filename = options.delete(:filename)
186
+ generator = Thinreports::Generator.new(type, self, options)
187
+
188
+ generator.generate(filename)
168
189
  end
169
-
170
- # @see ThinReports::Core::Shape::Manager::Target#list
190
+
191
+ # @see Thinreports::Core::Shape::Manager::Target#list
171
192
  def list(id = nil, &block)
172
193
  start_new_page if page.nil? || page.finalized?
173
194
  page.list(id, &block)
174
195
  end
175
-
176
- # @return [ThinReports::Report::Events]
196
+
197
+ # @return [Thinreports::Report::Events]
198
+ # @deprecated
199
+ # `Report::Base#events` will be removed in the next major version.
200
+ # Please use #on_page_create callbacks instead.
201
+ # See also https://github.com/thinreports/thinreports-generator/blob/master/examples/report_callbacks/report_callbacks.rb.
177
202
  def events
203
+ warn '[DEPRECATION] `Report::Base#events` will be removed in the next major version. ' +
204
+ 'Please use #on_page_create callbacks instead. ' +
205
+ 'See also https://github.com/thinreports/thinreports-generator/blob/master/examples/report_callbacks/report_callbacks.rb.'
178
206
  internal.events
179
207
  end
180
-
181
- # @return [ThinReports::Core::Page, nil]
182
- def page
183
- internal.page
184
- end
185
-
186
- # @return [Integer]
187
- def page_count
188
- internal.page_count
189
- end
190
-
191
- # @return [void]
192
- # @private
193
- def finalize
194
- internal.finalize
195
- end
196
-
197
- # @return [Boolean]
198
- # @private
199
- def finalized?
200
- internal.finalized?
201
- end
208
+
209
+ def_delegators :internal, :finalize, :finalized?
202
210
  end
203
-
211
+
204
212
  end
205
213
  end
@@ -1,6 +1,6 @@
1
1
  # coding: utf-8
2
2
 
3
- module ThinReports
3
+ module Thinreports
4
4
  module Report
5
5
 
6
6
  class Events < Core::Events
@@ -9,14 +9,14 @@ module ThinReports
9
9
  :generate)
10
10
  end
11
11
 
12
- class Event < ThinReports::Core::Events::Event
13
- # @return [ThinReports::Core::Page, nil]
12
+ class Event < Thinreports::Core::Events::Event
13
+ # @return [Thinreports::Report::Page, nil]
14
14
  attr_reader :page
15
15
 
16
- # @return [Array<ThinReports::Core::Page>]
16
+ # @return [Array<Thinreports::Report::Page>]
17
17
  attr_reader :pages
18
18
 
19
- # @return [ThinReports::Report::Base]
19
+ # @return [Thinreports::Report::Base]
20
20
  alias_method :report, :target
21
21
 
22
22
  def initialize(type, target, page = nil, pages = nil)
@@ -1,9 +1,8 @@
1
1
  # coding: utf-8
2
2
 
3
- module ThinReports
3
+ module Thinreports
4
4
  module Report
5
-
6
- # @private
5
+
7
6
  class Internal
8
7
  attr_reader :pages
9
8
  attr_reader :page
@@ -11,9 +10,12 @@ module ThinReports
11
10
  attr_reader :default_layout
12
11
  attr_reader :layout_registry
13
12
  attr_reader :events
14
-
15
- # @param [ThinReports::Report::Base] report
16
- # @param options (see ThinReports::Report::Base#initialize)
13
+
14
+ attr_accessor :page_create_handler
15
+ attr_accessor :generate_handler
16
+
17
+ # @param [Thinreports::Report::Base] report
18
+ # @param options (see Thinreports::Report::Base#initialize)
17
19
  def initialize(report, options)
18
20
  @report = report
19
21
  # Default layout
@@ -24,17 +26,20 @@ module ThinReports
24
26
  @pages = []
25
27
  @page = nil
26
28
  @page_count = 0
27
-
29
+
30
+ @page_create_handler = nil
31
+ @generate_handler = nil
32
+
28
33
  @events = Report::Events.new
29
34
  end
30
-
31
- # @see ThinReports::Report::Base#use_layout
35
+
36
+ # @see Thinreports::Report::Base#use_layout
32
37
  def register_layout(layout, options = {}, &block)
33
38
  layout = if options.empty? || options[:default]
34
39
  @default_layout = init_layout(layout)
35
40
  else
36
41
  id = options[:id].to_sym
37
-
42
+
38
43
  if layout_registry.key?(id)
39
44
  raise ArgumentError, "Id :#{id} is already in use."
40
45
  end
@@ -42,87 +47,92 @@ module ThinReports
42
47
  end
43
48
  layout.config(&block)
44
49
  end
45
-
50
+
46
51
  def add_page(new_page)
47
52
  finalize_current_page
48
53
  insert_page(new_page)
49
54
  end
50
-
55
+
51
56
  def copy_page
52
- finalize_current_page(:at => :copy)
57
+ finalize_current_page(at: :copy)
53
58
  insert_page(page.copy)
54
59
  end
55
-
60
+
56
61
  def finalize
57
62
  unless finalized?
58
63
  finalize_current_page
59
64
  @finalized = true
60
-
61
- # Dispatch event on before generate.
65
+
66
+ # [DEPRECATION] Please use Report::Base#on_generate callback instead.
62
67
  events.dispatch(Report::Events::Event.new(:generate,
63
68
  @report, nil, pages))
69
+
70
+ @generate_handler.call(pages) if @generate_handler
64
71
  end
65
72
  end
66
-
73
+
67
74
  def finalized?
68
75
  @finalized
69
76
  end
70
-
71
- def load_layout(layout)
72
- return @default_layout if layout.nil?
73
-
74
- case layout
77
+
78
+ def load_layout(id_or_filename)
79
+ return @default_layout if id_or_filename.nil?
80
+
81
+ layout = case id_or_filename
75
82
  when Symbol
76
- layout_registry[layout]
83
+ layout_registry[id_or_filename]
77
84
  when String
78
- prepare_layout(layout)
85
+ prepare_layout(id_or_filename)
79
86
  else
80
87
  raise ArgumentError, 'Invalid argument for layout.'
81
88
  end
89
+ @default_layout = layout unless @default_layout
90
+ layout
82
91
  end
83
-
92
+
84
93
  private
85
-
94
+
86
95
  def insert_page(new_page)
87
96
  @pages << new_page
88
-
97
+
89
98
  if new_page.count?
90
99
  @page_count += 1
91
100
  new_page.no = @page_count
92
101
  end
93
- # Dispatch event on +before+ page create.
94
- # But do not dispatch if page is blank-page.
102
+
95
103
  unless new_page.blank?
104
+ # [DEPRECATION] Please use #on_page_create callback instead.
96
105
  events.dispatch(Report::Events::Event.new(:page_create,
97
106
  @report,
98
107
  new_page))
108
+ @page_create_handler.call(new_page) if @page_create_handler
99
109
  end
100
110
  @page = new_page
101
111
  end
102
-
103
- # @param (see ThinReports::Core::Page#finalize)
112
+
113
+ # @param (see Thinreports::Report::Page#finalize)
104
114
  def finalize_current_page(options = {})
105
- page.finalize(options) unless page.blank?
115
+ page.finalize(options) unless page.nil? || page.blank?
106
116
  end
107
-
117
+
108
118
  def prepare_layout(layout)
109
119
  return nil if layout.nil?
110
-
120
+
111
121
  case layout
112
122
  when String
113
123
  init_layout(layout)
114
124
  # @note Currently not used. Since 0.6.0?
115
- when ThinReports::Layout::Base
125
+ when Thinreports::Layout::Base
116
126
  layout
117
127
  else
118
128
  raise ArgumentError, 'Invalid argument for layout.'
119
129
  end
120
130
  end
121
-
131
+
122
132
  def init_layout(filename, id = nil)
123
- ThinReports::Layout.new(filename, :id => id)
133
+ Thinreports::Layout.new(filename, id: id)
124
134
  end
125
135
  end
126
-
136
+
127
137
  end
128
138
  end
@@ -1,91 +1,101 @@
1
1
  # coding: utf-8
2
2
 
3
- module ThinReports
4
- module Core
5
-
3
+ module Thinreports
4
+ module Report
5
+
6
6
  class BlankPage
7
+ # @example
8
+ # 3.times do
9
+ # page = report.start_new_page
10
+ # puts page.no
11
+ # end
12
+ # # => 1, 2, 3
7
13
  # @return [Integer]
8
14
  attr_accessor :no
9
-
15
+
10
16
  # @param [Boolean] count (nil)
11
17
  def initialize(count = nil)
12
18
  @count = count.nil? ? true : count
13
19
  end
14
-
20
+
21
+ # @example
22
+ # page = report.start_new_page
23
+ # page.count? # => true
24
+ #
25
+ # page = report.start_new_page count: false
26
+ # page.count? # => false
15
27
  # @return [Boolean]
16
28
  def count?
17
29
  @count
18
30
  end
19
-
31
+
20
32
  # @return [Boolean] (true)
21
33
  def blank?
22
34
  true
23
35
  end
24
36
  end
25
-
37
+
26
38
  class Page < BlankPage
27
39
  include Core::Shape::Manager::Target
28
-
29
- # @return [ThinReports::Report::Base]
30
- # @private
40
+
41
+ # @return [Thinreports::Report::Base]
31
42
  attr_reader :report
32
-
33
- # @return [ThinReports::Layout::Base]
43
+
44
+ # @return [Thinreports::Layout::Base]
34
45
  attr_reader :layout
35
-
36
- # @param [ThinReports::Report::Base] report
37
- # @param [ThinReports::Layout::Base] layout
38
- def initialize(report, layout)
39
- super(true)
40
-
46
+
47
+ # @param [Thinreports::Report::Base] report
48
+ # @param [Thinreports::Layout::Base] layout
49
+ # @param [Hash] options ({})
50
+ # @option options [Boolean] :count (true)
51
+ def initialize(report, layout, options = {})
52
+ super(options.key?(:count) ? options[:count] : true)
53
+
41
54
  @report = report
42
55
  @layout = layout
43
56
  @finalized = false
44
-
57
+
45
58
  initialize_manager(layout.format) do |f|
46
59
  Core::Shape::Interface(self, f)
47
60
  end
48
61
  end
49
-
62
+
50
63
  # @return [Boolean] (false)
51
64
  def blank?
52
65
  false
53
66
  end
54
-
55
- # @private
67
+
56
68
  def copy
57
- new_page = self.class.new(report, layout)
58
-
69
+ new_page = self.class.new(report, layout, count: count?)
70
+
59
71
  manager.shapes.each do |id, shape|
60
72
  new_shape = shape.copy(new_page)
61
73
  new_page.manager.shapes[id] = new_shape
62
-
74
+
63
75
  if new_shape.internal.type_of?(:list)
64
76
  new_page.manager.lists[id] = new_shape
65
77
  end
66
78
  end
67
79
  new_page
68
80
  end
69
-
81
+
70
82
  # @param [Hash] options
71
83
  # @option options [:create, :copy] :at (:create)
72
- # @private
73
84
  def finalize(options = {})
74
85
  at = options[:at] || :create
75
-
86
+
76
87
  # For list shapes.
77
88
  if at == :create
78
89
  manager.lists.values.each {|list| list.manager.finalize }
79
90
  end
80
-
91
+
81
92
  @finalized = true
82
93
  end
83
-
84
- # @private
94
+
85
95
  def finalized?
86
96
  @finalized
87
97
  end
88
98
  end
89
-
99
+
90
100
  end
91
101
  end
@@ -1,31 +1,27 @@
1
1
  # coding: utf-8
2
2
 
3
- module ThinReports
4
-
3
+ module Thinreports
4
+
5
5
  module Report
6
- # @see ThinReports::Report::Base#initialize
6
+ # @see Thinreports::Report::Base#initialize
7
7
  def self.new(*args)
8
8
  Base.new(*args)
9
9
  end
10
-
11
- # @see ThinReports::Report::Base#create
10
+
11
+ # @see Thinreports::Report::Base#create
12
12
  def self.create(*args, &block)
13
13
  Base.create(*args, &block)
14
14
  end
15
-
16
- # @see ThinReports::Report::Base#generate
15
+
16
+ # @see Thinreports::Report::Base#generate
17
17
  def self.generate(*args, &block)
18
18
  Base.generate(*args, &block)
19
19
  end
20
-
21
- # @see ThinReports::Report::Base#generate_file
22
- def self.generate_file(*args, &block)
23
- Base.generate_file(*args, &block)
24
- end
25
20
  end
26
-
21
+
27
22
  end
28
23
 
29
24
  require 'thinreports/report/base'
30
25
  require 'thinreports/report/internal'
31
- require 'thinreports/report/events'
26
+ require 'thinreports/report/events'
27
+ require 'thinreports/report/page'