trackoid 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/trackoid/aggregates.rb +7 -4
- data/lib/trackoid/tracker.rb +10 -4
- data/spec/aggregates_spec.rb +12 -5
- data/trackoid.gemspec +2 -2
- metadata +4 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.5
|
data/lib/trackoid/aggregates.rb
CHANGED
@@ -109,9 +109,9 @@ module Mongoid #:nodoc:
|
|
109
109
|
def foreign_class_defined?
|
110
110
|
# The following construct doesn't work with namespaced constants.
|
111
111
|
# Object.const_defined?(internal_aggregates_name.to_sym)
|
112
|
-
|
113
|
-
|
114
|
-
|
112
|
+
internal_aggregates_name.constantize && true
|
113
|
+
rescue NameError
|
114
|
+
false
|
115
115
|
end
|
116
116
|
|
117
117
|
# Adds the aggregate field to the array of aggregated fields.
|
@@ -123,7 +123,10 @@ module Mongoid #:nodoc:
|
|
123
123
|
# the original class model but with "Aggregates" appended.
|
124
124
|
# Example: TestModel ==> TestModelAggregates
|
125
125
|
def define_klass(&block)
|
126
|
-
|
126
|
+
scope = internal_aggregates_name.split('::')
|
127
|
+
klass = scope.pop
|
128
|
+
scope = scope.inject(Kernel) {|scope, const_name| scope.const_get(const_name)}
|
129
|
+
klass = scope.const_set(klass, Class.new)
|
127
130
|
klass.class_eval(&block)
|
128
131
|
end
|
129
132
|
|
data/lib/trackoid/tracker.rb
CHANGED
@@ -35,16 +35,13 @@ module Mongoid #:nodoc:
|
|
35
35
|
def add(how_much = 1, date = DateTime.now)
|
36
36
|
raise Errors::ModelNotSaved, "Can't update a new record" if @owner.new_record?
|
37
37
|
update_data(data_for(date) + how_much, date)
|
38
|
-
|
39
38
|
@owner.collection.update( @owner._selector,
|
40
39
|
{ (how_much > 0 ? "$inc" : "$dec") => update_hash(how_much.abs, date) },
|
41
40
|
:upsert => true)
|
42
|
-
|
43
41
|
return unless @owner.aggregated?
|
44
42
|
|
45
43
|
@owner.aggregate_fields.each do |(k,v)|
|
46
44
|
next unless token = v.call(@aggregate_data)
|
47
|
-
|
48
45
|
fk = @owner.class.name.to_s.foreign_key.to_sym
|
49
46
|
selector = { fk => @owner.id, :ns => k, :key => token.to_s }
|
50
47
|
@owner.aggregate_klass.collection.update( selector,
|
@@ -63,11 +60,20 @@ module Mongoid #:nodoc:
|
|
63
60
|
|
64
61
|
def set(how_much, date = DateTime.now)
|
65
62
|
raise Errors::ModelNotSaved, "Can't update a new record" if @owner.new_record?
|
66
|
-
|
67
63
|
update_data(how_much, date)
|
68
64
|
@owner.collection.update( @owner._selector,
|
69
65
|
{ "$set" => update_hash(how_much, date) },
|
70
66
|
:upsert => true)
|
67
|
+
return unless @owner.aggregated?
|
68
|
+
|
69
|
+
@owner.aggregate_fields.each do |(k,v)|
|
70
|
+
next unless token = v.call(@aggregate_data)
|
71
|
+
fk = @owner.class.name.to_s.foreign_key.to_sym
|
72
|
+
selector = { fk => @owner.id, :ns => k, :key => token.to_s }
|
73
|
+
@owner.aggregate_klass.collection.update( selector,
|
74
|
+
{ "$set" => update_hash(how_much, date) },
|
75
|
+
:upsert => true)
|
76
|
+
end
|
71
77
|
end
|
72
78
|
|
73
79
|
|
data/spec/aggregates_spec.rb
CHANGED
@@ -209,21 +209,28 @@ describe Mongoid::Tracking::Aggregates do
|
|
209
209
|
@mock.visits.browsers.today.inject(0) {|total, c| total + c.last }.should == 2
|
210
210
|
end
|
211
211
|
|
212
|
+
it "should work also with set" do
|
213
|
+
@mock.visits("Google Chrome").set(5)
|
214
|
+
@mock.visits.browsers.today.should == [["mozilla", 1], ["google", 5]]
|
215
|
+
@mock.visits.referers.today.should == [["firefox", 1], ["chrome", 5]]
|
216
|
+
@mock.visits.today.should == 5
|
217
|
+
end
|
218
|
+
|
212
219
|
it "let's chek what happens when sorting the best browser..." do
|
213
220
|
@mock.visits("Google Chrome").inc
|
214
|
-
@mock.visits.browsers.today.should == [["mozilla", 1], ["google",
|
215
|
-
@mock.visits.browsers.today.max {|a,b| a.second <=> b.second }.should == ["google",
|
221
|
+
@mock.visits.browsers.today.should == [["mozilla", 1], ["google", 6]]
|
222
|
+
@mock.visits.browsers.today.max {|a,b| a.second <=> b.second }.should == ["google", 6]
|
216
223
|
end
|
217
224
|
|
218
225
|
it "should work without aggregation information" do
|
219
226
|
@mock.visits.inc
|
220
|
-
@mock.visits.browsers.today.should == [["mozilla", 1], ["google",
|
221
|
-
@mock.visits.referers.today.should == [["firefox", 1], ["chrome",
|
227
|
+
@mock.visits.browsers.today.should == [["mozilla", 1], ["google", 6]]
|
228
|
+
@mock.visits.referers.today.should == [["firefox", 1], ["chrome", 6]]
|
222
229
|
|
223
230
|
# A more throughout test would check totals...
|
224
231
|
visits_today = @mock.visits.today
|
225
232
|
visits_today_with_browser = @mock.visits.browsers.today.inject(0) {|total, c| total + c.last }
|
226
|
-
visits_today.should == visits_today_with_browser
|
233
|
+
visits_today.should == visits_today_with_browser
|
227
234
|
end
|
228
235
|
|
229
236
|
end
|
data/trackoid.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{trackoid}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.5"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Jose Miguel Perez"]
|
12
|
-
s.date = %q{2010-07-
|
12
|
+
s.date = %q{2010-07-15}
|
13
13
|
s.description = %q{Trackoid uses an embeddable approach to track analytics data using the poweful features of MongoDB for scalability}
|
14
14
|
s.email = %q{josemiguel@perezruiz.com}
|
15
15
|
s.extra_rdoc_files = [
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: trackoid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 17
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 5
|
10
|
+
version: 0.1.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jose Miguel Perez
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-07-
|
18
|
+
date: 2010-07-15 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|