stance 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.autotest +12 -0
- data/.gitignore +9 -0
- data/.rubocop.yml +8 -0
- data/.travis.yml +6 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +15 -0
- data/Gemfile.lock +121 -0
- data/LICENSE.txt +21 -0
- data/README.md +75 -0
- data/Rakefile +12 -0
- data/app/models/stance/event_record.rb +62 -0
- data/config.ru +9 -0
- data/lib/generators/stance/install_generator.rb +35 -0
- data/lib/generators/stance/templates/migration.rb.tt +11 -0
- data/lib/stance.rb +12 -0
- data/lib/stance/engine.rb +15 -0
- data/lib/stance/event.rb +30 -0
- data/lib/stance/eventable.rb +52 -0
- data/lib/stance/events.rb +14 -0
- data/lib/stance/version.rb +5 -0
- data/stance.gemspec +30 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 593f0e612e270f03cdacbe2338c181287094558be03d721588db3ab54b094f52
|
4
|
+
data.tar.gz: 20fb411cd404fa4762e195c44a4a730670ea07862499d50b12f855b41d97cc22
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cdd07c0676ff44d4ebf2137747d573992c7dc7015c00b3520452aa2478aad46fc4bbf94b8c9d22cd90380a10dad9be96ab091945e1842e3f0529c2d7f70641ab
|
7
|
+
data.tar.gz: 1b1939a3e0b0686172b4a0edf7fa91cea2fac0a5c7257a18f7003f117bd43674d772692643f9d8bfdc96776a8f00eb8c451b6451a9c911e18673e591942245ce
|
data/.autotest
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'autotest/suffix'
|
4
|
+
require 'autotest/restart'
|
5
|
+
require 'autotest/bundler'
|
6
|
+
|
7
|
+
Autotest.add_hook :initialize do |at|
|
8
|
+
at.testlib = 'minitest/autorun'
|
9
|
+
at.add_exception '.git'
|
10
|
+
at.add_exception %r{^\./test/internal/db/.+\.sqlite}
|
11
|
+
at.add_exception %r{^\./test/internal/(?:log|public|tmp|app/assets)}
|
12
|
+
end
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at joel@developwithstyle.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in stance.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem 'autotest-suffix'
|
9
|
+
gem 'combustion'
|
10
|
+
gem 'minitest'
|
11
|
+
gem 'minitest-autotest'
|
12
|
+
gem 'minitest-focus'
|
13
|
+
gem 'rake', '~> 12.0'
|
14
|
+
gem 'rubocop'
|
15
|
+
gem 'sqlite3'
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
stance (0.1.0)
|
5
|
+
activerecord (>= 5)
|
6
|
+
multi_json
|
7
|
+
railties (>= 5)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
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)
|
16
|
+
rack-test (>= 0.6.3)
|
17
|
+
rails-dom-testing (~> 2.0)
|
18
|
+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
19
|
+
actionview (6.0.3.2)
|
20
|
+
activesupport (= 6.0.3.2)
|
21
|
+
builder (~> 3.1)
|
22
|
+
erubi (~> 1.4)
|
23
|
+
rails-dom-testing (~> 2.0)
|
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)
|
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)
|
36
|
+
ast (2.4.1)
|
37
|
+
autotest-suffix (1.1.0)
|
38
|
+
builder (3.2.4)
|
39
|
+
combustion (1.3.0)
|
40
|
+
activesupport (>= 3.0.0)
|
41
|
+
railties (>= 3.0.0)
|
42
|
+
thor (>= 0.14.6)
|
43
|
+
concurrent-ruby (1.1.6)
|
44
|
+
crass (1.0.6)
|
45
|
+
erubi (1.9.0)
|
46
|
+
i18n (1.8.3)
|
47
|
+
concurrent-ruby (~> 1.0)
|
48
|
+
loofah (2.6.0)
|
49
|
+
crass (~> 1.0.2)
|
50
|
+
nokogiri (>= 1.5.9)
|
51
|
+
method_source (1.0.0)
|
52
|
+
mini_portile2 (2.4.0)
|
53
|
+
minitest (5.14.1)
|
54
|
+
minitest-autotest (1.1.1)
|
55
|
+
minitest-server (~> 1.0)
|
56
|
+
path_expander (~> 1.0)
|
57
|
+
minitest-focus (1.2.1)
|
58
|
+
minitest (>= 4, < 6)
|
59
|
+
minitest-server (1.0.6)
|
60
|
+
minitest (~> 5.0)
|
61
|
+
multi_json (1.14.1)
|
62
|
+
nokogiri (1.10.9)
|
63
|
+
mini_portile2 (~> 2.4.0)
|
64
|
+
parallel (1.19.2)
|
65
|
+
parser (2.7.1.4)
|
66
|
+
ast (~> 2.4.1)
|
67
|
+
path_expander (1.1.0)
|
68
|
+
rack (2.2.3)
|
69
|
+
rack-test (1.1.0)
|
70
|
+
rack (>= 1.0, < 3)
|
71
|
+
rails-dom-testing (2.0.3)
|
72
|
+
activesupport (>= 4.2.0)
|
73
|
+
nokogiri (>= 1.6)
|
74
|
+
rails-html-sanitizer (1.3.0)
|
75
|
+
loofah (~> 2.3)
|
76
|
+
railties (6.0.3.2)
|
77
|
+
actionpack (= 6.0.3.2)
|
78
|
+
activesupport (= 6.0.3.2)
|
79
|
+
method_source
|
80
|
+
rake (>= 0.8.7)
|
81
|
+
thor (>= 0.20.3, < 2.0)
|
82
|
+
rainbow (3.0.0)
|
83
|
+
rake (12.3.3)
|
84
|
+
regexp_parser (1.7.1)
|
85
|
+
rexml (3.2.4)
|
86
|
+
rubocop (0.86.0)
|
87
|
+
parallel (~> 1.10)
|
88
|
+
parser (>= 2.7.0.1)
|
89
|
+
rainbow (>= 2.2.2, < 4.0)
|
90
|
+
regexp_parser (>= 1.7)
|
91
|
+
rexml
|
92
|
+
rubocop-ast (>= 0.0.3, < 1.0)
|
93
|
+
ruby-progressbar (~> 1.7)
|
94
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
95
|
+
rubocop-ast (0.0.3)
|
96
|
+
parser (>= 2.7.0.1)
|
97
|
+
ruby-progressbar (1.10.1)
|
98
|
+
sqlite3 (1.4.2)
|
99
|
+
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.3.0)
|
105
|
+
|
106
|
+
PLATFORMS
|
107
|
+
ruby
|
108
|
+
|
109
|
+
DEPENDENCIES
|
110
|
+
autotest-suffix
|
111
|
+
combustion
|
112
|
+
minitest
|
113
|
+
minitest-autotest
|
114
|
+
minitest-focus
|
115
|
+
rake (~> 12.0)
|
116
|
+
rubocop
|
117
|
+
sqlite3
|
118
|
+
stance!
|
119
|
+
|
120
|
+
BUNDLED WITH
|
121
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Joel Moss
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# Stance - Simple Events for Rails apps
|
2
|
+
|
3
|
+
```ruby
|
4
|
+
class AppointmentEvents < Stance::Events
|
5
|
+
# Define events.
|
6
|
+
event :my_event
|
7
|
+
event :some_event
|
8
|
+
|
9
|
+
# Namespaced events
|
10
|
+
event 'offers.create'
|
11
|
+
event 'offers.delete'
|
12
|
+
|
13
|
+
# Singleton event: only one event with this name can exist for the same subject.
|
14
|
+
event :my_event, singleton: true
|
15
|
+
|
16
|
+
# Optionally, create a class for an event.
|
17
|
+
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
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Publish events from the model
|
30
|
+
Appointment.find(1).publish_event :some_event
|
31
|
+
Appointment.find(1).publish_event 'offers.create'
|
32
|
+
```
|
33
|
+
|
34
|
+
## Installation
|
35
|
+
|
36
|
+
Add this line to your application's Gemfile:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
gem 'stance'
|
40
|
+
```
|
41
|
+
|
42
|
+
And then execute:
|
43
|
+
|
44
|
+
$ bundle install
|
45
|
+
|
46
|
+
Or install it yourself as:
|
47
|
+
|
48
|
+
$ gem install stance
|
49
|
+
|
50
|
+
Run the installer to generate the migration, and then run the migration:
|
51
|
+
|
52
|
+
$ rails g stance:install
|
53
|
+
$ rails db:migrate
|
54
|
+
|
55
|
+
## Usage
|
56
|
+
|
57
|
+
TODO: Write usage instructions here
|
58
|
+
|
59
|
+
## Development
|
60
|
+
|
61
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
62
|
+
|
63
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
64
|
+
|
65
|
+
## Contributing
|
66
|
+
|
67
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/joelmoss/stance. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/joelmoss/stance/blob/master/CODE_OF_CONDUCT.md).
|
68
|
+
|
69
|
+
## License
|
70
|
+
|
71
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
72
|
+
|
73
|
+
## Code of Conduct
|
74
|
+
|
75
|
+
Everyone interacting in the Stance project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/joelmoss/stance/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'multi_json'
|
4
|
+
|
5
|
+
class JSONWithIndifferentAccess
|
6
|
+
def self.load(str)
|
7
|
+
return str unless str
|
8
|
+
|
9
|
+
obj = HashWithIndifferentAccess.new(MultiJson.load(str))
|
10
|
+
obj.freeze
|
11
|
+
obj
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.dump(obj)
|
15
|
+
MultiJson.dump(obj)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module Stance
|
20
|
+
class EventRecord < ActiveRecord::Base
|
21
|
+
belongs_to :subject, polymorphic: true
|
22
|
+
|
23
|
+
scope :active, -> { where dismissed_at: nil }
|
24
|
+
scope :dismissed, -> { where.not dismissed_at: nil }
|
25
|
+
|
26
|
+
def self.table_name_prefix
|
27
|
+
'stance_'
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.dismiss_all
|
31
|
+
active.each(&:dismiss)
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_s
|
35
|
+
name
|
36
|
+
end
|
37
|
+
|
38
|
+
def event_class_name
|
39
|
+
@event_class_name ||= "#{subject.model_name.name}Events::#{name.tr('.', '/').classify}"
|
40
|
+
end
|
41
|
+
|
42
|
+
def event_class
|
43
|
+
@event_class ||= event_class_name.constantize
|
44
|
+
end
|
45
|
+
|
46
|
+
def dismissed?
|
47
|
+
dismissed_at.present?
|
48
|
+
end
|
49
|
+
|
50
|
+
def active?
|
51
|
+
!dismissed?
|
52
|
+
end
|
53
|
+
|
54
|
+
def dismiss
|
55
|
+
update_attribute :dismissed_at, Time.current
|
56
|
+
end
|
57
|
+
|
58
|
+
unless connection.adapter_name =~ /postg|mysql/i
|
59
|
+
serialize :metadata, ::JSONWithIndifferentAccess
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/config.ru
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails/generators/active_record'
|
4
|
+
|
5
|
+
module Stance
|
6
|
+
module Generators
|
7
|
+
class InstallGenerator < Rails::Generators::Base
|
8
|
+
include ActiveRecord::Generators::Migration
|
9
|
+
|
10
|
+
source_root File.join(__dir__, 'templates')
|
11
|
+
|
12
|
+
desc 'Install Stance, including a database migration.'
|
13
|
+
|
14
|
+
def copy_migration
|
15
|
+
migration_template 'migration.rb', 'db/migrate/create_stance_event_record.rb',
|
16
|
+
migration_version: migration_version
|
17
|
+
end
|
18
|
+
|
19
|
+
def migration_version
|
20
|
+
"[#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}]"
|
21
|
+
end
|
22
|
+
|
23
|
+
def properties_type
|
24
|
+
case ActiveRecord::Base.connection_config[:adapter].to_s
|
25
|
+
when /postg/i # postgres, postgis
|
26
|
+
'jsonb'
|
27
|
+
when /mysql/i
|
28
|
+
'json'
|
29
|
+
else
|
30
|
+
'text'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version %>
|
2
|
+
def change
|
3
|
+
create_table :stance_event_records do |t|
|
4
|
+
t.string :name
|
5
|
+
t.<%= properties_type %> :metadata
|
6
|
+
t.belongs_to :subject, polymorphic: true
|
7
|
+
t.datetime :dismissed_at, index: true
|
8
|
+
t.timestamps
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
data/lib/stance.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'stance/version'
|
4
|
+
require 'stance/engine'
|
5
|
+
|
6
|
+
module Stance
|
7
|
+
class EventNotFound < StandardError; end
|
8
|
+
|
9
|
+
autoload :Events, 'stance/events'
|
10
|
+
autoload :Event, 'stance/event'
|
11
|
+
autoload :Eventable, 'stance/eventable'
|
12
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails'
|
4
|
+
|
5
|
+
module Stance
|
6
|
+
class Engine < ::Rails::Engine
|
7
|
+
isolate_namespace Stance
|
8
|
+
|
9
|
+
# initializer 'active_record.include_event_concern' do
|
10
|
+
# ActiveSupport.on_load(:active_record) do
|
11
|
+
# ActiveRecord::Base.include Stance::EventConcern
|
12
|
+
# end
|
13
|
+
# end
|
14
|
+
end
|
15
|
+
end
|
data/lib/stance/event.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Stance
|
4
|
+
class Event
|
5
|
+
attr_reader :record, :options
|
6
|
+
|
7
|
+
delegate :subject, :name, to: :record
|
8
|
+
|
9
|
+
def initialize(name, subject, metadata, options)
|
10
|
+
@options = { singleton: false }.merge(options)
|
11
|
+
@record = Stance::EventRecord.new(name: name, subject: subject, metadata: metadata)
|
12
|
+
end
|
13
|
+
|
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)
|
18
|
+
|
19
|
+
callable? && record.save
|
20
|
+
end
|
21
|
+
|
22
|
+
def callable?
|
23
|
+
true
|
24
|
+
end
|
25
|
+
|
26
|
+
def call
|
27
|
+
true
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Stance
|
4
|
+
module Eventable
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
has_many :events, as: :subject, class_name: 'Stance::EventRecord'
|
9
|
+
end
|
10
|
+
|
11
|
+
# Publish an event.
|
12
|
+
#
|
13
|
+
# Creates an EventRecord with the given `name`, `metadata` and self as the 'subject'.
|
14
|
+
#
|
15
|
+
# Returns the results of `call`ing the event class.
|
16
|
+
def publish_event(name, metadata = {})
|
17
|
+
name = name.to_s
|
18
|
+
ensure_event! name
|
19
|
+
|
20
|
+
# 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])
|
23
|
+
|
24
|
+
Stance::EventRecord.transaction do
|
25
|
+
ev.valid? && ev.call
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
# Raise EventNotFound if the event has not been defined.
|
32
|
+
def ensure_event!(name)
|
33
|
+
return if events_class.events.keys.include?(name)
|
34
|
+
|
35
|
+
raise Stance::EventNotFound, "Event `#{name}` not found"
|
36
|
+
end
|
37
|
+
|
38
|
+
def events_class
|
39
|
+
@events_class ||= events_class_name.constantize
|
40
|
+
end
|
41
|
+
|
42
|
+
def events_class_name
|
43
|
+
@events_class_name ||= "#{model_name.name}Events"
|
44
|
+
end
|
45
|
+
|
46
|
+
def event_class(name)
|
47
|
+
name.constantize
|
48
|
+
rescue NameError
|
49
|
+
Stance::Event
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/stance.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/stance/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'stance'
|
7
|
+
spec.version = Stance::VERSION
|
8
|
+
spec.authors = ['Joel Moss']
|
9
|
+
spec.email = ['joel@developwithstyle.com']
|
10
|
+
|
11
|
+
spec.summary = 'Event System for Rails'
|
12
|
+
spec.homepage = 'https://github.com/joelmoss/stance'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
|
15
|
+
|
16
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
17
|
+
spec.metadata['source_code_uri'] = spec.homepage
|
18
|
+
spec.metadata['changelog_uri'] = "#{spec.homepage}/releases"
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
23
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
24
|
+
end
|
25
|
+
spec.require_paths = ['lib']
|
26
|
+
|
27
|
+
spec.add_dependency 'activerecord', '>= 5'
|
28
|
+
spec.add_dependency 'multi_json'
|
29
|
+
spec.add_dependency 'railties', '>= 5'
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: stance
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joel Moss
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-06-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: multi_json
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: railties
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- joel@developwithstyle.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".autotest"
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rubocop.yml"
|
65
|
+
- ".travis.yml"
|
66
|
+
- CODE_OF_CONDUCT.md
|
67
|
+
- Gemfile
|
68
|
+
- Gemfile.lock
|
69
|
+
- LICENSE.txt
|
70
|
+
- README.md
|
71
|
+
- Rakefile
|
72
|
+
- app/models/stance/event_record.rb
|
73
|
+
- config.ru
|
74
|
+
- lib/generators/stance/install_generator.rb
|
75
|
+
- lib/generators/stance/templates/migration.rb.tt
|
76
|
+
- lib/stance.rb
|
77
|
+
- lib/stance/engine.rb
|
78
|
+
- lib/stance/event.rb
|
79
|
+
- lib/stance/eventable.rb
|
80
|
+
- lib/stance/events.rb
|
81
|
+
- lib/stance/version.rb
|
82
|
+
- stance.gemspec
|
83
|
+
homepage: https://github.com/joelmoss/stance
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
metadata:
|
87
|
+
homepage_uri: https://github.com/joelmoss/stance
|
88
|
+
source_code_uri: https://github.com/joelmoss/stance
|
89
|
+
changelog_uri: https://github.com/joelmoss/stance/releases
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: 2.3.0
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubygems_version: 3.1.2
|
106
|
+
signing_key:
|
107
|
+
specification_version: 4
|
108
|
+
summary: Event System for Rails
|
109
|
+
test_files: []
|