Class: Jreport::Assembler

Inherits:
Object
  • Object
show all
Defined in:
lib/jreport/assembler.rb

Instance Method Summary collapse

Constructor Details

#initialize(report_name) ⇒ Assembler

Returns a new instance of Assembler.



8
9
10
11
12
# File 'lib/jreport/assembler.rb', line 8

def initialize(report_name)
  @root=`pwd`.chomp
  @_report=report_name
  @report=report_name.split('_').map{|x| x.capitalize}.join
end

Instance Method Details

#fetch_dataObject



13
14
15
16
17
# File 'lib/jreport/assembler.rb', line 13

def fetch_data
  collector="#{@report}Collector"
  raise "Can't find #{collector}" unless Kernel.const_get(collector)
  Kernel.const_get(collector).new.fetch_data
end

#make_reportObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/jreport/assembler.rb', line 18

def make_report
  controller="#{@report}Controller"
  ctrl_klas=Kernel.const_get(controller)
  mtds=ctrl_klas.instance_methods(false).grep /_report$/
  mtds.each do |m|
    begin
      ctrl=ctrl_klas.new
      class << ctrl
        attr_accessor :data,:save_to
      end
      ctrl.data=fetch_data
      # initialize a mail object
      mail=Mail.new
      ctrl.send m,mail
      dir="#{@root}/views/#{@_report}"
      html=render_html(dir,m.to_s,:data=>ctrl.data,:attachments=>mail.attachments)
    File.open(ctrl.save_to,'w'){|fi| fi.write(html) } if ctrl.save_to
      # send out report
      if mail.html_part
          mail.html_part.body=html
      else
          mail.body=html
      end
	  send_mail(mail)
    rescue=>e
      puts e
      puts e.backtrace
    end
  end
  
end

#render_html(dir, report_name, bindings) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/jreport/assembler.rb', line 50

def render_html(dir,report_name,bindings)
  base="#{dir}/#{report_name}"
  html=Erubis::Eruby.new(File.read("#{base}.eruby")).result(bindings)
  if File.exist?("#{base}.css")
css=File.read "#{base}.css"
		p=Premailer.new(html,:with_html_string=>true,:css_string=>css)
html=p.to_inline_css
  end
  html
end

#send_mail(mail) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/jreport/assembler.rb', line 60

def send_mail(mail)
	lost=mail.from.nil? ? 'from' : mail.to.nil? ? 'to' : mail.subject.nil? ? 'subject' : nil
	if  lost
		puts "Mail '#{lost}' empty, mail wouldn't be sent!"
		return
	end
	mail.deliver!
	puts "Mail sent!"
end