Class: Ruport::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/ruport/report.rb,
lib/ruport/report/mailer.rb

Defined Under Namespace

Classes: Mailer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_name = :default, mailer_name = :default) ⇒ Report

Returns a new instance of Report.



15
16
17
18
19
20
21
22
23
24
# File 'lib/ruport/report.rb', line 15

def initialize( source_name=:default, mailer_name=:default )
  
  @source = source_name
  
  if Ruport::Config.mailers[mailer_name]
    @mailer      = Mailer.new(mailer_name)
  end
  @report_name = @report = ""
  @file        = nil
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



26
27
28
# File 'lib/ruport/report.rb', line 26

def file
  @file
end

#mailerObject (readonly)

Returns the value of attribute mailer.



27
28
29
# File 'lib/ruport/report.rb', line 27

def mailer
  @mailer
end

Instance Method Details

#eval_template(filename, code) ⇒ Object

Evaluates code from filename as pure ruby code for files ending in .rb, and as ERb templates for anything else.



52
53
54
55
56
57
58
# File 'lib/ruport/report.rb', line 52

def eval_template( filename, code )
  if filename =~ /\.rb/
    eval(code)
  else
    ERB.new(code, 0, "%").run(binding)
  end
end

#generate_reportObject

Generates the report. If @pre or @post are defined with lambdas, they will be called before and after the main code.

If @file != nil, ruport will print to the file with the specified name. Otherwise, it will print to STDOUT by default.

Lastly, if you have your mailer configuration set up and you’ve specified recipients, the contents of @mailer.body will be automatically emailed by this fuction.

The source for this function is probably easier to read than this explanation, so you may want to start there.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ruport/report.rb', line 74

def generate_report
  @pre.call if @pre
  if (@file.nil?) 
    puts(@report)
  else 
    File.open(@file,"w") { |f| f.puts @report }
  end
  unless @mailer.nil? || @mailer.recipients.empty?
    @mailer.body = @report if @mailer.body.empty?
    @mailer.send_report(@report_name) 
  end
  @post.call if @post

end

#query(sql, options = {}, &action) ⇒ Object

High level interface to Ruport::Query

  • Can read SQL statements from file or string

  • Can use multistatement SQL

  • Can iterate by row or return entire set

  • Can return raw DBI:Row objects or Ruport constructs.

Defaults to returning entire sets of Ruport constructs.

See source of this function and methods of Ruport::Query for details.



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ruport/report.rb', line 38

def query(sql, options={}, &action)
  options[:origin] ||= :string
  options[:source] ||= @source

  q = Query.new(sql, options)
  if options[:yield_type].eql?(:by_row)
    q.each { |r| action.call(r) }
  else
    block_given? ? action.call(q.result) : q.result
  end
end

#render(string, options) ⇒ Object

Provides a nice way to execute templates and filters.

Example:

my_report.render( "_<%= @some_internal_var %>_", 
                  :filters => [:erb,:red_cloth] )

This method automatically passes a binding into the filters, so you are free to access data from your Report instance in your templates.



103
104
105
106
107
108
109
110
# File 'lib/ruport/report.rb', line 103

def render(string, options)
  options[:filters].each do |f|
    format = Format.new(binding)
    format.content = string
    string = format.send("filter_#{f}")
  end
  string
end

#use_source(label) ⇒ Object

sets the active source to the Ruport::Config source requested by label.



90
91
92
# File 'lib/ruport/report.rb', line 90

def use_source(label)
  @source = label
end