dm-observers 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/AUTHORS +1 -0
- data/LICENSE +20 -0
- data/README +126 -0
- data/Rakefile +53 -0
- data/TODO +2 -0
- metadata +69 -0
data/AUTHORS
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Carlos A. Paramio (carlos@evolve.st)
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Carlos A. Paramio (carlos@evolve.st)
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
This is a DataMapper plugin that provides observers for DataMapper resource
|
2
|
+
classes. It's compatible with DataMapper 0.9.1
|
3
|
+
|
4
|
+
== Setup
|
5
|
+
DataMapper observer capabilities are automatically available when you
|
6
|
+
'require dm-observers' into your application.
|
7
|
+
|
8
|
+
== Basic Usage
|
9
|
+
|
10
|
+
require 'dm-observers'
|
11
|
+
|
12
|
+
DataMapper.setup(:default, 'sqlite3::memory:')
|
13
|
+
|
14
|
+
class MyModel
|
15
|
+
include DataMapper::Resource
|
16
|
+
include DataMapper::Observers::Observable
|
17
|
+
property :id, Integer, :key => true
|
18
|
+
|
19
|
+
def my_instance_method
|
20
|
+
puts "my_instance_method called"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class MyObserver < DataMapper::Observers::Observer
|
25
|
+
|
26
|
+
before :save do
|
27
|
+
puts "before :save block..."
|
28
|
+
end
|
29
|
+
|
30
|
+
after :destroy do
|
31
|
+
puts "after :destroy block..."
|
32
|
+
end
|
33
|
+
|
34
|
+
before :my_instance_method do
|
35
|
+
puts "before :my_instance_method block..."
|
36
|
+
# ...
|
37
|
+
end
|
38
|
+
|
39
|
+
before :my_instance_method do |object|
|
40
|
+
puts "before :my_instance_method block with object parameter #{object.inspect}..."
|
41
|
+
#...
|
42
|
+
end
|
43
|
+
|
44
|
+
# ...
|
45
|
+
end
|
46
|
+
|
47
|
+
MyObserver.instance.observed_class = MyModel
|
48
|
+
|
49
|
+
MyModel.auto_migrate!
|
50
|
+
object = MyModel.new
|
51
|
+
|
52
|
+
object.my_instance_method
|
53
|
+
---- output ----
|
54
|
+
before :my_instance_method block...
|
55
|
+
before :my_instance_method block with object parameter #<MyModel id=nil>...
|
56
|
+
my_instance_method called
|
57
|
+
---- output ----
|
58
|
+
|
59
|
+
object.save
|
60
|
+
---- output ----
|
61
|
+
before :save block...
|
62
|
+
---- output ----
|
63
|
+
|
64
|
+
object.destroy
|
65
|
+
---- output ----
|
66
|
+
after :destroy block...
|
67
|
+
---- output ----
|
68
|
+
|
69
|
+
|
70
|
+
The methods "before" and "after" take a target method parameter -- the name of the method at
|
71
|
+
the model to observe -- and a block -- the code to execute before/after the target method is
|
72
|
+
executed. The syntax is pretty similar to model hooks, just that it doesn't support class methods
|
73
|
+
or method symbols yet.
|
74
|
+
|
75
|
+
The block supports a parameter, that will be filled with the object instance that sent the
|
76
|
+
update notification to the observers.
|
77
|
+
|
78
|
+
|
79
|
+
== Observers for DataMapper Resources
|
80
|
+
|
81
|
+
The observers defined for DataMapper Resources can be automatically added to the model,
|
82
|
+
if the observer class name is prefixed with the name of the model to observe, and suffixed
|
83
|
+
with 'Observer'. Example:
|
84
|
+
|
85
|
+
class MyModel
|
86
|
+
# ...
|
87
|
+
end
|
88
|
+
|
89
|
+
class MyModelObserver
|
90
|
+
# ...
|
91
|
+
end
|
92
|
+
|
93
|
+
MyModelObserver.instance # The observer is automatically added to the MyModel class.
|
94
|
+
|
95
|
+
|
96
|
+
You can include all the observers instanciations at some bootstrap block of your project.
|
97
|
+
|
98
|
+
|
99
|
+
== Custom messages to the observer
|
100
|
+
|
101
|
+
Additionally to the default hooks called on the observers, you can send a custom message
|
102
|
+
to them using the notify_observers method:
|
103
|
+
|
104
|
+
class MyModel
|
105
|
+
include DataMapper::Resource
|
106
|
+
include DataMapper::Observers::Observable
|
107
|
+
property :id, Integer, :key => true
|
108
|
+
|
109
|
+
def do_something
|
110
|
+
# ...
|
111
|
+
MyModel.notify_observers(:purge_old_data, self)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
class MyModelObserver < DataMapper::Observers::Observer
|
116
|
+
def purge_old_data(object)
|
117
|
+
puts "Purge Old Data..."
|
118
|
+
# ...
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
MyModelObserver.instance
|
123
|
+
MyModel.new.do_something
|
124
|
+
---- output ----
|
125
|
+
Purge Old Data...
|
126
|
+
---- output ----
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'spec'
|
3
|
+
require 'rake/clean'
|
4
|
+
require 'rake/rdoctask'
|
5
|
+
require 'rake/gempackagetask'
|
6
|
+
require 'spec/rake/spectask'
|
7
|
+
require 'pathname'
|
8
|
+
|
9
|
+
CLEAN.include '{log,pkg}/'
|
10
|
+
|
11
|
+
desc "Generate Documentation"
|
12
|
+
rd = Rake::RDocTask.new do |rdoc|
|
13
|
+
rdoc.rdoc_dir = 'doc'
|
14
|
+
rdoc.title = "DataMapper Observers"
|
15
|
+
rdoc.options << '--line-numbers' << '--inline-source' << '--main' << 'README'
|
16
|
+
rdoc.rdoc_files.include(FileList[ 'lib/**/*.rb', 'README', 'LICENSE'])
|
17
|
+
end
|
18
|
+
|
19
|
+
spec = Gem::Specification.new do |s|
|
20
|
+
s.name = 'dm-observers'
|
21
|
+
s.version = '0.9.1'
|
22
|
+
s.platform = Gem::Platform::RUBY
|
23
|
+
s.has_rdoc = true
|
24
|
+
s.extra_rdoc_files = %w[ README LICENSE TODO ]
|
25
|
+
s.summary = 'DataMapper plugin for performing observers for data models'
|
26
|
+
s.description = s.summary
|
27
|
+
s.author = 'Carlos A. Paramio'
|
28
|
+
s.email = 'carlosparamio@gmail.com'
|
29
|
+
s.homepage = 'http://github.com/carlosparamio/dm-observers/tree/master'
|
30
|
+
s.require_path = 'lib'
|
31
|
+
s.files = FileList[ '{lib,spec}/**/*.rb', 'spec/spec.opts', 'Rakefile', *s.extra_rdoc_files ]
|
32
|
+
s.add_dependency('dm-core', "=#{s.version}")
|
33
|
+
end
|
34
|
+
|
35
|
+
task :default => [ :spec ]
|
36
|
+
|
37
|
+
WIN32 = (RUBY_PLATFORM =~ /win32|mingw|cygwin/) rescue nil
|
38
|
+
SUDO = WIN32 ? '' : ('sudo' unless ENV['SUDOLESS'])
|
39
|
+
|
40
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
41
|
+
pkg.gem_spec = spec
|
42
|
+
end
|
43
|
+
|
44
|
+
desc "Install #{spec.name} #{spec.version}"
|
45
|
+
task :install => [ :package ] do
|
46
|
+
sh "#{SUDO} gem install pkg/#{spec.name}-#{spec.version} --no-update-sources", :verbose => false
|
47
|
+
end
|
48
|
+
|
49
|
+
desc 'Run specifications'
|
50
|
+
Spec::Rake::SpecTask.new(:spec) do |t|
|
51
|
+
t.spec_opts << '--options' << 'spec/spec.opts' if File.exists?('spec/spec.opts')
|
52
|
+
t.spec_files = Pathname.glob(Pathname.new(__FILE__).dirname + 'spec/**/*_spec.rb')
|
53
|
+
end
|
data/TODO
ADDED
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dm-observers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Carlos Paramio
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-10-31 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: dm-core
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.9.1
|
24
|
+
version:
|
25
|
+
description: This is a DataMapper plugin that provides observers for DataMapper resource classes. It's compatible with DataMapper 0.9.1.
|
26
|
+
email: carlosparamio@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files: []
|
32
|
+
|
33
|
+
files:
|
34
|
+
- AUTHORS
|
35
|
+
- LICENSE
|
36
|
+
- README
|
37
|
+
- Rakefile
|
38
|
+
- TODO
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: http://github.com/carlosparamio/dm-observers/
|
41
|
+
licenses: []
|
42
|
+
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options:
|
45
|
+
- --main
|
46
|
+
- README
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
version:
|
61
|
+
requirements: []
|
62
|
+
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 1.3.5
|
65
|
+
signing_key:
|
66
|
+
specification_version: 3
|
67
|
+
summary: DataMapper Observers gem
|
68
|
+
test_files: []
|
69
|
+
|