Class: ReportMe::ReportFactory
- Inherits:
-
Object
- Object
- ReportMe::ReportFactory
- Defined in:
- lib/report_me/report_factory.rb
Class Method Summary collapse
Instance Method Summary collapse
- #exec(sql) ⇒ Object
-
#initialize ⇒ ReportFactory
constructor
A new instance of ReportFactory.
- #periods(today = DateTime.now) ⇒ Object
- #report(name, &block) ⇒ Object
- #report_exists?(name, von, bis) ⇒ Boolean
- #report_information_table_name ⇒ Object
- #report_information_table_name_exist? ⇒ Boolean
- #reset ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize ⇒ ReportFactory
Returns a new instance of ReportFactory.
14 15 16 |
# File 'lib/report_me/report_factory.rb', line 14 def initialize @reports = [] end |
Class Method Details
.create(&block) ⇒ Object
7 8 9 10 11 12 |
# File 'lib/report_me/report_factory.rb', line 7 def self.create(&block) rme = ReportMe::ReportFactory.new rme.instance_eval(&block) rme.run rme end |
Instance Method Details
#exec(sql) ⇒ Object
139 140 141 |
# File 'lib/report_me/report_factory.rb', line 139 def exec(sql) ActiveRecord::Base.connection.execute(sql) end |
#periods(today = DateTime.now) ⇒ Object
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 |
# File 'lib/report_me/report_factory.rb', line 18 def periods(today = DateTime.now) periods = [] [:today, :day, :week, :calendar_week, :month, :calendar_month].each do |period| von, bis = case period when :today [today, today] when :day [today - 1.day, today - 1.day] when :week [today - 1.day - 1.week, today - 1.day] when :calendar_week n = today - 1.day [n - n.cwday + 1, n - n.cwday + 7] when :month [today - 1.day - 1.month, today - 1.day] when :calendar_month n = today - 1.month [n.beginning_of_month, n.end_of_month] end periods << {:name => period, :von => von, :bis => bis} end periods end |
#report(name, &block) ⇒ Object
143 144 145 146 147 148 |
# File 'lib/report_me/report_factory.rb', line 143 def report(name, &block) r = ReportMe::Report.new(name) r.instance_eval(&block) @reports << r end |
#report_exists?(name, von, bis) ⇒ Boolean
56 57 58 |
# File 'lib/report_me/report_factory.rb', line 56 def report_exists?(name, von, bis) ActiveRecord::Base.connection.select_value("select 1 from #{report_information_table_name} where report = '#{name}' and von = '#{von}' and bis = '#{bis}'") != nil end |
#report_information_table_name ⇒ Object
48 49 50 |
# File 'lib/report_me/report_factory.rb', line 48 def report_information_table_name "report_informations" end |
#report_information_table_name_exist? ⇒ Boolean
52 53 54 |
# File 'lib/report_me/report_factory.rb', line 52 def report_information_table_name_exist? ActiveRecord::Base.connection.select_value("show tables like '#{report_information_table_name}'") != nil end |
#reset ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/report_me/report_factory.rb', line 60 def reset exec("drop table if exists #{report_information_table_name};") periods.each do |period| @reports.each do |r| exec("drop table if exists #{r.table_name(period[:name])};") end end end |
#run ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/report_me/report_factory.rb', line 70 def run debug = false unless report_information_table_name_exist? ddl = <<-SQL create table report_informations ( report varchar(255) not null, von datetime not null, bis datetime not null, created_at datetime not null, primary key (report) ) ENGINE=InnoDB default CHARSET=utf8; SQL exec(ddl) end if debug # just for testing exec("truncate #{report_information_table_name};") end periods.each do |period| @reports.each do |r| von = period[:von].strftime("%Y-%m-%d 00:00:00") bis = period[:bis].strftime("%Y-%m-%d 23:59:59") table_name = r.table_name(period[:name]) if debug # drop and create table while in testing mode exec("drop table if exists #{table_name};") end table_exist = r.table_exist?(period[:name]) sql = r.sql(von, bis) report_exist = report_exists?(table_name, von, bis) puts "report: #{r.name}_#{period[:name]} :: #{report_exist}" unless table_exist exec("create table #{table_name} ENGINE=InnoDB default CHARSET=utf8 as #{sql} limit 0;") end # puts sql if !report_exist || period[:name] == :today ActiveRecord::Base.transaction do exec("insert into #{report_information_table_name} values ('#{table_name}', '#{von}', '#{bis}', now());") unless report_exist if period[:name] == :today exec("truncate #{table_name};") end exec("insert into #{table_name} #{sql};") end end end end end |