ardm-observer 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 56959984d8ce88bb80a834ca2d622f77edb28f89
4
+ data.tar.gz: 0b9bb0dbf2ffab34da86c963330304901ca9cd4a
5
+ SHA512:
6
+ metadata.gz: cbd9516724bce83c2d4c5762792086fc22b270d5f81291802b5b855dfd1410480eac5e7795a59482ea14620d860c78575a7660e5e8dc16cf154bc3f0c7e72fc6
7
+ data.tar.gz: 503e28584e8cc62a923624b17ef79ec43f17fa0ca566c9ec20631312658d62af3c4a0fe28e04c0656cce78de0241f28bd74df733e49f20d2865204efa0e45cad
@@ -0,0 +1,35 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## Rubinius
17
+ *.rbc
18
+
19
+ ## PROJECT::GENERAL
20
+ *.gem
21
+ coverage
22
+ rdoc
23
+ pkg
24
+ tmp
25
+ doc
26
+ log
27
+ .yardoc
28
+ measurements
29
+
30
+ ## BUNDLER
31
+ .bundle
32
+ Gemfile.*
33
+
34
+ ## PROJECT::SPECIFIC
35
+ spec/db/
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ sudo: false
3
+ before_install:
4
+ - gem install bundler
5
+ script: "bundle exec rake spec"
6
+ env:
7
+ - "ADAPTER=in_memory"
8
+ rvm:
9
+ - 1.9.3
10
+ - 2.0.0
11
+ - 2.1.5
12
+ - 2.2.0
data/Gemfile ADDED
@@ -0,0 +1,55 @@
1
+ require 'pathname'
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
6
+
7
+ SOURCE = ENV.fetch('SOURCE', :git).to_sym
8
+ REPO_POSTFIX = SOURCE == :path ? '' : '.git'
9
+ DATAMAPPER = SOURCE == :path ? Pathname(__FILE__).dirname.parent : 'http://github.com/ar-dm'
10
+ DM_VERSION = '~> 1.2'
11
+ DO_VERSION = '~> 0.10.6'
12
+ DM_DO_ADAPTERS = %w[ sqlite postgres mysql oracle sqlserver ]
13
+ CURRENT_BRANCH = ENV.fetch('GIT_BRANCH', 'master')
14
+
15
+ gem 'ardm-core', DM_VERSION,
16
+ SOURCE => "#{DATAMAPPER}/ardm-core#{REPO_POSTFIX}",
17
+ :branch => CURRENT_BRANCH
18
+
19
+ group :datamapper do
20
+
21
+ adapters = ENV['ADAPTER'] || ENV['ADAPTERS']
22
+ adapters = adapters.to_s.tr(',', ' ').split.uniq - %w[ in_memory ]
23
+
24
+ if (do_adapters = DM_DO_ADAPTERS & adapters).any?
25
+ do_options = {}
26
+ do_options[:git] = "#{DATAMAPPER}/do#{REPO_POSTFIX}" if ENV['DO_GIT'] == 'true'
27
+
28
+ gem 'data_objects', DO_VERSION, do_options.dup
29
+
30
+ do_adapters.each do |adapter|
31
+ adapter = 'sqlite3' if adapter == 'sqlite'
32
+ gem "do_#{adapter}", DO_VERSION, do_options.dup
33
+ end
34
+
35
+ gem 'ardm-do-adapter', DM_VERSION,
36
+ SOURCE => "#{DATAMAPPER}/ardm-do-adapter#{REPO_POSTFIX}",
37
+ :branch => CURRENT_BRANCH
38
+ end
39
+
40
+ adapters.each do |adapter|
41
+ gem "ardm-#{adapter}-adapter", DM_VERSION,
42
+ SOURCE => "#{DATAMAPPER}/ardm-#{adapter}-adapter#{REPO_POSTFIX}",
43
+ :branch => CURRENT_BRANCH
44
+ end
45
+
46
+ plugins = ENV['PLUGINS'] || ENV['PLUGIN']
47
+ plugins = plugins.to_s.tr(',', ' ').split.push('ardm-migrations').uniq
48
+
49
+ plugins.each do |plugin|
50
+ gem plugin, DM_VERSION,
51
+ SOURCE => "#{DATAMAPPER}/#{plugin}#{REPO_POSTFIX}",
52
+ :branch => CURRENT_BRANCH
53
+ end
54
+
55
+ end
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 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.
@@ -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, Serial
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
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ FileList['tasks/**/*.rake'].each { |task| import task }
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/dm-observer/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = "ardm-observer"
6
+ gem.version = DataMapper::Observer::VERSION
7
+
8
+ gem.authors = [ "Martin Emde", "Mark Bates", "Dan Kubb" ]
9
+ gem.email = [ "me@martinemde.com", "dan.kubb@gmail.com" ]
10
+ gem.summary = "Ardm fork of dm-observer"
11
+ gem.description = "DataMapper plugin for observing Resources"
12
+ gem.homepage = "http://github.com/ar-dm/ardm-observer"
13
+
14
+ gem.files = `git ls-files`.split("\n")
15
+ gem.test_files = `git ls-files -- {spec}/*`.split("\n")
16
+ gem.extra_rdoc_files = %w[LICENSE README.rdoc]
17
+ gem.require_paths = [ "lib" ]
18
+
19
+ gem.add_runtime_dependency 'ardm-core', '~> 1.2'
20
+
21
+ gem.add_development_dependency 'rake', '~> 10.0'
22
+ gem.add_development_dependency 'rspec', '~> 2.0'
23
+ end
@@ -0,0 +1 @@
1
+ require 'dm-observer'
@@ -0,0 +1 @@
1
+ require 'dm-observer/observer'
@@ -0,0 +1,54 @@
1
+ require 'dm-core'
2
+
3
+ module DataMapper
4
+ # Observers allow you to add callback hooks to DataMapper::Resource objects
5
+ # in a separate class. This is great for separating out logic that is not
6
+ # really part of the model, but needs to be triggered by a model, or models.
7
+ module Observer
8
+
9
+ def self.included(klass)
10
+ klass.extend(ClassMethods)
11
+ end
12
+
13
+ module ClassMethods
14
+
15
+ attr_accessor :observing
16
+
17
+ def initialize
18
+ self.observing = []
19
+ end
20
+
21
+ # Assign an Array of Class names to watch.
22
+ # observe User, Article, Topic
23
+ def observe(*args)
24
+ self.observing = args
25
+ end
26
+
27
+ def before(sym, &block)
28
+ self.observing.each do |klass|
29
+ klass.before(sym.to_sym, &block)
30
+ end
31
+ end
32
+
33
+ def after(sym, &block)
34
+ self.observing.each do |klass|
35
+ klass.after(sym.to_sym, &block)
36
+ end
37
+ end
38
+
39
+ def before_class_method(sym, &block)
40
+ self.observing.each do |klass|
41
+ klass.before_class_method(sym.to_sym, &block)
42
+ end
43
+ end
44
+
45
+ def after_class_method(sym, &block)
46
+ self.observing.each do |klass|
47
+ klass.after_class_method(sym.to_sym, &block)
48
+ end
49
+ end
50
+
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,5 @@
1
+ module DataMapper
2
+ module Observer
3
+ VERSION = '1.2.0'
4
+ end
5
+ end
@@ -0,0 +1,130 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe DataMapper::Observer do
4
+ before :all do
5
+ class ::Adam
6
+ include DataMapper::Resource
7
+
8
+ property :id, Serial
9
+ property :name, String
10
+ attr_accessor :done
11
+
12
+ def falling?
13
+ @falling
14
+ end
15
+
16
+ def dig_a_hole_to_china
17
+ @done = true
18
+ end
19
+
20
+ def drink
21
+ @happy = true
22
+ end
23
+
24
+ def happy?
25
+ @happy
26
+ end
27
+
28
+ def self.unite
29
+ [Adam.new(:name => "Adam 1"), Adam.new(:name => "Adam 2")]
30
+ end
31
+
32
+ end
33
+
34
+ module ::Alcohol
35
+ class Beer
36
+ include DataMapper::Resource
37
+
38
+ property :id, Serial
39
+ property :name, String
40
+
41
+ def drink
42
+ @empty = true
43
+ end
44
+
45
+ def empty?
46
+ @empty
47
+ end
48
+
49
+ end
50
+ end
51
+
52
+
53
+ class ::AdamObserver
54
+ include DataMapper::Observer
55
+
56
+ observe Adam
57
+
58
+ before :save do
59
+ @falling = true
60
+ end
61
+
62
+ before :dig_a_hole_to_china do
63
+ throw :halt
64
+ end
65
+
66
+ after_class_method :unite do
67
+ raise "Call for help!"
68
+ end
69
+
70
+ end
71
+
72
+ class ::DrinkingObserver
73
+ include DataMapper::Observer
74
+
75
+ observe Adam, Alcohol::Beer
76
+
77
+ after :drink do
78
+ @refrigerated = true
79
+ end
80
+
81
+ end
82
+
83
+ end
84
+
85
+ before(:each) do
86
+ @adam = Adam.new
87
+ @beer = Alcohol::Beer.new
88
+ end
89
+
90
+ supported_by :all do
91
+
92
+ it "should assign a callback" do
93
+ @adam.should_not be_falling
94
+ @adam.name = "Adam French"
95
+ @adam.save
96
+ @adam.should be_falling
97
+ end
98
+
99
+ it "should be able to trigger an abort" do
100
+ @adam.dig_a_hole_to_china
101
+ @adam.done.should be_nil
102
+ end
103
+
104
+ it "observe should add a class to the neighborhood watch" do
105
+ AdamObserver.observing.size.should == 1
106
+ AdamObserver.observing.first.should == Adam
107
+ end
108
+
109
+ it "observe should add more than one class to the neighborhood watch" do
110
+ DrinkingObserver.observing.size.should == 2
111
+ DrinkingObserver.observing.first.should == Adam
112
+ DrinkingObserver.observing[1].should == Alcohol::Beer
113
+ end
114
+
115
+ it "should observe multiple classes with the same method name" do
116
+ @adam.should_not be_happy
117
+ @beer.should_not be_empty
118
+ @adam.drink
119
+ @beer.drink
120
+ @adam.should be_happy
121
+ @beer.should be_empty
122
+ end
123
+
124
+ it "should wrap class methods" do
125
+ lambda {Adam.unite}.should raise_error('Call for help!')
126
+ end
127
+
128
+ end
129
+
130
+ end
@@ -0,0 +1,11 @@
1
+ require 'rspec'
2
+
3
+ require 'dm-core/spec/setup'
4
+ require 'dm-core/spec/lib/adapter_helpers'
5
+
6
+ require 'dm-observer'
7
+ require 'dm-migrations'
8
+
9
+ RSpec.configure do |config|
10
+ config.extend(DataMapper::Spec::Adapters::Helpers)
11
+ end
@@ -0,0 +1,3 @@
1
+ require 'rspec/core/rake_task'
2
+ RSpec::Core::RakeTask.new(:spec)
3
+ task :default => :spec
@@ -0,0 +1,9 @@
1
+ begin
2
+ require 'yard'
3
+
4
+ YARD::Rake::YardocTask.new
5
+ rescue LoadError
6
+ task :yard do
7
+ abort 'YARD is not available. In order to run yard, you must: gem install yard'
8
+ end
9
+ end
@@ -0,0 +1,19 @@
1
+ begin
2
+ require 'pathname'
3
+ require 'yardstick/rake/measurement'
4
+ require 'yardstick/rake/verify'
5
+
6
+ # yardstick_measure task
7
+ Yardstick::Rake::Measurement.new
8
+
9
+ # verify_measurements task
10
+ Yardstick::Rake::Verify.new do |verify|
11
+ verify.threshold = 100
12
+ end
13
+ rescue LoadError
14
+ %w[ yardstick_measure verify_measurements ].each do |name|
15
+ task name.to_s do
16
+ abort "Yardstick is not available. In order to run #{name}, you must: gem install yardstick"
17
+ end
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ardm-observer
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Martin Emde
8
+ - Mark Bates
9
+ - Dan Kubb
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2015-03-06 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: ardm-core
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: '1.2'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: '1.2'
29
+ - !ruby/object:Gem::Dependency
30
+ name: rake
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - "~>"
34
+ - !ruby/object:Gem::Version
35
+ version: '10.0'
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: '10.0'
43
+ - !ruby/object:Gem::Dependency
44
+ name: rspec
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '2.0'
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - "~>"
55
+ - !ruby/object:Gem::Version
56
+ version: '2.0'
57
+ description: DataMapper plugin for observing Resources
58
+ email:
59
+ - me@martinemde.com
60
+ - dan.kubb@gmail.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files:
64
+ - LICENSE
65
+ - README.rdoc
66
+ files:
67
+ - ".gitignore"
68
+ - ".travis.yml"
69
+ - Gemfile
70
+ - LICENSE
71
+ - README.rdoc
72
+ - Rakefile
73
+ - ardm-observer.gemspec
74
+ - lib/ardm-observer.rb
75
+ - lib/dm-observer.rb
76
+ - lib/dm-observer/observer.rb
77
+ - lib/dm-observer/version.rb
78
+ - spec/integration/dm-observer_spec.rb
79
+ - spec/spec_helper.rb
80
+ - tasks/spec.rake
81
+ - tasks/yard.rake
82
+ - tasks/yardstick.rake
83
+ homepage: http://github.com/ar-dm/ardm-observer
84
+ licenses: []
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.4.5
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: Ardm fork of dm-observer
106
+ test_files: []
107
+ has_rdoc: