active_record_events 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7901cc7d702e85765dceb49f23b89df43cac4160
4
+ data.tar.gz: b2a511841b70dd3c2a6845bcb14995ea59c555bc
5
+ SHA512:
6
+ metadata.gz: b440f82b33a8991f2f0c924c4f94a42837221c8166e52c8097fc2b834c2af766c30dfde926b0564b6559950af92bb229f7ccab795a57c3de0137e8ae58b6bd6c
7
+ data.tar.gz: b245036f59aac3662a0c6d041ae338588a681e054558522f401aed39c8f3acd0d5a7068b9e2f671f7d1be49a3ed818cbfbf5cfe0ce494bbfeb0bdb972f801ee7
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_record_events.gemspec
4
+ gemspec
5
+
6
+ gem "event_emitter", git: "git@github.com:teleborder/event_emitter.git"
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Trey Griffith
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # ActiveRecordEvents
2
+
3
+ Listen to `create`, `update`, and `destroy` events for your ActiveRecord models.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'active_record_events'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install active_record_events
18
+
19
+ ## Usage
20
+
21
+ include in ActiveRecord model
22
+
23
+ ```ruby
24
+ class User < ActiveRecord::Base
25
+ include ActiveRecordEvents
26
+
27
+ # your model logic
28
+ end
29
+ ```
30
+
31
+ set up a listener on the Class
32
+
33
+ ```ruby
34
+ User.instance_on :create do
35
+ puts self.name # writes the user's name property to stdout
36
+ end
37
+ ```
38
+
39
+ or on the model itself
40
+
41
+ ```ruby
42
+ user = User.new
43
+ user.on :create do
44
+ puts self.name # writes the user's name property to stdout
45
+ end
46
+ ```
47
+
48
+ and trigger the hook
49
+
50
+ ```ruby
51
+ user.save!
52
+ ```
53
+
54
+ ## Contributing
55
+
56
+ 1. Fork it ( https://github.com/[my-github-username]/active_record_events/fork )
57
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
58
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
59
+ 4. Push to the branch (`git push origin my-new-feature`)
60
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_record_events/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_record_events"
8
+ spec.version = ActiveRecordEvents::VERSION
9
+ spec.authors = ["Trey Griffith"]
10
+ spec.email = ["trey.griffith@gmail.com"]
11
+ spec.summary = %q{Listen to lifecycle events for ActiveRecord models}
12
+ spec.description = %q{Broadcasts lifecycle events (create, update, destroy) for ActiveRecord models}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activesupport"
22
+
23
+ spec.add_development_dependency "activerecord"
24
+ spec.add_development_dependency "sqlite3"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "bundler", "~> 1.6"
27
+ spec.add_development_dependency "rake"
28
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveRecordEvents
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ require "active_record_events/version"
2
+ require "active_support/concern"
3
+ require "event_emitter"
4
+
5
+ module ActiveRecordEvents
6
+ extend ActiveSupport::Concern
7
+ include EventEmitter
8
+
9
+ def after_create
10
+ self.emit(:create)
11
+ end
12
+
13
+ def after_update
14
+ self.emit(:update)
15
+ end
16
+
17
+ def after_destroy
18
+ self.emit(:destroy)
19
+ end
20
+
21
+ included do
22
+ after_create :after_create
23
+ after_update :after_update
24
+ after_destroy :after_destroy
25
+ end
26
+ end
@@ -0,0 +1,58 @@
1
+ require "spec_helper"
2
+
3
+ describe "ActiveRecordEvents" do
4
+
5
+ before do
6
+ @user = User.new
7
+ end
8
+
9
+ it "calls the block in the contex of the ActiveRecord instance" do
10
+ user = nil
11
+ User.instance_on(:create) do
12
+ user = self
13
+ end
14
+
15
+ @user.save!
16
+
17
+ expect(user).to eq @user
18
+ end
19
+
20
+ it "calls listeners on model create" do
21
+ name = nil
22
+ @user.name = "hello"
23
+ User.instance_on(:create) do
24
+ name = self.name
25
+ end
26
+
27
+ @user.save!
28
+
29
+ expect(name).to eq @user.name
30
+ end
31
+
32
+ it "calls listeners on model update" do
33
+ name = nil
34
+ @user.name = "hello"
35
+ User.instance_on(:update) do
36
+ name = self.name
37
+ end
38
+
39
+ @user.save!
40
+ @user.name = "goodbye"
41
+ @user.save!
42
+
43
+ expect(name).to eq @user.name
44
+ end
45
+
46
+ it "calls listeners on model destroy" do
47
+ name = nil
48
+ @user.name = "hello"
49
+ User.instance_on(:destroy) do
50
+ name = self.name
51
+ end
52
+
53
+ @user.destroy!
54
+
55
+ expect(name).to eq @user.name
56
+ end
57
+
58
+ end
data/spec/models.rb ADDED
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ include ActiveRecordEvents
3
+ end
data/spec/schema.rb ADDED
@@ -0,0 +1,12 @@
1
+ ActiveRecord::Schema.define do
2
+ self.verbose = false
3
+
4
+ create_table :users, :force => true do |t|
5
+ t.string :key
6
+ t.string :name
7
+ t.integer :age
8
+ t.datetime :dob
9
+
10
+ t.timestamps
11
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ require "bundler/setup"
2
+ Bundler.setup
3
+
4
+ require "active_record_events"
5
+ require "active_record"
6
+
7
+ ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
8
+
9
+ load File.dirname(__FILE__) + '/schema.rb'
10
+ require File.dirname(__FILE__) + '/models.rb'
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record_events
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Trey Griffith
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.6'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.6'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Broadcasts lifecycle events (create, update, destroy) for ActiveRecord
98
+ models
99
+ email:
100
+ - trey.griffith@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - active_record_events.gemspec
111
+ - lib/active_record_events.rb
112
+ - lib/active_record_events/version.rb
113
+ - spec/active_record_events_spec.rb
114
+ - spec/models.rb
115
+ - spec/schema.rb
116
+ - spec/spec_helper.rb
117
+ homepage: ''
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.2.2
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: Listen to lifecycle events for ActiveRecord models
141
+ test_files:
142
+ - spec/active_record_events_spec.rb
143
+ - spec/models.rb
144
+ - spec/schema.rb
145
+ - spec/spec_helper.rb