lib/thinreports/report/internal.rb in thinreports-0.8.2 vs lib/thinreports/report/internal.rb in thinreports-0.9.0

- old
+ new

@@ -7,46 +7,40 @@ attr_reader :pages attr_reader :page attr_reader :page_count attr_reader :default_layout attr_reader :layout_registry - attr_reader :events attr_accessor :page_create_handler # @param [Thinreports::Report::Base] report # @param options (see Thinreports::Report::Base#initialize) def initialize(report, options) @report = report - # Default layout - @default_layout = prepare_layout(options[:layout]) + @default_layout = options[:layout] ? init_layout(options[:layout]) : nil @layout_registry = {} @finalized = false @pages = [] @page = nil @page_count = 0 @page_create_handler = nil - - @events = Report::Events.new end # @see Thinreports::Report::Base#use_layout - def register_layout(layout, options = {}, &block) - layout = - if options.empty? || options[:default] - @default_layout = init_layout(layout) - else - id = options[:id].to_sym + def register_layout(layout, options = {}) + if options.empty? || options[:default] + @default_layout = init_layout(layout) + else + id = options[:id].to_sym - if layout_registry.key?(id) - raise ArgumentError, "Id :#{id} is already in use." - end - layout_registry[id] = init_layout(layout, id) + if layout_registry.key?(id) + raise ArgumentError, "Id :#{id} is already in use." end - layout.config(&block) + layout_registry[id] = init_layout(layout, id) + end end def add_page(new_page) finalize_current_page insert_page(new_page) @@ -59,27 +53,10 @@ def finalize unless finalized? finalize_current_page @finalized = true - - # [DEPRECATION] You can do the same implements as the :generate Event - # with the following code: - # - # report = Thinreports::Report.new layout: 'foo.tlf' - # report.start_new_page do |page| - # page.item(:price).value = 100000 - # end - # - # report.pages.each do |page| - # page[:title] = 'Common Title' - # end - # - # report.generate filename: 'foo.pdf' - # - events.dispatch(Report::Events::Event.new(:generate, - @report, nil, pages)) end end def finalized? @finalized @@ -91,14 +68,15 @@ layout = case id_or_filename when Symbol layout_registry[id_or_filename] when String - prepare_layout(id_or_filename) + init_layout(id_or_filename) else raise ArgumentError, 'Invalid argument for layout.' end + @default_layout = layout unless @default_layout layout end private @@ -109,36 +87,16 @@ if new_page.count? @page_count += 1 new_page.no = @page_count end - unless new_page.blank? - # [DEPRECATION] Please use #on_page_create callback instead. - events.dispatch(Report::Events::Event.new(:page_create, - @report, - new_page)) - @page_create_handler.call(new_page) if @page_create_handler - end + @page_create_handler.call(new_page) if !new_page.blank? && @page_create_handler @page = new_page end # @param (see Thinreports::Report::Page#finalize) def finalize_current_page(options = {}) page.finalize(options) unless page.nil? || page.blank? - end - - def prepare_layout(layout) - return nil if layout.nil? - - case layout - when String - init_layout(layout) - # @note Currently not used. Since 0.6.0? - when Thinreports::Layout::Base - layout - else - raise ArgumentError, 'Invalid argument for layout.' - end end def init_layout(filename, id = nil) Thinreports::Layout.new(filename, id: id) end