/
fires.rb
32 lines (28 loc) · 936 Bytes
/
fires.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
module TimelineFu
module Fires
def self.included(klass)
klass.send(:extend, ClassMethods)
end
class MissingOnArgument < ArgumentError
def initialize(options)
super "You did not provide the :on key in: #{options.inspect}"
end
end
module ClassMethods
def fires(event_type, opts)
raise MissingOnArgument.new(opts) unless opts.has_key?(:on)
method_name = :"fire_#{event_type}_after_#{opts[:on]}"
define_method(method_name) do
create_options = [:actor, :subject, :secondary_subject].inject({}) do |memo, sym|
memo[sym] = send(opts[sym]) if opts[sym]
memo
end
create_options[:subject] ||= self
create_options[:event_type] = event_type.to_s
TimelineEvent.create!(create_options)
end
send(:"after_#{opts[:on]}", method_name, :if => opts[:if])
end
end
end
end