stance 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +2 -1
- data/Gemfile.lock +51 -48
- data/README.md +41 -1
- data/lib/stance.rb +2 -0
- data/lib/stance/active_record_callbacks.rb +18 -0
- data/lib/stance/active_record_events.rb +13 -0
- data/lib/stance/event.rb +11 -2
- data/lib/stance/version.rb +1 -1
- data/stance.gemspec +1 -1
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e5dcb8d2d1f3afe7451a48687ab8ce7c2f073b0dc9645cf958ecdb38c6673f2b
|
4
|
+
data.tar.gz: 756df16a830baf56ab25bcb4ee6f027f15c13ff9908516f0c1dae1b98f761b1a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a770f62d1a8e6d6c58f1f2643dc6bd5403f957bcfb350bc5696ceca50dde989462f45ff317754602735445bffb43797db7452110f604c3304f67b5ab10cb63c9
|
7
|
+
data.tar.gz: 850c0b54c13ea154307a9788e21e3670e42ec06fa17628682aa206b1cbb7de12a741d46689c783ca9a9dc43fbf02a212473fffce8ea1c41842393fa0be5720d1
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
stance (0.
|
4
|
+
stance (0.3.0)
|
5
5
|
activerecord (>= 5)
|
6
6
|
multi_json
|
7
7
|
railties (>= 5)
|
@@ -9,48 +9,48 @@ PATH
|
|
9
9
|
GEM
|
10
10
|
remote: https://rubygems.org/
|
11
11
|
specs:
|
12
|
-
actionpack (6.
|
13
|
-
actionview (= 6.
|
14
|
-
activesupport (= 6.
|
15
|
-
rack (~> 2.0, >= 2.0.
|
12
|
+
actionpack (6.1.1)
|
13
|
+
actionview (= 6.1.1)
|
14
|
+
activesupport (= 6.1.1)
|
15
|
+
rack (~> 2.0, >= 2.0.9)
|
16
16
|
rack-test (>= 0.6.3)
|
17
17
|
rails-dom-testing (~> 2.0)
|
18
18
|
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
19
|
-
actionview (6.
|
20
|
-
activesupport (= 6.
|
19
|
+
actionview (6.1.1)
|
20
|
+
activesupport (= 6.1.1)
|
21
21
|
builder (~> 3.1)
|
22
22
|
erubi (~> 1.4)
|
23
23
|
rails-dom-testing (~> 2.0)
|
24
24
|
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
25
|
-
activemodel (6.
|
26
|
-
activesupport (= 6.
|
27
|
-
activerecord (6.
|
28
|
-
activemodel (= 6.
|
29
|
-
activesupport (= 6.
|
30
|
-
activesupport (6.
|
25
|
+
activemodel (6.1.1)
|
26
|
+
activesupport (= 6.1.1)
|
27
|
+
activerecord (6.1.1)
|
28
|
+
activemodel (= 6.1.1)
|
29
|
+
activesupport (= 6.1.1)
|
30
|
+
activesupport (6.1.1)
|
31
31
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
32
|
-
i18n (>=
|
33
|
-
minitest (
|
34
|
-
tzinfo (~>
|
35
|
-
zeitwerk (~> 2.
|
32
|
+
i18n (>= 1.6, < 2)
|
33
|
+
minitest (>= 5.1)
|
34
|
+
tzinfo (~> 2.0)
|
35
|
+
zeitwerk (~> 2.3)
|
36
36
|
ast (2.4.1)
|
37
37
|
autotest-suffix (1.1.0)
|
38
38
|
builder (3.2.4)
|
39
|
-
combustion (1.3.
|
39
|
+
combustion (1.3.1)
|
40
40
|
activesupport (>= 3.0.0)
|
41
41
|
railties (>= 3.0.0)
|
42
42
|
thor (>= 0.14.6)
|
43
|
-
concurrent-ruby (1.1.
|
43
|
+
concurrent-ruby (1.1.8)
|
44
44
|
crass (1.0.6)
|
45
|
-
erubi (1.
|
46
|
-
i18n (1.8.
|
45
|
+
erubi (1.10.0)
|
46
|
+
i18n (1.8.7)
|
47
47
|
concurrent-ruby (~> 1.0)
|
48
|
-
loofah (2.
|
48
|
+
loofah (2.9.0)
|
49
49
|
crass (~> 1.0.2)
|
50
50
|
nokogiri (>= 1.5.9)
|
51
51
|
method_source (1.0.0)
|
52
|
-
mini_portile2 (2.
|
53
|
-
minitest (5.14.
|
52
|
+
mini_portile2 (2.5.0)
|
53
|
+
minitest (5.14.3)
|
54
54
|
minitest-autotest (1.1.1)
|
55
55
|
minitest-server (~> 1.0)
|
56
56
|
path_expander (~> 1.0)
|
@@ -58,13 +58,16 @@ GEM
|
|
58
58
|
minitest (>= 4, < 6)
|
59
59
|
minitest-server (1.0.6)
|
60
60
|
minitest (~> 5.0)
|
61
|
+
mocha (1.12.0)
|
61
62
|
multi_json (1.15.0)
|
62
|
-
nokogiri (1.
|
63
|
-
mini_portile2 (~> 2.
|
64
|
-
|
65
|
-
|
63
|
+
nokogiri (1.11.1)
|
64
|
+
mini_portile2 (~> 2.5.0)
|
65
|
+
racc (~> 1.4)
|
66
|
+
parallel (1.20.1)
|
67
|
+
parser (3.0.0.0)
|
66
68
|
ast (~> 2.4.1)
|
67
69
|
path_expander (1.1.0)
|
70
|
+
racc (1.5.2)
|
68
71
|
rack (2.2.3)
|
69
72
|
rack-test (1.1.0)
|
70
73
|
rack (>= 1.0, < 3)
|
@@ -73,35 +76,34 @@ GEM
|
|
73
76
|
nokogiri (>= 1.6)
|
74
77
|
rails-html-sanitizer (1.3.0)
|
75
78
|
loofah (~> 2.3)
|
76
|
-
railties (6.
|
77
|
-
actionpack (= 6.
|
78
|
-
activesupport (= 6.
|
79
|
+
railties (6.1.1)
|
80
|
+
actionpack (= 6.1.1)
|
81
|
+
activesupport (= 6.1.1)
|
79
82
|
method_source
|
80
83
|
rake (>= 0.8.7)
|
81
|
-
thor (
|
84
|
+
thor (~> 1.0)
|
82
85
|
rainbow (3.0.0)
|
83
|
-
rake (
|
84
|
-
regexp_parser (
|
86
|
+
rake (13.0.3)
|
87
|
+
regexp_parser (2.0.3)
|
85
88
|
rexml (3.2.4)
|
86
|
-
rubocop (
|
89
|
+
rubocop (1.8.1)
|
87
90
|
parallel (~> 1.10)
|
88
|
-
parser (>=
|
91
|
+
parser (>= 3.0.0.0)
|
89
92
|
rainbow (>= 2.2.2, < 4.0)
|
90
|
-
regexp_parser (>= 1.
|
93
|
+
regexp_parser (>= 1.8, < 3.0)
|
91
94
|
rexml
|
92
|
-
rubocop-ast (>=
|
95
|
+
rubocop-ast (>= 1.2.0, < 2.0)
|
93
96
|
ruby-progressbar (~> 1.7)
|
94
|
-
unicode-display_width (>= 1.4.0, <
|
95
|
-
rubocop-ast (
|
96
|
-
parser (>= 2.7.1.
|
97
|
-
ruby-progressbar (1.
|
97
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
98
|
+
rubocop-ast (1.4.0)
|
99
|
+
parser (>= 2.7.1.5)
|
100
|
+
ruby-progressbar (1.11.0)
|
98
101
|
sqlite3 (1.4.2)
|
99
102
|
thor (1.0.1)
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
zeitwerk (2.4.0)
|
103
|
+
tzinfo (2.0.4)
|
104
|
+
concurrent-ruby (~> 1.0)
|
105
|
+
unicode-display_width (2.0.0)
|
106
|
+
zeitwerk (2.4.2)
|
105
107
|
|
106
108
|
PLATFORMS
|
107
109
|
ruby
|
@@ -112,7 +114,8 @@ DEPENDENCIES
|
|
112
114
|
minitest
|
113
115
|
minitest-autotest
|
114
116
|
minitest-focus
|
115
|
-
|
117
|
+
mocha
|
118
|
+
rake (~> 13.0)
|
116
119
|
rubocop
|
117
120
|
sqlite3
|
118
121
|
stance!
|
data/README.md
CHANGED
@@ -1,6 +1,14 @@
|
|
1
|
-
# Stance - Simple Events for Rails apps
|
1
|
+
# Stance - Simple & Explicit Events for Rails apps
|
2
|
+
|
3
|
+
## Usage
|
2
4
|
|
3
5
|
```ruby
|
6
|
+
# Your model
|
7
|
+
class Appointment < ActiveRecord::Base
|
8
|
+
include Stance::Eventable
|
9
|
+
end
|
10
|
+
|
11
|
+
# Define your events
|
4
12
|
class AppointmentEvents < Stance::Events
|
5
13
|
# Define events.
|
6
14
|
event :my_event
|
@@ -13,6 +21,9 @@ class AppointmentEvents < Stance::Events
|
|
13
21
|
# Singleton event: only one active event with this name can exist for the same subject.
|
14
22
|
event :my_event, singleton: true
|
15
23
|
|
24
|
+
# By default, events are recorded in the database, unless you set the `record` option to false,
|
25
|
+
event :my_recordless_event, record: true
|
26
|
+
|
16
27
|
# Will be called before/after each event in this class. Have access to the event `subject` and
|
17
28
|
# `record`.
|
18
29
|
before_create :do_something_before
|
@@ -32,6 +43,35 @@ Appointment.find(1).publish_event 'offers.create'
|
|
32
43
|
Appointment.find(1).publish_event :event_with_metadata, foo: :bah
|
33
44
|
```
|
34
45
|
|
46
|
+
### ActiveRecord Callbacks
|
47
|
+
|
48
|
+
Stance comes with a couple of opt-in modules to help ease your Callback spaghetti...
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
class Appointment < ActiveRecord::Base
|
52
|
+
include Stance::Eventable
|
53
|
+
include Stance::ActiveRecordCallbacks
|
54
|
+
end
|
55
|
+
|
56
|
+
class AppointmentEvents < Stance::Events
|
57
|
+
include Stance::ActiveRecordEvents
|
58
|
+
end
|
59
|
+
|
60
|
+
# Now all your model callbacks will trigger an event of the same name, where any public methods
|
61
|
+
# defined will be called.
|
62
|
+
class AppointmentEvents::AfterCreate < Stance::Event
|
63
|
+
include Stance::ActiveRecordEvents
|
64
|
+
|
65
|
+
# This method will be called upon the :after_create callback of the Appointment model.
|
66
|
+
def do_something;end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
# Private methods will not be called by the callback.
|
71
|
+
def my_private_method;end
|
72
|
+
end
|
73
|
+
```
|
74
|
+
|
35
75
|
## Installation
|
36
76
|
|
37
77
|
Add this line to your application's Gemfile:
|
data/lib/stance.rb
CHANGED
@@ -19,4 +19,6 @@ module Stance
|
|
19
19
|
autoload :Events, 'stance/events'
|
20
20
|
autoload :Event, 'stance/event'
|
21
21
|
autoload :Eventable, 'stance/eventable'
|
22
|
+
autoload :ActiveRecordCallbacks, 'stance/active_record_callbacks'
|
23
|
+
autoload :ActiveRecordEvents, 'stance/active_record_events'
|
22
24
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Stance
|
4
|
+
module ActiveRecordCallbacks
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
CALLBACKS = %i[before_validation after_validation before_save before_create
|
8
|
+
after_create before_update after_update before_destroy after_destroy after_save
|
9
|
+
after_touch after_commit after_save_commit after_create_commit
|
10
|
+
after_update_commit after_destroy_commit after_rollback].freeze
|
11
|
+
|
12
|
+
included do
|
13
|
+
CALLBACKS.each do |cb|
|
14
|
+
send(cb) { publish_event cb }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/stance/event.rb
CHANGED
@@ -20,16 +20,25 @@ module Stance
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def initialize(name, subject, metadata, options)
|
23
|
-
@options = { singleton: false }.merge(options)
|
23
|
+
@options = { singleton: false, record: true }.merge(options)
|
24
24
|
@record = Stance::EventRecord.new(name: name, subject: subject, metadata: metadata)
|
25
25
|
end
|
26
26
|
|
27
27
|
def create
|
28
28
|
return self if singleton_exists?
|
29
29
|
|
30
|
+
Rails.logger.info "Event: #{full_name}"
|
31
|
+
|
30
32
|
Stance::EventRecord.transaction do
|
31
33
|
run_callbacks :create do
|
32
|
-
|
34
|
+
# Call each public method of the Event class if a custom class.
|
35
|
+
if self.class.name != 'Stance::Event'
|
36
|
+
(public_methods(false) - Stance::Event.instance_methods(false)).each do |method|
|
37
|
+
send method
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
record.save if @options[:record]
|
33
42
|
end
|
34
43
|
end
|
35
44
|
|
data/lib/stance/version.rb
CHANGED
data/stance.gemspec
CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
|
|
11
11
|
spec.summary = 'Event System for Rails'
|
12
12
|
spec.homepage = 'https://github.com/joelmoss/stance'
|
13
13
|
spec.license = 'MIT'
|
14
|
-
spec.required_ruby_version = Gem::Requirement.new('>= 2.
|
14
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.7.0')
|
15
15
|
|
16
16
|
spec.metadata['homepage_uri'] = spec.homepage
|
17
17
|
spec.metadata['source_code_uri'] = spec.homepage
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stance
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joel Moss
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-01-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -74,6 +74,8 @@ files:
|
|
74
74
|
- lib/generators/stance/install_generator.rb
|
75
75
|
- lib/generators/stance/templates/migration.rb.tt
|
76
76
|
- lib/stance.rb
|
77
|
+
- lib/stance/active_record_callbacks.rb
|
78
|
+
- lib/stance/active_record_events.rb
|
77
79
|
- lib/stance/engine.rb
|
78
80
|
- lib/stance/event.rb
|
79
81
|
- lib/stance/eventable.rb
|
@@ -95,14 +97,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
95
97
|
requirements:
|
96
98
|
- - ">="
|
97
99
|
- !ruby/object:Gem::Version
|
98
|
-
version: 2.
|
100
|
+
version: 2.7.0
|
99
101
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
102
|
requirements:
|
101
103
|
- - ">="
|
102
104
|
- !ruby/object:Gem::Version
|
103
105
|
version: '0'
|
104
106
|
requirements: []
|
105
|
-
rubygems_version: 3.1.
|
107
|
+
rubygems_version: 3.1.4
|
106
108
|
signing_key:
|
107
109
|
specification_version: 4
|
108
110
|
summary: Event System for Rails
|