This repository has been archived by the owner on Aug 30, 2019. It is now read-only.
/
extra.rb
81 lines (66 loc) · 2.32 KB
/
extra.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
require 'mongo'
include Mongo
module Extra
# Extra is the main class in Extra! Extra! It's what gets returned from
# queries, and is the main interface to the entire system.
class Extra
attr_accessor :category,
:who_id,
:who_name,
:who_class,
:what,
:when
def initialize opts={}
self.category ||= opts['category']
self.who_id ||= opts['who_id']
self.who_name ||= opts['who_name']
self.who_class ||= opts['who_class']
self.what ||= opts['what']
self.when ||= opts['when']
end
# For now, this is just a simple sentence describing what happened.
# We'll see if something more complex makes sense later.
def to_s
"#{who_name} #{what}"
end
#returns the instantiated object of whodunnit
def who
Object.const_get(who_class).find(who_id)
end
class << self
attr_accessor :db, :collection
# The source method is used to set up the connection to MongoDB. Right now
# it has to be called by the user before doing anything else, which sucks.
# I'll probably add in something to make it autoconnect soon.
def source opts={}
opts[:host] ||= "localhost"
opts[:port] ||= 27017
self.db = Mongo::Connection.new(opts[:host], opts[:port]).db("extraextra")
self.collection = db["extras"]
end
# Extra::Extra.! is the method used to report what's going on. It takes a
# symbol for a category, the user who did whatever's newsworthy, and some
# text that describes what happened.
def !(category, user, text)
args = {
'category' => category,
'who_id' => user.id,
'who_name' => user.username,
'who_class' => user.class.to_s,
'what' => text,
'when' => Time.now.to_s
}
collection.insert args
Extra.new args
end
# This method returns all Extras that are associated with a given person.
def read_all_about_it user
collection.find(who_id: user.id,
who_class: user.class.to_s
).collect{|args| Extra.new args }
end
# aliasing a shorter name
alias :the_scoop :read_all_about_it
end
end
end