stance 0.1.0 → 0.2.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: 593f0e612e270f03cdacbe2338c181287094558be03d721588db3ab54b094f52
4
- data.tar.gz: 20fb411cd404fa4762e195c44a4a730670ea07862499d50b12f855b41d97cc22
3
+ metadata.gz: 4da3d0518f890cfeff617a5751efd1cfa9267735425489cd60fda3cc756fb7c3
4
+ data.tar.gz: 63ed6c237aba36901be39c205b86f27339c2adc950a119a02eb70be8d9313827
5
5
  SHA512:
6
- metadata.gz: cdd07c0676ff44d4ebf2137747d573992c7dc7015c00b3520452aa2478aad46fc4bbf94b8c9d22cd90380a10dad9be96ab091945e1842e3f0529c2d7f70641ab
7
- data.tar.gz: 1b1939a3e0b0686172b4a0edf7fa91cea2fac0a5c7257a18f7003f117bd43674d772692643f9d8bfdc96776a8f00eb8c451b6451a9c911e18673e591942245ce
6
+ metadata.gz: 95260c2c651deebe0b4f00f667d9961c88128100e5d56cb5f956ae098af00e0421f35fdcbccf17ddd4fea63a8f5050206560c69c3daea18961b5a5501c364338
7
+ data.tar.gz: 6c72c0b24be0c1a2ad4c5943914ee74c24ed9b1c3d70a5581429f445a81c27d183473a36885fe1ba0bf72afea929062c14107be489d65edd75413ac85c34a54d
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- stance (0.1.0)
4
+ stance (0.2.0)
5
5
  activerecord (>= 5)
6
6
  multi_json
7
7
  railties (>= 5)
@@ -43,7 +43,7 @@ GEM
43
43
  concurrent-ruby (1.1.6)
44
44
  crass (1.0.6)
45
45
  erubi (1.9.0)
46
- i18n (1.8.3)
46
+ i18n (1.8.5)
47
47
  concurrent-ruby (~> 1.0)
48
48
  loofah (2.6.0)
49
49
  crass (~> 1.0.2)
@@ -58,8 +58,8 @@ GEM
58
58
  minitest (>= 4, < 6)
59
59
  minitest-server (1.0.6)
60
60
  minitest (~> 5.0)
61
- multi_json (1.14.1)
62
- nokogiri (1.10.9)
61
+ multi_json (1.15.0)
62
+ nokogiri (1.10.10)
63
63
  mini_portile2 (~> 2.4.0)
64
64
  parallel (1.19.2)
65
65
  parser (2.7.1.4)
@@ -83,17 +83,17 @@ GEM
83
83
  rake (12.3.3)
84
84
  regexp_parser (1.7.1)
85
85
  rexml (3.2.4)
86
- rubocop (0.86.0)
86
+ rubocop (0.89.0)
87
87
  parallel (~> 1.10)
88
- parser (>= 2.7.0.1)
88
+ parser (>= 2.7.1.1)
89
89
  rainbow (>= 2.2.2, < 4.0)
90
90
  regexp_parser (>= 1.7)
91
91
  rexml
92
- rubocop-ast (>= 0.0.3, < 1.0)
92
+ rubocop-ast (>= 0.1.0, < 1.0)
93
93
  ruby-progressbar (~> 1.7)
94
94
  unicode-display_width (>= 1.4.0, < 2.0)
95
- rubocop-ast (0.0.3)
96
- parser (>= 2.7.0.1)
95
+ rubocop-ast (0.3.0)
96
+ parser (>= 2.7.1.4)
97
97
  ruby-progressbar (1.10.1)
98
98
  sqlite3 (1.4.2)
99
99
  thor (1.0.1)
@@ -101,7 +101,7 @@ GEM
101
101
  tzinfo (1.2.7)
102
102
  thread_safe (~> 0.1)
103
103
  unicode-display_width (1.7.0)
104
- zeitwerk (2.3.0)
104
+ zeitwerk (2.4.0)
105
105
 
106
106
  PLATFORMS
107
107
  ruby
data/README.md CHANGED
@@ -10,25 +10,26 @@ class AppointmentEvents < Stance::Events
10
10
  event 'offers.create'
11
11
  event 'offers.delete'
12
12
 
13
- # Singleton event: only one event with this name can exist for the same subject.
13
+ # Singleton event: only one active event with this name can exist for the same subject.
14
14
  event :my_event, singleton: true
15
15
 
16
+ # Will be called before/after each event in this class. Have access to the event `subject` and
17
+ # `record`.
18
+ before_create :do_something_before
19
+ after_create :do_something_after
20
+
16
21
  # Optionally, create a class for an event.
17
22
  class SomeEvent < Stance::Event
18
- # Return false if you do not want the event to be created.
19
- def callable?
20
- false
21
- end
22
-
23
- def call
24
- # do something when the event is created.
25
- end
23
+ # Define optional callbacks which have access to the `subject` and event `record`.
24
+ before_create :do_something_before
25
+ after_create :do_something_after
26
26
  end
27
27
  end
28
28
 
29
29
  # Publish events from the model
30
30
  Appointment.find(1).publish_event :some_event
31
31
  Appointment.find(1).publish_event 'offers.create'
32
+ Appointment.find(1).publish_event :event_with_metadata, foo: :bah
32
33
  ```
33
34
 
34
35
  ## Installation
@@ -2,29 +2,49 @@
2
2
 
3
3
  module Stance
4
4
  class Event
5
+ include ActiveSupport::Callbacks
6
+
7
+ define_callbacks :create
5
8
  attr_reader :record, :options
6
9
 
7
10
  delegate :subject, :name, to: :record
8
11
 
12
+ class << self
13
+ def before_create(*methods, &block)
14
+ set_callback :create, :before, *methods, &block
15
+ end
16
+
17
+ def after_create(*methods, &block)
18
+ set_callback :create, :after, *methods, &block
19
+ end
20
+ end
21
+
9
22
  def initialize(name, subject, metadata, options)
10
23
  @options = { singleton: false }.merge(options)
11
24
  @record = Stance::EventRecord.new(name: name, subject: subject, metadata: metadata)
12
25
  end
13
26
 
14
- def valid?
15
- # If event is a singleton, check there is no other active event with the same name. If there
16
- # is, return false.
17
- return false if options[:singleton] && subject.events.active.exists?(name: name)
27
+ def create
28
+ return self if singleton_exists?
29
+
30
+ Stance::EventRecord.transaction do
31
+ run_callbacks :create do
32
+ record.save
33
+ end
34
+ end
18
35
 
19
- callable? && record.save
36
+ self
20
37
  end
21
38
 
22
- def callable?
23
- true
39
+ def full_name
40
+ "#{subject.model_name.singular}.#{name}"
24
41
  end
25
42
 
26
- def call
27
- true
43
+ private
44
+
45
+ # Event is a singleton and already exists.
46
+ def singleton_exists?
47
+ options[:singleton] && subject.events.active.exists?(name: name)
28
48
  end
29
49
  end
30
50
  end
@@ -21,8 +21,8 @@ module Stance
21
21
  event_class_name = "#{events_class_name}::#{name.to_s.tr('.', '/').classify}"
22
22
  ev = event_class(event_class_name).new(name, self, metadata, events_class.events[name])
23
23
 
24
- Stance::EventRecord.transaction do
25
- ev.valid? && ev.call
24
+ events_class.new(ev).run_callbacks :create do
25
+ ev.create
26
26
  end
27
27
  end
28
28
 
@@ -2,6 +2,12 @@
2
2
 
3
3
  module Stance
4
4
  class Events
5
+ include ActiveSupport::Callbacks
6
+
7
+ attr_reader :event
8
+
9
+ define_callbacks :create
10
+
5
11
  class << self
6
12
  attr_reader :events
7
13
 
@@ -9,6 +15,18 @@ module Stance
9
15
  @events ||= {}
10
16
  @events[name.to_s] = options
11
17
  end
18
+
19
+ def before_create(*methods, &block)
20
+ set_callback :create, :before, *methods, &block
21
+ end
22
+
23
+ def after_create(*methods, &block)
24
+ set_callback :create, :after, *methods, &block
25
+ end
26
+ end
27
+
28
+ def initialize(event)
29
+ @event = event
12
30
  end
13
31
  end
14
32
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Stance
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.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.1.0
4
+ version: 0.2.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-06-24 00:00:00.000000000 Z
11
+ date: 2020-08-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord