dm-observer 0.9.2

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/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Mark Bates
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,32 @@
1
+ README
2
+ ======
3
+ DataMapper::Observer allows you to add callback hooks to many models. This is
4
+ similar to observers in ActiveRecord.
5
+
6
+ Example:
7
+
8
+ class Adam
9
+ include DataMapper::Resource
10
+
11
+ property :id, Integer, :serial => true
12
+ property :name, String
13
+ end
14
+
15
+ class AdamObserver
16
+ include DataMapper::Observer
17
+
18
+ observe Adam
19
+
20
+ before :save do
21
+ # log message
22
+ end
23
+
24
+ before :get_drunk do
25
+ # eat something
26
+ end
27
+
28
+ after_class_method :unite do
29
+ raise "Call for help!"
30
+ end
31
+
32
+ end
data/Rakefile ADDED
@@ -0,0 +1,65 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require 'rake/clean'
4
+ require 'rake/gempackagetask'
5
+ require 'spec/rake/spectask'
6
+ require 'pathname'
7
+
8
+ CLEAN.include '{log,pkg}/'
9
+
10
+ spec = Gem::Specification.new do |s|
11
+ s.name = 'dm-observer'
12
+ s.version = '0.9.2'
13
+ s.platform = Gem::Platform::RUBY
14
+ s.has_rdoc = true
15
+ s.extra_rdoc_files = %w[ README LICENSE TODO ]
16
+ s.summary = 'DataMapper plugin for observing Resource Models'
17
+ s.description = s.summary
18
+ s.author = 'Mark Bates'
19
+ s.email = 'mark@mackframework.com'
20
+ s.homepage = 'http://github.com/sam/dm-more/tree/master/dm-observer'
21
+ s.require_path = 'lib'
22
+ s.files = FileList[ '{lib,spec}/**/*.rb', 'spec/spec.opts', 'Rakefile', *s.extra_rdoc_files ]
23
+ s.add_dependency('dm-core', "=#{s.version}")
24
+ end
25
+
26
+ task :default => [ :spec ]
27
+
28
+ WIN32 = (RUBY_PLATFORM =~ /win32|mingw|cygwin/) rescue nil
29
+ SUDO = WIN32 ? '' : ('sudo' unless ENV['SUDOLESS'])
30
+
31
+ Rake::GemPackageTask.new(spec) do |pkg|
32
+ pkg.gem_spec = spec
33
+ end
34
+
35
+ desc "Install #{spec.name} #{spec.version} (default ruby)"
36
+ task :install => [ :package ] do
37
+ sh "#{SUDO} gem install --local pkg/#{spec.name}-#{spec.version} --no-update-sources", :verbose => false
38
+ end
39
+
40
+ desc "Uninstall #{spec.name} #{spec.version} (default ruby)"
41
+ task :uninstall => [ :clobber ] do
42
+ sh "#{SUDO} gem uninstall #{spec.name} -v#{spec.version} -I -x", :verbose => false
43
+ end
44
+
45
+ namespace :jruby do
46
+ desc "Install #{spec.name} #{spec.version} with JRuby"
47
+ task :install => [ :package ] do
48
+ sh %{#{SUDO} jruby -S gem install --local pkg/#{spec.name}-#{spec.version} --no-update-sources}, :verbose => false
49
+ end
50
+ end
51
+
52
+ desc 'Run specifications'
53
+ Spec::Rake::SpecTask.new(:spec) do |t|
54
+ t.spec_opts << '--options' << 'spec/spec.opts' if File.exists?('spec/spec.opts')
55
+ t.spec_files = Pathname.glob(Pathname.new(__FILE__).dirname + 'spec/**/*_spec.rb')
56
+
57
+ begin
58
+ t.rcov = ENV.has_key?('NO_RCOV') ? ENV['NO_RCOV'] != 'true' : true
59
+ t.rcov_opts << '--exclude' << 'spec'
60
+ t.rcov_opts << '--text-summary'
61
+ t.rcov_opts << '--sort' << 'coverage' << '--sort-reverse'
62
+ rescue Exception
63
+ # rcov not installed
64
+ end
65
+ end
data/TODO ADDED
File without changes
@@ -0,0 +1,91 @@
1
+ module DataMapper
2
+ # Observers allow you to add callback hooks to DataMapper::Resource objects
3
+ # in a separate class. This is great for separating out logic that is not
4
+ # really part of the model, but needs to be triggered by a model, or models.
5
+ module Observer
6
+
7
+ def self.included(klass)
8
+ klass.extend(ClassMethods)
9
+ end
10
+
11
+ module ClassMethods
12
+
13
+ attr_accessor :observing
14
+
15
+ def initialize
16
+ self.observing = []
17
+ end
18
+
19
+ # Assign an Array of Class names to watch.
20
+ # observe User, Article, Topic
21
+ def observe(*args)
22
+ # puts "#{self.to_s} observing... #{args.collect{|c| Extlib::Inflection.classify(c.to_s)}.join(', ')}"
23
+ self.observing = args
24
+ end
25
+
26
+ def before(sym, &block)
27
+ self.observing.each do |klass|
28
+ klass.before(sym.to_sym, &block)
29
+ end
30
+ end
31
+
32
+ def after(sym, &block)
33
+ self.observing.each do |klass|
34
+ klass.after(sym.to_sym, &block)
35
+ end
36
+ end
37
+
38
+ def before_class_method(sym, &block)
39
+ self.observing.each do |klass|
40
+ klass.before_class_method(sym.to_sym, &block)
41
+ end
42
+ end
43
+
44
+ def after_class_method(sym, &block)
45
+ self.observing.each do |klass|
46
+ klass.after_class_method(sym.to_sym, &block)
47
+ end
48
+ end
49
+
50
+ end # ClassMethods
51
+
52
+ end # Observer
53
+ end # DataMapper
54
+
55
+ if $0 == __FILE__
56
+ require 'rubygems'
57
+
58
+ gem 'dm-core', '=0.9.2'
59
+ require 'dm-core'
60
+
61
+ FileUtils.touch(File.join(Dir.pwd, "migration_test.db"))
62
+ DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/migration_test.db")
63
+
64
+ class Foo
65
+ include DataMapper::Resource
66
+
67
+ property :id, Integer, :serial => true
68
+ property :bar, Text
69
+ end
70
+
71
+ Foo.auto_migrate!
72
+
73
+ class FooObserver
74
+ include DataMapper::Observer
75
+
76
+ observe :foo
77
+
78
+ before :save do
79
+ raise "Hell!" if self.bar.nil?
80
+ puts "hi"
81
+ end
82
+
83
+ after :save do
84
+ puts "bye"
85
+ end
86
+
87
+ end
88
+
89
+ Foo.new(:bar => "hello").save
90
+
91
+ end
@@ -0,0 +1,129 @@
1
+ require 'pathname'
2
+ require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
3
+
4
+ describe DataMapper::Observer do
5
+ before :all do
6
+ class Adam
7
+ include DataMapper::Resource
8
+
9
+ property :id, Integer, :serial => true
10
+ property :name, String
11
+ attr_accessor :done
12
+
13
+ def falling?
14
+ @falling
15
+ end
16
+
17
+ def dig_a_hole_to_china
18
+ @done = true
19
+ end
20
+
21
+ def drink
22
+ @happy = true
23
+ end
24
+
25
+ def happy?
26
+ @happy
27
+ end
28
+
29
+ def self.unite
30
+ [Adam.new(:name => "Adam 1"), Adam.new(:name => "Adam 2")]
31
+ end
32
+
33
+ end
34
+ Adam.auto_migrate!
35
+
36
+ module Alcohol
37
+ class Beer
38
+ include DataMapper::Resource
39
+
40
+ property :id, Integer, :serial => true
41
+ property :name, String
42
+
43
+ def drink
44
+ @empty = true
45
+ end
46
+
47
+ def empty?
48
+ @empty
49
+ end
50
+
51
+ end
52
+ end
53
+ Alcohol::Beer.auto_migrate!
54
+
55
+
56
+ class AdamObserver
57
+ include DataMapper::Observer
58
+
59
+ observe Adam
60
+
61
+ before :save do
62
+ @falling = true
63
+ end
64
+
65
+ before :dig_a_hole_to_china do
66
+ throw :halt
67
+ end
68
+
69
+ after_class_method :unite do
70
+ raise "Call for help!"
71
+ end
72
+
73
+ end
74
+
75
+ class DrinkingObserver
76
+ include DataMapper::Observer
77
+
78
+ observe Adam, Alcohol::Beer
79
+
80
+ after :drink do
81
+ @refrigerated = true
82
+ end
83
+
84
+ end
85
+
86
+ end
87
+
88
+ before(:each) do
89
+ @adam = Adam.new
90
+ @beer = Alcohol::Beer.new
91
+ end
92
+
93
+ it "should assign a callback" do
94
+ @adam.should_not be_falling
95
+ @adam.name = "Adam French"
96
+ @adam.save
97
+ @adam.should be_falling
98
+ end
99
+
100
+ it "should be able to trigger an abort" do
101
+ @adam.dig_a_hole_to_china
102
+ @adam.done.should be_nil
103
+ end
104
+
105
+ it "observe should add a class to the neighborhood watch" do
106
+ AdamObserver.should have(1).observing
107
+ AdamObserver.observing.first.should == Adam
108
+ end
109
+
110
+ it "observe should add more than one class to the neighborhood watch" do
111
+ DrinkingObserver.should have(2).observing
112
+ DrinkingObserver.observing.first.should == Adam
113
+ DrinkingObserver.observing[1].should == Alcohol::Beer
114
+ end
115
+
116
+ it "should observe multiple classes with the same method name" do
117
+ @adam.should_not be_happy
118
+ @beer.should_not be_empty
119
+ @adam.drink
120
+ @beer.drink
121
+ @adam.should be_happy
122
+ @beer.should be_empty
123
+ end
124
+
125
+ it "should wrap class methods" do
126
+ lambda {Adam.unite}.should raise_error('Call for help!')
127
+ end
128
+
129
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,2 @@
1
+ --format specdoc
2
+ --colour
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ gem 'rspec', '>=1.1.3'
3
+ require 'spec'
4
+ gem "dm-core"
5
+ require 'dm-core'
6
+ require 'pathname'
7
+ require Pathname(__FILE__).dirname.parent.expand_path + 'lib/dm-observer'
8
+
9
+ def load_driver(name, default_uri)
10
+ return false if ENV['ADAPTER'] != name.to_s
11
+
12
+ lib = "do_#{name}"
13
+
14
+ begin
15
+ gem lib, '=0.9.2'
16
+ require lib
17
+ DataMapper.setup(name, ENV["#{name.to_s.upcase}_SPEC_URI"] || default_uri)
18
+ DataMapper::Repository.adapters[:default] = DataMapper::Repository.adapters[name]
19
+ true
20
+ rescue Gem::LoadError => e
21
+ warn "Could not load #{lib}: #{e}"
22
+ false
23
+ end
24
+ end
25
+
26
+ ENV['ADAPTER'] ||= 'sqlite3'
27
+
28
+ HAS_SQLITE3 = load_driver(:sqlite3, 'sqlite3::memory:')
29
+ HAS_MYSQL = load_driver(:mysql, 'mysql://localhost/dm_core_test')
30
+ HAS_POSTGRES = load_driver(:postgres, 'postgres://postgres@localhost/dm_core_test')
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dm-observer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.2
5
+ platform: ruby
6
+ authors:
7
+ - Mark Bates
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-06-25 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: dm-core
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - "="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.9.2
23
+ version:
24
+ description: DataMapper plugin for observing Resource Models
25
+ email: mark@mackframework.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - README
32
+ - LICENSE
33
+ - TODO
34
+ files:
35
+ - lib/dm-observer.rb
36
+ - spec/integration/dm-observer_spec.rb
37
+ - spec/spec_helper.rb
38
+ - spec/spec.opts
39
+ - Rakefile
40
+ - README
41
+ - LICENSE
42
+ - TODO
43
+ has_rdoc: true
44
+ homepage: http://github.com/sam/dm-more/tree/master/dm-observer
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.0.1
66
+ signing_key:
67
+ specification_version: 2
68
+ summary: DataMapper plugin for observing Resource Models
69
+ test_files: []
70
+