lib/thinreports/report/base.rb in thinreports-0.9.1 vs lib/thinreports/report/base.rb in thinreports-0.10.0

- old
+ new

@@ -1,10 +1,9 @@ -# coding: utf-8 +# frozen_string_literal: true module Thinreports module Report - class Base extend Forwardable include Utils class << self @@ -12,57 +11,41 @@ # @option options (see #initialize) # @yield [report] # @yieldparam [Thinreports::Report::Base] report # @return [Thinreports::Report::Base] def create(options = {}, &block) - unless block_given? - raise ArgumentError, '#create requires a block' - end + raise ArgumentError, '#create requires a block' unless block_given? + report = new(options) call_block_in(report, &block) report.finalize report end - # @overload generate(type, options = {}, &block) - # @param [Symbol] type - # @param [Hash] options - # @option options [Hash] :report ({}) Options for Report. - # @option options [Hash] :generator ({}) Options for Generator. - # @overload generate(options = {}, &block) - # @param [Hash] options - # @option options [Hash] :report ({}) Options for Report. - # @option options [Hash] :generator ({}) Options for Generator. + # @param layout (see #initialize) + # @param filename (see #generate) + # @param security (see #generate) + # @param [Hash] report ({}) DEPRECATED. Options for Report. + # @param [Hash] generator ({}) DEPRECATED. Options for Generator. # @yield (see .create) # @yieldparam (see .create) # @return [String] - def generate(*args, &block) + def generate(layout: nil, filename: nil, security: nil, report: {}, generator: {}, &block) raise ArgumentError, '#generate requires a block' unless block_given? - report_opts, generator_opts = extract_options!(args) + if report.any? || generator.any? + warn '[DEPRECATION] :report and :generator argument has been deprecated. ' \ + 'Use :layout and :filename, :security argument instead.' + end - report = create(report_opts, &block) - report.generate(*args.push(generator_opts)) - end + layout ||= report[:layout] + filename ||= generator[:filename] + security ||= generator[:security] - # @param [Array] args - # @return [Array<Hash>] - def extract_options!(args) - if args.last.is_a?(::Hash) - options = args.pop - - generator = options.delete(:generator) || {} - report = options.delete(:report) || {} - - if options.key?(:layout) - report[:layout] = options.delete(:layout) - end - [report, generator.merge(options)] - else - [{}, {}] - end + report = create(layout: layout, &block) + report.generate(filename: filename, security: security) end end # @return [Thinreports::Report::Internal] attr_reader :internal @@ -148,11 +131,11 @@ # @option options [Boolean] :count (true) # @return [Thinreports::Report::BlankPage] def add_blank_page(options = {}) internal.add_page(Report::BlankPage.new(options[:count])) end - alias_method :blank_page, :add_blank_page + alias blank_page add_blank_page # @param [Symbol, nil] id # @return [Thinreports::Layout::Base] def layout(id = nil) if id @@ -161,36 +144,26 @@ else internal.default_layout end end - # @overload generate(options = {}) - # @param [Hash] options ({}) - # @return [String] - # @overload generate(type, options = {}) - # This way has been DEPRECATED. Use instead #generate(options = {}). - # @param [Symbol] type - # @return [String] + # @param [String] filename + # @param [Hash] security (see http://prawnpdf.org/api-docs/2.0/Prawn/Document/Security.html#encrypt_document-instance_method) + # @return [String] # @example Generate PDF data # report.generate # => "%PDF-1.4...." # @example Create a PDF file # report.generate(filename: 'foo.pdf') - def generate(*args) - options = args.last.is_a?(::Hash) ? args.pop : {} - type = args.first || Thinreports.config.generator.default - filename = options.delete(:filename) - generator = Thinreports::Generator.new(type, self, options) - - generator.generate(filename) + def generate(filename: nil, security: nil) + Thinreports::Generator::PDF.new(self, security: security).generate(filename) end # @see Thinreports::Core::Shape::Manager::Target#list def list(id = nil, &block) start_new_page if page.nil? || page.finalized? page.list(id, &block) end def_delegators :internal, :finalize, :finalized? end - end end