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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 51b0695fffdd5e9543b325277fabfd2244cac3a84ffefb2660ed9c48499bdf12
4
- data.tar.gz: 28c77b84783964cc08617e2d28b8c1906105fe621b4ad83236bb571327e6dfc0
3
+ metadata.gz: e5dcb8d2d1f3afe7451a48687ab8ce7c2f073b0dc9645cf958ecdb38c6673f2b
4
+ data.tar.gz: 756df16a830baf56ab25bcb4ee6f027f15c13ff9908516f0c1dae1b98f761b1a
5
5
  SHA512:
6
- metadata.gz: 2816cde14c5643554d55faa81122667e13b123bc8591e8d959d412a40e204b72aeca434b41a4b98a5270b07f828652f59064e60851c5e9e83a34aa21b6c97b52
7
- data.tar.gz: '049bd51dd0ddb46eaa7e52b4a2fa0f7a709fd1c101510499637411c5d55e0ae374e5fca1de23a30760276213b1b38b43a56c353f392df9fe20272cee46eb0dcc'
6
+ metadata.gz: a770f62d1a8e6d6c58f1f2643dc6bd5403f957bcfb350bc5696ceca50dde989462f45ff317754602735445bffb43797db7452110f604c3304f67b5ab10cb63c9
7
+ data.tar.gz: 850c0b54c13ea154307a9788e21e3670e42ec06fa17628682aa206b1cbb7de12a741d46689c783ca9a9dc43fbf02a212473fffce8ea1c41842393fa0be5720d1
data/Gemfile CHANGED
@@ -10,6 +10,7 @@ gem 'combustion'
10
10
  gem 'minitest'
11
11
  gem 'minitest-autotest'
12
12
  gem 'minitest-focus'
13
- gem 'rake', '~> 12.0'
13
+ gem 'mocha'
14
+ gem 'rake', '~> 13.0'
14
15
  gem 'rubocop'
15
16
  gem 'sqlite3'
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- stance (0.2.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.0.3.2)
13
- actionview (= 6.0.3.2)
14
- activesupport (= 6.0.3.2)
15
- rack (~> 2.0, >= 2.0.8)
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.0.3.2)
20
- activesupport (= 6.0.3.2)
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.0.3.2)
26
- activesupport (= 6.0.3.2)
27
- activerecord (6.0.3.2)
28
- activemodel (= 6.0.3.2)
29
- activesupport (= 6.0.3.2)
30
- activesupport (6.0.3.2)
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 (>= 0.7, < 2)
33
- minitest (~> 5.1)
34
- tzinfo (~> 1.1)
35
- zeitwerk (~> 2.2, >= 2.2.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.0)
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.6)
43
+ concurrent-ruby (1.1.8)
44
44
  crass (1.0.6)
45
- erubi (1.9.0)
46
- i18n (1.8.5)
45
+ erubi (1.10.0)
46
+ i18n (1.8.7)
47
47
  concurrent-ruby (~> 1.0)
48
- loofah (2.6.0)
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.4.0)
53
- minitest (5.14.1)
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.10.10)
63
- mini_portile2 (~> 2.4.0)
64
- parallel (1.19.2)
65
- parser (2.7.1.4)
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.0.3.2)
77
- actionpack (= 6.0.3.2)
78
- activesupport (= 6.0.3.2)
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 (>= 0.20.3, < 2.0)
84
+ thor (~> 1.0)
82
85
  rainbow (3.0.0)
83
- rake (12.3.3)
84
- regexp_parser (1.7.1)
86
+ rake (13.0.3)
87
+ regexp_parser (2.0.3)
85
88
  rexml (3.2.4)
86
- rubocop (0.89.0)
89
+ rubocop (1.8.1)
87
90
  parallel (~> 1.10)
88
- parser (>= 2.7.1.1)
91
+ parser (>= 3.0.0.0)
89
92
  rainbow (>= 2.2.2, < 4.0)
90
- regexp_parser (>= 1.7)
93
+ regexp_parser (>= 1.8, < 3.0)
91
94
  rexml
92
- rubocop-ast (>= 0.1.0, < 1.0)
95
+ rubocop-ast (>= 1.2.0, < 2.0)
93
96
  ruby-progressbar (~> 1.7)
94
- unicode-display_width (>= 1.4.0, < 2.0)
95
- rubocop-ast (0.3.0)
96
- parser (>= 2.7.1.4)
97
- ruby-progressbar (1.10.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
- thread_safe (0.3.6)
101
- tzinfo (1.2.7)
102
- thread_safe (~> 0.1)
103
- unicode-display_width (1.7.0)
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
- rake (~> 12.0)
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:
@@ -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
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stance
4
+ module ActiveRecordEvents
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ with_options record: false do
9
+ Stance::ActiveRecordCallbacks::CALLBACKS.each { |ev| event(ev) }
10
+ end
11
+ end
12
+ end
13
+ end
@@ -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
- record.save
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
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Stance
4
- VERSION = '0.3.0'
4
+ VERSION = '0.4.0'
5
5
  end
@@ -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.3.0')
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.3.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: 2020-08-24 00:00:00.000000000 Z
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.3.0
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.2
107
+ rubygems_version: 3.1.4
106
108
  signing_key:
107
109
  specification_version: 4
108
110
  summary: Event System for Rails