stance 0.4.0 → 0.5.3

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: e5dcb8d2d1f3afe7451a48687ab8ce7c2f073b0dc9645cf958ecdb38c6673f2b
4
- data.tar.gz: 756df16a830baf56ab25bcb4ee6f027f15c13ff9908516f0c1dae1b98f761b1a
3
+ metadata.gz: e0a1f7dabcadb5940fbff6909c98d24b60f1f1c50536be7e0219f77d07cd2412
4
+ data.tar.gz: 02a1ed28bb48015bed11b026c32528a5d473ac9ce8befb1efd66913dcf141b8f
5
5
  SHA512:
6
- metadata.gz: a770f62d1a8e6d6c58f1f2643dc6bd5403f957bcfb350bc5696ceca50dde989462f45ff317754602735445bffb43797db7452110f604c3304f67b5ab10cb63c9
7
- data.tar.gz: 850c0b54c13ea154307a9788e21e3670e42ec06fa17628682aa206b1cbb7de12a741d46689c783ca9a9dc43fbf02a212473fffce8ea1c41842393fa0be5720d1
6
+ metadata.gz: bad97f4ed5bf8da34ea4c875c833afd64207f841565199c4ae9c61237562bc633a75aabb0d714fccd1182050842270cd334f4692762496be6591eef68cc7c831
7
+ data.tar.gz: bb09581a78c3aad7989720e2076a88a31986cc7205dba12b9658adfd26f3744f61693528420d2f2769a4dd23b410ce4e6f684d68d21527a973a6b429d04c2c9e
data/.rubocop.yml CHANGED
@@ -5,4 +5,6 @@ Style/Documentation:
5
5
  Layout/LineLength:
6
6
  Max: 100
7
7
  Metrics/MethodLength:
8
- Max: 12
8
+ Max: 15
9
+ Style/ClassVars:
10
+ Enabled: false
data/Gemfile CHANGED
@@ -13,4 +13,6 @@ gem 'minitest-focus'
13
13
  gem 'mocha'
14
14
  gem 'rake', '~> 13.0'
15
15
  gem 'rubocop'
16
+ gem 'rubocop-minitest'
17
+ gem 'rubocop-rake'
16
18
  gem 'sqlite3'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- stance (0.3.0)
4
+ stance (0.5.2)
5
5
  activerecord (>= 5)
6
6
  multi_json
7
7
  railties (>= 5)
@@ -9,62 +9,62 @@ PATH
9
9
  GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
- actionpack (6.1.1)
13
- actionview (= 6.1.1)
14
- activesupport (= 6.1.1)
12
+ actionpack (6.1.4)
13
+ actionview (= 6.1.4)
14
+ activesupport (= 6.1.4)
15
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.1.1)
20
- activesupport (= 6.1.1)
19
+ actionview (6.1.4)
20
+ activesupport (= 6.1.4)
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.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)
25
+ activemodel (6.1.4)
26
+ activesupport (= 6.1.4)
27
+ activerecord (6.1.4)
28
+ activemodel (= 6.1.4)
29
+ activesupport (= 6.1.4)
30
+ activesupport (6.1.4)
31
31
  concurrent-ruby (~> 1.0, >= 1.0.2)
32
32
  i18n (>= 1.6, < 2)
33
33
  minitest (>= 5.1)
34
34
  tzinfo (~> 2.0)
35
35
  zeitwerk (~> 2.3)
36
- ast (2.4.1)
36
+ ast (2.4.2)
37
37
  autotest-suffix (1.1.0)
38
38
  builder (3.2.4)
39
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.8)
43
+ concurrent-ruby (1.1.9)
44
44
  crass (1.0.6)
45
45
  erubi (1.10.0)
46
- i18n (1.8.7)
46
+ i18n (1.8.10)
47
47
  concurrent-ruby (~> 1.0)
48
- loofah (2.9.0)
48
+ loofah (2.10.0)
49
49
  crass (~> 1.0.2)
50
50
  nokogiri (>= 1.5.9)
51
51
  method_source (1.0.0)
52
- mini_portile2 (2.5.0)
53
- minitest (5.14.3)
52
+ mini_portile2 (2.5.3)
53
+ minitest (5.14.4)
54
54
  minitest-autotest (1.1.1)
55
55
  minitest-server (~> 1.0)
56
56
  path_expander (~> 1.0)
57
- minitest-focus (1.2.1)
57
+ minitest-focus (1.3.1)
58
58
  minitest (>= 4, < 6)
59
59
  minitest-server (1.0.6)
60
60
  minitest (~> 5.0)
61
- mocha (1.12.0)
61
+ mocha (1.13.0)
62
62
  multi_json (1.15.0)
63
- nokogiri (1.11.1)
63
+ nokogiri (1.11.7)
64
64
  mini_portile2 (~> 2.5.0)
65
65
  racc (~> 1.4)
66
66
  parallel (1.20.1)
67
- parser (3.0.0.0)
67
+ parser (3.0.2.0)
68
68
  ast (~> 2.4.1)
69
69
  path_expander (1.1.0)
70
70
  racc (1.5.2)
@@ -76,30 +76,34 @@ GEM
76
76
  nokogiri (>= 1.6)
77
77
  rails-html-sanitizer (1.3.0)
78
78
  loofah (~> 2.3)
79
- railties (6.1.1)
80
- actionpack (= 6.1.1)
81
- activesupport (= 6.1.1)
79
+ railties (6.1.4)
80
+ actionpack (= 6.1.4)
81
+ activesupport (= 6.1.4)
82
82
  method_source
83
- rake (>= 0.8.7)
83
+ rake (>= 0.13)
84
84
  thor (~> 1.0)
85
85
  rainbow (3.0.0)
86
- rake (13.0.3)
87
- regexp_parser (2.0.3)
88
- rexml (3.2.4)
89
- rubocop (1.8.1)
86
+ rake (13.0.6)
87
+ regexp_parser (2.1.1)
88
+ rexml (3.2.5)
89
+ rubocop (1.18.3)
90
90
  parallel (~> 1.10)
91
91
  parser (>= 3.0.0.0)
92
92
  rainbow (>= 2.2.2, < 4.0)
93
93
  regexp_parser (>= 1.8, < 3.0)
94
94
  rexml
95
- rubocop-ast (>= 1.2.0, < 2.0)
95
+ rubocop-ast (>= 1.7.0, < 2.0)
96
96
  ruby-progressbar (~> 1.7)
97
97
  unicode-display_width (>= 1.4.0, < 3.0)
98
- rubocop-ast (1.4.0)
99
- parser (>= 2.7.1.5)
98
+ rubocop-ast (1.8.0)
99
+ parser (>= 3.0.1.1)
100
+ rubocop-minitest (0.14.0)
101
+ rubocop (>= 0.90, < 2.0)
102
+ rubocop-rake (0.6.0)
103
+ rubocop (~> 1.0)
100
104
  ruby-progressbar (1.11.0)
101
105
  sqlite3 (1.4.2)
102
- thor (1.0.1)
106
+ thor (1.1.0)
103
107
  tzinfo (2.0.4)
104
108
  concurrent-ruby (~> 1.0)
105
109
  unicode-display_width (2.0.0)
@@ -117,8 +121,10 @@ DEPENDENCIES
117
121
  mocha
118
122
  rake (~> 13.0)
119
123
  rubocop
124
+ rubocop-minitest
125
+ rubocop-rake
120
126
  sqlite3
121
127
  stance!
122
128
 
123
129
  BUNDLED WITH
124
- 2.1.4
130
+ 2.2.18
data/README.md CHANGED
@@ -22,7 +22,10 @@ class AppointmentEvents < Stance::Events
22
22
  event :my_event, singleton: true
23
23
 
24
24
  # By default, events are recorded in the database, unless you set the `record` option to false,
25
- event :my_recordless_event, record: true
25
+ event :my_recordless_event, record: false
26
+
27
+ # You can define a class event, which is published on the class instead of the instance.
28
+ event :my_class_event, class: true
26
29
 
27
30
  # Will be called before/after each event in this class. Have access to the event `subject` and
28
31
  # `record`.
@@ -41,6 +44,7 @@ end
41
44
  Appointment.find(1).publish_event :some_event
42
45
  Appointment.find(1).publish_event 'offers.create'
43
46
  Appointment.find(1).publish_event :event_with_metadata, foo: :bah
47
+ Appointment.publish_event :my_class_event
44
48
  ```
45
49
 
46
50
  ### ActiveRecord Callbacks
@@ -36,7 +36,7 @@ module Stance
36
36
  end
37
37
 
38
38
  def event_class_name
39
- @event_class_name ||= "#{subject.model_name.name}Events::#{name.tr('.', '/').classify}"
39
+ @event_class_name ||= "#{subject_type}Events::#{name.tr('.', '/').classify}"
40
40
  end
41
41
 
42
42
  def event_class
@@ -4,7 +4,7 @@ module Stance
4
4
  module ActiveRecordCallbacks
5
5
  extend ActiveSupport::Concern
6
6
 
7
- CALLBACKS = %i[before_validation after_validation before_save before_create
7
+ CALLBACKS = %i[after_initialize before_validation after_validation before_save before_create
8
8
  after_create before_update after_update before_destroy after_destroy after_save
9
9
  after_touch after_commit after_save_commit after_create_commit
10
10
  after_update_commit after_destroy_commit after_rollback].freeze
data/lib/stance/event.rb CHANGED
@@ -20,19 +20,26 @@ module Stance
20
20
  end
21
21
 
22
22
  def initialize(name, subject, metadata, options)
23
- @options = { singleton: false, record: true }.merge(options)
24
- @record = Stance::EventRecord.new(name: name, subject: subject, metadata: metadata)
23
+ @options = { singleton: false, record: true, class: false }.merge(options)
24
+
25
+ attrs = { name: name, metadata: metadata }
26
+ if subject.is_a?(String)
27
+ attrs[:subject_type] = subject
28
+ else
29
+ attrs[:subject] = subject
30
+ end
31
+ @record = Stance::EventRecord.new(attrs)
25
32
  end
26
33
 
27
34
  def create
28
35
  return self if singleton_exists?
29
36
 
30
- Rails.logger.info "Event: #{full_name}"
31
-
32
37
  Stance::EventRecord.transaction do
33
38
  run_callbacks :create do
34
39
  # Call each public method of the Event class if a custom class.
35
40
  if self.class.name != 'Stance::Event'
41
+ Rails.logger.info "Event: #{full_name}"
42
+
36
43
  (public_methods(false) - Stance::Event.instance_methods(false)).each do |method|
37
44
  send method
38
45
  end
@@ -46,7 +53,7 @@ module Stance
46
53
  end
47
54
 
48
55
  def full_name
49
- "#{subject.model_name.singular}.#{name}"
56
+ "#{record.subject_type.downcase}.#{name}"
50
57
  end
51
58
 
52
59
  private
@@ -8,6 +8,43 @@ module Stance
8
8
  has_many :events, as: :subject, class_name: 'Stance::EventRecord'
9
9
  end
10
10
 
11
+ class_methods do
12
+ def publish_event(name, metadata = {})
13
+ name = name.to_s
14
+ ensure_event! name
15
+
16
+ # Find the Event class - if any - and call it. Falls back to Stance::Event.
17
+ ev = event_class(name).new(name, model_name.name, metadata, events_class.events[name])
18
+
19
+ return ev if Stance.disabled_events.include?(ev.full_name)
20
+
21
+ events_class.new(ev).run_callbacks(:create) { ev.create }
22
+ end
23
+
24
+ private
25
+
26
+ # Raise EventNotFound if the class event has not been defined.
27
+ def ensure_event!(name)
28
+ return if events_class.events.one? { |event, options| name == event && options[:class] }
29
+
30
+ raise Stance::EventNotFound, "Class event `#{name}` not found"
31
+ end
32
+
33
+ def events_class
34
+ @events_class ||= events_class_name.constantize
35
+ end
36
+
37
+ def events_class_name
38
+ @events_class_name ||= "#{model_name.name}Events"
39
+ end
40
+
41
+ def event_class(name)
42
+ name.constantize
43
+ rescue NameError
44
+ Stance::Event
45
+ end
46
+ end
47
+
11
48
  # Publish an event.
12
49
  #
13
50
  # Creates an EventRecord with the given `name`, `metadata` and self as the 'subject'.
@@ -18,19 +55,16 @@ module Stance
18
55
  ensure_event! name
19
56
 
20
57
  # Find the Event class - if any - and call it. Falls back to Stance::Event.
21
- event_class_name = "#{events_class_name}::#{name.to_s.tr('.', '/').classify}"
22
- ev = event_class(event_class_name).new(name, self, metadata, events_class.events[name])
58
+ ev = event_class(name).new(name, self, metadata, events_class.events[name])
23
59
 
24
60
  return ev if Stance.disabled_events.include?(ev.full_name)
25
61
 
26
62
  events_class.new(ev).run_callbacks(:create) { ev.create }
27
63
  end
28
64
 
29
- private
30
-
31
65
  # Raise EventNotFound if the event has not been defined.
32
66
  def ensure_event!(name)
33
- return if events_class.events.keys.include?(name)
67
+ return if events_class.events.one? { |event, options| name == event && !options[:class] }
34
68
 
35
69
  raise Stance::EventNotFound, "Event `#{name}` not found"
36
70
  end
@@ -44,7 +78,7 @@ module Stance
44
78
  end
45
79
 
46
80
  def event_class(name)
47
- name.constantize
81
+ "#{events_class_name}::#{name.tr('.', '/').classify}".constantize
48
82
  rescue NameError
49
83
  Stance::Event
50
84
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Stance
4
- VERSION = '0.4.0'
4
+ VERSION = '0.5.3'
5
5
  end
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.0
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel Moss
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-20 00:00:00.000000000 Z
11
+ date: 2021-07-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -52,7 +52,7 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '5'
55
- description:
55
+ description:
56
56
  email:
57
57
  - joel@developwithstyle.com
58
58
  executables: []
@@ -89,7 +89,7 @@ metadata:
89
89
  homepage_uri: https://github.com/joelmoss/stance
90
90
  source_code_uri: https://github.com/joelmoss/stance
91
91
  changelog_uri: https://github.com/joelmoss/stance/releases
92
- post_install_message:
92
+ post_install_message:
93
93
  rdoc_options: []
94
94
  require_paths:
95
95
  - lib
@@ -104,8 +104,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
104
  - !ruby/object:Gem::Version
105
105
  version: '0'
106
106
  requirements: []
107
- rubygems_version: 3.1.4
108
- signing_key:
107
+ rubygems_version: 3.2.15
108
+ signing_key:
109
109
  specification_version: 4
110
110
  summary: Event System for Rails
111
111
  test_files: []