242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
# File 'lib/opinions.rb', line 242
def initialize(*args)
super
self.class.registered_opinions.each do |opinion|
self.class.send :define_method, :"#{opinion}" do |*args|
target, time = *args
time = time || Time.now.utc
e = Opinion.new(object: self, target: target, opinion: opinion)
true & e.persist(time: time)
end
self.class.send :define_method, :"cancel_#{opinion}" do |pollable|
true & Opinion.new(object: self, target: pollable, opinion: opinion).remove
end
self.class.send :define_method, :"have_#{opinion}_on" do |pollable|
send("#{opinion}_opinions").collect { |o| o.target == pollable }.any?
end
self.class.send :define_method, :"#{opinion}_opinions" do
lookup_key_builder = KeyBuilder.new(object: self, opinion: opinion)
keys = Opinions.backend.keys_matching(lookup_key_builder.key + "*")
keys.collect do |key_name|
OpinionFactory.new(from_object: key_name).opinion
end.flatten
end
end
end
|