stance 0.4.0 → 0.5.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: e5dcb8d2d1f3afe7451a48687ab8ce7c2f073b0dc9645cf958ecdb38c6673f2b
4
- data.tar.gz: 756df16a830baf56ab25bcb4ee6f027f15c13ff9908516f0c1dae1b98f761b1a
3
+ metadata.gz: 3e3331680d4153bfa8cefa453b03db38cc87ad2ee34e5413790e70fca91e9626
4
+ data.tar.gz: aaf358e5b0a7f9f42c08df1860b5410536a591ee61525ab24e8449714c257d00
5
5
  SHA512:
6
- metadata.gz: a770f62d1a8e6d6c58f1f2643dc6bd5403f957bcfb350bc5696ceca50dde989462f45ff317754602735445bffb43797db7452110f604c3304f67b5ab10cb63c9
7
- data.tar.gz: 850c0b54c13ea154307a9788e21e3670e42ec06fa17628682aa206b1cbb7de12a741d46689c783ca9a9dc43fbf02a212473fffce8ea1c41842393fa0be5720d1
6
+ metadata.gz: 3f98996364a4ebd043d9718e9c0ef7052bbc0fd03048376f66736c686c2d05fd97a1cec05833ad809e8a48361e90e8af33ac01c33a87863865916a3dbd98bd4c
7
+ data.tar.gz: 970e10e13187da5d36c3d7dff886c9c1d4971b539ef0c57d180f56a029f2987f3231b5851c377e9b9c7d0eb94bd1cece8a32a7777a594df8ae093c3bccd5609b
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- stance (0.3.0)
4
+ stance (0.4.0)
5
5
  activerecord (>= 5)
6
6
  multi_json
7
7
  railties (>= 5)
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
@@ -20,8 +20,15 @@ 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
@@ -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.0'
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.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: 2021-01-20 00:00:00.000000000 Z
11
+ date: 2021-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord