Class: Thinreports::BasicReport::Report::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Utils
Defined in:
lib/thinreports/basic_report/report/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

#blank_value?, #call_block_in, #deep_copy, included

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :layout (String, nil) — default: nil


72
73
74
75
# File 'lib/thinreports/basic_report/report/base.rb', line 72

def initialize(options = {})
  @internal = Report::Internal.new(self, options)
  @start_page_number = 1
end

Instance Attribute Details

#internalThinreports::BasicReport::Layout::Base (readonly)



53
54
55
# File 'lib/thinreports/basic_report/report/base.rb', line 53

def internal
  @internal
end

#start_page_numberInteger (readonly)

Returns:

  • (Integer)


56
57
58
# File 'lib/thinreports/basic_report/report/base.rb', line 56

def start_page_number
  @start_page_number
end

Class Method Details

.create(options = {}) {|report| ... } ⇒ Thinreports::BasicReport::Report::Base

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :layout (String, nil) — default: nil

Yields:

  • (report)

Yield Parameters:

Returns:

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
# File 'lib/thinreports/basic_report/report/base.rb', line 16

def create(options = {}, &block)
  raise ArgumentError, '#create requires a block' unless block_given?

  report = new(options)
  call_block_in(report, &block)
  report.finalize

  report
end

.generate(layout: nil, filename: nil, security: nil, title: nil, report: {}, generator: {}) {|report| ... } ⇒ String

Parameters:

  • report (Hash) (defaults to: {})

    ({}) DEPRECATED. Options for Report.

  • generator (Hash) (defaults to: {})

    ({}) DEPRECATED. Options for Generator.

  • filename (String) (defaults to: nil)
  • security (Hash) (defaults to: nil)
  • title (String) (defaults to: nil)

    Value of the title attribute of the PDF document metadata. if nil, the title of the default layout file is set.

Yields:

  • (report)

Yield Parameters:

Returns:

  • (String)

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/thinreports/basic_report/report/base.rb', line 35

def generate(layout: nil, filename: nil, security: nil, title: nil, report: {}, generator: {}, &block)
  raise ArgumentError, '#generate requires a block' unless block_given?

  if report.any? || generator.any?
    warn '[DEPRECATION] :report and :generator argument has been deprecated. ' \
         'Use :layout and :filename, :security argument instead.'
  end

  layout ||= report[:layout]
  filename ||= generator[:filename]
  security ||= generator[:security]

  report = create(layout: layout, &block)
  report.generate(filename: filename, security: security, title: title)
end

Instance Method Details

#add_blank_page(options = {}) ⇒ Thinreports::BasicReport::Report::BlankPage Also known as: blank_page

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :count (Boolean) — default: true

Returns:



135
136
137
# File 'lib/thinreports/basic_report/report/base.rb', line 135

def add_blank_page(options = {})
  internal.add_page(Report::BlankPage.new(options[:count]))
end

#generate(filename: nil, security: nil, title: nil) ⇒ String

Examples:

Generate PDF data

report.generate # => "%PDF-1.4...."

Create a PDF file

report.generate(filename: 'foo.pdf')

Parameters:

Returns:

  • (String)


160
161
162
# File 'lib/thinreports/basic_report/report/base.rb', line 160

def generate(filename: nil, security: nil, title: nil)
  Thinreports::BasicReport::Generator::PDF.new(self, security: security, title: title).generate(filename)
end

#layout(id = nil) ⇒ Thinreports::BasicReport::Layout::Base

Parameters:

  • id (Symbol, nil) (defaults to: nil)

Returns:



142
143
144
145
146
147
148
149
# File 'lib/thinreports/basic_report/report/base.rb', line 142

def layout(id = nil)
  if id
    internal.layout_registry[id] ||
      raise(Thinreports::BasicReport::Errors::UnknownLayoutId)
  else
    internal.default_layout
  end
end

#list(id = nil, &block) ⇒ Object



165
166
167
168
# File 'lib/thinreports/basic_report/report/base.rb', line 165

def list(id = nil, &block)
  start_new_page if page.nil? || page.finalized?
  page.list(id, &block)
end

#on_page_create {|page| ... } ⇒ Object

Examples:

report.on_page_create do |page|
  page.item(:header_title).value = 'Title'
end

Yields:

  • (page)

Yield Parameters:



83
84
85
# File 'lib/thinreports/basic_report/report/base.rb', line 83

def on_page_create(&block)
  internal.page_create_handler = block
end

#start_new_page(options = {}) {|page| ... } ⇒ Thinreports::BasicReport::Report::Page

Examples:

page = report.start_new_page

report.start_new_page do |page|
  page.item(:text).value = 'value'
end

report.use_layout 'foo.tlf', default: true
report.use_layout 'bar.tlf', id: :bar

report.start_new_page                   # Use 'foo.tlf'
report.start_new_page layout: :bar      # Use 'bar.tlf'
report.start_new_page layout: 'boo.tlf' # Use 'boo.tlf'

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :layout (String, Symbol) — default: nil
  • :count (Boolean) — default: true

Yields:

  • (page)

Yield Parameters:

Returns:

Raises:



123
124
125
126
127
128
129
130
# File 'lib/thinreports/basic_report/report/base.rb', line 123

def start_new_page(options = {}, &block)
  layout = internal.load_layout(options.delete(:layout))

  raise Thinreports::BasicReport::Errors::NoRegisteredLayoutFound unless layout

  page = internal.add_page(Report::Page.new(self, layout, options))
  call_block_in(page, &block)
end

#start_page_number_from(page_number) ⇒ Object

Parameters:

  • page_number (Integer)


88
89
90
# File 'lib/thinreports/basic_report/report/base.rb', line 88

def start_page_number_from(page_number)
  @start_page_number = page_number
end

#use_layout(layout, options = {}) ⇒ Object

Examples:

report.use_layout '/path/to/default_layout.tlf' # Default layout
report.use_layout '/path/to/default_layout.tlf', default: true
report.use_layout '/path/to/other_layout', id: :other_layout

Parameters:

  • layout (String)

    filename of layout file

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :default (Boolean) — default: true
  • :id (Symbol) — default: nil


100
101
102
# File 'lib/thinreports/basic_report/report/base.rb', line 100

def use_layout(layout, options = {})
  internal.register_layout(layout, options)
end