event_watcher 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/event_watcher/watcher.rb +42 -0
- data/lib/event_watcher.rb +33 -0
- metadata +65 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
require "rubygems"
|
|
2
|
+
require "active_record"
|
|
3
|
+
|
|
4
|
+
module EventWatcher
|
|
5
|
+
class Watcher
|
|
6
|
+
include ActiveSupport::Inflector
|
|
7
|
+
attr_accessor :watched_method_name
|
|
8
|
+
alias :watched_callback_name :watched_method_name
|
|
9
|
+
|
|
10
|
+
@@watchers = {}
|
|
11
|
+
def self.find(key)
|
|
12
|
+
@@watchers[key]
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def initialize(attributes)
|
|
16
|
+
@watched_object_name = attributes[:object_name].to_s
|
|
17
|
+
@watched_method_name = attributes[:method_name].to_s
|
|
18
|
+
@callback = attributes[:callback]
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def key
|
|
22
|
+
"#{@watched_object_name}|#{@watched_method_name}"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def self.build(attributes)
|
|
26
|
+
watcher = Watcher.new(attributes)
|
|
27
|
+
@@watchers[watcher.key] = watcher
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def trigger_callback(*args)
|
|
31
|
+
@callback.call(*args)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def class_method_watcher?
|
|
35
|
+
@watched_object_name.camelize == @watched_object_name
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def klass
|
|
39
|
+
@watched_object_name.camelize.constantize
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
require 'event_watcher/watcher'
|
|
2
|
+
|
|
3
|
+
module EventWatcher
|
|
4
|
+
class << self
|
|
5
|
+
def method_call_watcher(object_name, method_name, &block)
|
|
6
|
+
watcher = Watcher.build :object_name => object_name, :method_name => method_name, :callback => block
|
|
7
|
+
code = <<-CODE
|
|
8
|
+
alias :old_method :#{watcher.watched_method_name}
|
|
9
|
+
def #{watcher.watched_method_name}(*args)
|
|
10
|
+
result = old_method(*args)
|
|
11
|
+
Watcher.find("#{watcher.key}").trigger_callback(self, args, result) rescue nil
|
|
12
|
+
result
|
|
13
|
+
end
|
|
14
|
+
CODE
|
|
15
|
+
|
|
16
|
+
code = "class << self; #{code}; end" if watcher.class_method_watcher?
|
|
17
|
+
watcher.klass.class_eval code
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def callback_watcher(class_name, callback_name, &block)
|
|
21
|
+
watcher = Watcher.build :object_name => class_name, :method_name => callback_name, :callback => block
|
|
22
|
+
watcher.klass.class_eval <<-CODE
|
|
23
|
+
after_create Proc.new {|resource| Watcher.find("#{watcher.key}").trigger_callback(resource) rescue nil }
|
|
24
|
+
CODE
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def register_watchers(&block)
|
|
28
|
+
module_eval &block
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
alias :setup :register_watchers
|
|
32
|
+
end
|
|
33
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: event_watcher
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
prerelease: false
|
|
5
|
+
segments:
|
|
6
|
+
- 0
|
|
7
|
+
- 0
|
|
8
|
+
- 1
|
|
9
|
+
version: 0.0.1
|
|
10
|
+
platform: ruby
|
|
11
|
+
authors:
|
|
12
|
+
- Juhyeong Yu
|
|
13
|
+
autorequire:
|
|
14
|
+
bindir: bin
|
|
15
|
+
cert_chain: []
|
|
16
|
+
|
|
17
|
+
date: 2011-10-21 00:00:00 -07:00
|
|
18
|
+
default_executable:
|
|
19
|
+
dependencies: []
|
|
20
|
+
|
|
21
|
+
description: This monitoring DSL will help writing flexible event loggers
|
|
22
|
+
email: zzurang@hotmail.com
|
|
23
|
+
executables: []
|
|
24
|
+
|
|
25
|
+
extensions: []
|
|
26
|
+
|
|
27
|
+
extra_rdoc_files: []
|
|
28
|
+
|
|
29
|
+
files:
|
|
30
|
+
- lib/event_watcher.rb
|
|
31
|
+
- lib/event_watcher/watcher.rb
|
|
32
|
+
has_rdoc: true
|
|
33
|
+
homepage: https://github.com/zzurang/event_watcher
|
|
34
|
+
licenses: []
|
|
35
|
+
|
|
36
|
+
post_install_message:
|
|
37
|
+
rdoc_options: []
|
|
38
|
+
|
|
39
|
+
require_paths:
|
|
40
|
+
- lib
|
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
42
|
+
none: false
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
segments:
|
|
47
|
+
- 0
|
|
48
|
+
version: "0"
|
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
50
|
+
none: false
|
|
51
|
+
requirements:
|
|
52
|
+
- - ">="
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
segments:
|
|
55
|
+
- 0
|
|
56
|
+
version: "0"
|
|
57
|
+
requirements: []
|
|
58
|
+
|
|
59
|
+
rubyforge_project:
|
|
60
|
+
rubygems_version: 1.3.7
|
|
61
|
+
signing_key:
|
|
62
|
+
specification_version: 3
|
|
63
|
+
summary: Simple DSL for monitoring class_methods, instance_methods and hooks calling history
|
|
64
|
+
test_files: []
|
|
65
|
+
|