jzimmek-reportme 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -16,6 +16,8 @@ module Reportme
16
16
 
17
17
  @reports = []
18
18
  @since = since.to_date
19
+ @subscribtions = {}
20
+ @report_exists_cache = []
19
21
  end
20
22
 
21
23
  def connect
@@ -70,7 +72,15 @@ module Reportme
70
72
  end
71
73
 
72
74
  def report_exists?(name, von, bis)
73
- select_value("select 1 from #{report_information_table_name} where report = '#{name}' and von = '#{von}' and bis = '#{bis}'") != nil
75
+ key = "#{name}__#{von}__#{bis}"
76
+
77
+ return true if @report_exists_cache.include?(key)
78
+
79
+ exists = select_value("select 1 from #{report_information_table_name} where report = '#{name}' and von = '#{von}' and bis = '#{bis}'") != nil
80
+
81
+ @report_exists_cache << key if exists
82
+
83
+ exists
74
84
  end
75
85
 
76
86
  def schema_name
@@ -94,6 +104,7 @@ module Reportme
94
104
  end
95
105
 
96
106
  def reset
107
+ @report_exists_cache.clear
97
108
  exec("drop table if exists #{report_information_table_name};")
98
109
 
99
110
  ReportFactory.periods.each do |period|
@@ -103,10 +114,6 @@ module Reportme
103
114
  end
104
115
  end
105
116
 
106
- def week_data_present?(report, von, bis)
107
- puts "von: #{von} ... bis: #{bis}"
108
- end
109
-
110
117
  def ensure_report_table_exist(report, period)
111
118
  unless report.table_exist?(period)
112
119
  table_name = report.table_name(period)
@@ -161,6 +168,8 @@ module Reportme
161
168
  ActiveRecord::Base.transaction do
162
169
  exec("insert into #{report_information_table_name} values ('#{table_name}', '#{von}', '#{bis}', now());")
163
170
  exec("insert into #{table_name} select #{column_names.join(',')} from #{report.table_name(:day)} as d where d.von between '#{von}' and '#{(_von + num_days.days).strftime("%Y-%m-%d 00:00:00")}';")
171
+
172
+ notify_subscriber(report.name, period_name, _von)
164
173
  end
165
174
  end
166
175
  end
@@ -235,6 +244,8 @@ module Reportme
235
244
  end
236
245
 
237
246
  exec("insert into #{table_name} #{sql};")
247
+
248
+ notify_subscriber(r.name, period_name, _von)
238
249
  end
239
250
  end
240
251
 
@@ -263,6 +274,35 @@ module Reportme
263
274
  puts "------------------------ //"
264
275
  ActiveRecord::Base.connection.select_values(sql)
265
276
  end
277
+
278
+ def has_subscribtion?(report_name)
279
+ !@subscribtions[report_name].blank?
280
+ end
281
+
282
+ def has_report?(report_name)
283
+ !@reports.find{|r|r.name == report_name}.blank?
284
+ end
285
+
286
+ def subscribe(report_name, &block)
287
+ report_name = report_name.to_sym
288
+
289
+ raise "report: #{report_name} does not exist" unless has_report?(report_name)
290
+
291
+ existing = @subscribtions[report_name] || (@subscribtions[report_name] = [])
292
+ existing << block
293
+ end
294
+
295
+ def notify_subscriber(report_name, period, von)
296
+
297
+ (@subscribtions[report_name] || []).each do |subscription|
298
+ begin
299
+ subscription.call(period, von)
300
+ rescue Exception => e
301
+ puts e
302
+ end
303
+ end
304
+
305
+ end
266
306
 
267
307
  def report(name, &block)
268
308
 
@@ -353,5 +353,38 @@ class ReportmeTest < Test::Unit::TestCase
353
353
 
354
354
  end
355
355
 
356
+ should "probe existing reports" do
357
+ rme = create_visit_report_factory
358
+ assert rme.has_report?(:visits)
359
+ assert !rme.has_report?(:some_not_existing_report)
360
+ end
361
+
362
+ should "subscribe to visits report" do
363
+ rme = create_visit_report_factory
364
+ rme.subscribe :visits do
365
+ end
366
+ assert rme.has_subscribtion?(:visits)
367
+ end
368
+
369
+ should "fail on subscribtion to not existing report" do
370
+ rme = create_visit_report_factory
371
+ assert_raise RuntimeError do
372
+ rme.subscribe :some_not_existing_report do
373
+ end
374
+ end
375
+ end
376
+
377
+ should "notify subscriptions" do
378
+ notifed = false
379
+
380
+ rme = create_visit_report_factory
381
+ rme.subscribe :visits do
382
+ notifed = true
383
+ end
384
+
385
+ rme.notify_subscriber(:visits, :day, '2009-01-01'.to_datetime)
386
+
387
+ assert notifed
388
+ end
356
389
 
357
390
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jzimmek-reportme
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Zimmek