Class: Metry::Storage
- Inherits:
-
Object
show all
- Includes:
- XGen::Mongo::Driver
- Defined in:
- lib/metry/storage.rb
Defined Under Namespace
Classes: PredictableKeyFactory
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(dbname) ⇒ Storage
Returns a new instance of Storage.
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/metry/storage.rb', line 21
def initialize(dbname)
@dbname = dbname
options = {}
@key_factory = nil
if self.class.predictable_keys?
@key_factory = PredictableKeyFactory.new
options[:pk] = @key_factory
end
@db = Mongo.new('localhost').db(dbname, options)
end
|
Instance Attribute Details
#dbname ⇒ Object
Returns the value of attribute dbname.
19
20
21
|
# File 'lib/metry/storage.rb', line 19
def dbname
@dbname
end
|
Class Method Details
.predictable_keys=(value) ⇒ Object
13
14
15
16
|
# File 'lib/metry/storage.rb', line 13
def predictable_keys=(value)
@predictable_keys = value
Metry.init Metry.current.dbname if Metry.current
end
|
.predictable_keys? ⇒ Boolean
9
10
11
|
# File 'lib/metry/storage.rb', line 9
def predictable_keys?
@predictable_keys
end
|
Instance Method Details
#add_event(event) ⇒ Object
52
53
54
|
# File 'lib/metry/storage.rb', line 52
def add_event(event)
@db.collection('events') << event
end
|
#add_goal(goal) ⇒ Object
76
77
78
|
# File 'lib/metry/storage.rb', line 76
def add_goal(goal)
goal(@db.collection('goals').insert(goal))
end
|
#all_events(*find_options) ⇒ Object
72
73
74
|
# File 'lib/metry/storage.rb', line 72
def all_events(*find_options)
@db.collection('events').find(*find_options).to_a
end
|
#clear ⇒ Object
92
93
94
95
|
# File 'lib/metry/storage.rb', line 92
def clear
%w(visitors events goals).each{|e| @db.drop_collection(e)}
@key_factory.clear if @key_factory
end
|
#event(id) ⇒ Object
56
57
58
|
# File 'lib/metry/storage.rb', line 56
def event(id)
@db.collection('events').find(prep_selector('_id' => id)).next_object
end
|
#event_count ⇒ Object
60
61
62
|
# File 'lib/metry/storage.rb', line 60
def event_count
@db.collection('events').count
end
|
#events_for(visitor) ⇒ Object
64
65
66
|
# File 'lib/metry/storage.rb', line 64
def events_for(visitor)
@db.collection('events').find('visitor' => visitor)
end
|
#goal(id) ⇒ Object
84
85
86
|
# File 'lib/metry/storage.rb', line 84
def goal(id)
@db.collection('goals').find(prep_selector('_id' => id)).next_object
end
|
#goals ⇒ Object
80
81
82
|
# File 'lib/metry/storage.rb', line 80
def goals
@db.collection('goals').find.to_a
end
|
#last_events(count = 1) ⇒ Object
68
69
70
|
# File 'lib/metry/storage.rb', line 68
def last_events(count=1)
@db.collection('events').find({}, :limit => count, :sort => {'time' => 1}).to_a
end
|
#new_visitor ⇒ Object
44
45
46
|
# File 'lib/metry/storage.rb', line 44
def new_visitor
visitor(@db.collection('visitors').insert({}))
end
|
#save_goal(goal) ⇒ Object
88
89
90
|
# File 'lib/metry/storage.rb', line 88
def save_goal(goal)
@db.collection('goals').repsert(prep_selector('_id' => goal['_id']), goal)
end
|
#save_visitor(visitor) ⇒ Object
48
49
50
|
# File 'lib/metry/storage.rb', line 48
def save_visitor(visitor)
@db.collection('visitors').repsert(prep_selector('_id' => visitor["_id"]), visitor)
end
|
#visitor(id) ⇒ Object
32
33
34
|
# File 'lib/metry/storage.rb', line 32
def visitor(id)
@db.collection('visitors').find(prep_selector('_id' => id)).next_object
end
|
#visitor_count ⇒ Object
36
37
38
|
# File 'lib/metry/storage.rb', line 36
def visitor_count
@db.collection('visitors').count
end
|
#visitors ⇒ Object
40
41
42
|
# File 'lib/metry/storage.rb', line 40
def visitors
@db.collection('visitors').find
end
|