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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +5 -1
- data/app/models/stance/event_record.rb +1 -1
- data/lib/stance/event.rb +10 -3
- data/lib/stance/eventable.rb +40 -6
- data/lib/stance/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3e3331680d4153bfa8cefa453b03db38cc87ad2ee34e5413790e70fca91e9626
|
4
|
+
data.tar.gz: aaf358e5b0a7f9f42c08df1860b5410536a591ee61525ab24e8449714c257d00
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3f98996364a4ebd043d9718e9c0ef7052bbc0fd03048376f66736c686c2d05fd97a1cec05833ad809e8a48361e90e8af33ac01c33a87863865916a3dbd98bd4c
|
7
|
+
data.tar.gz: 970e10e13187da5d36c3d7dff886c9c1d4971b539ef0c57d180f56a029f2987f3231b5851c377e9b9c7d0eb94bd1cece8a32a7777a594df8ae093c3bccd5609b
|
data/Gemfile.lock
CHANGED
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:
|
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
|
data/lib/stance/event.rb
CHANGED
@@ -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
|
-
|
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
|
-
"#{
|
56
|
+
"#{record.subject_type.downcase}.#{name}"
|
50
57
|
end
|
51
58
|
|
52
59
|
private
|
data/lib/stance/eventable.rb
CHANGED
@@ -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
|
-
|
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.
|
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
|
data/lib/stance/version.rb
CHANGED
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.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-
|
11
|
+
date: 2021-01-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|