bottled_observers 0.1.0.alpha.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +136 -6
- data/lib/bottled_observers/bottled_observable.rb +0 -1
- data/lib/bottled_observers/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0de7b24c1c7374eb622b321c0a15bfe54abda7f4
|
4
|
+
data.tar.gz: 5514640d30cb75a05fbb9fac0d898dcc27a182b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a5cfb97d46b6ce73546dedd6dc03f963e297944f53aee7bfb4402e601fb61ef2bb8d7e36b3b48703437ea11ed8ae8ac7d67d7fbc12349d2ce33fc2f4498dde62
|
7
|
+
data.tar.gz: 839972e93ad95495ec94ec40a647627d0099e76f5662b9476d5667fede9284786bd752d3b3205ce8affdba3b4c7628978155f9cb2a25510c5172c3517075d4dd
|
data/README.md
CHANGED
@@ -1,8 +1,12 @@
|
|
1
1
|
# BottledObservers
|
2
2
|
|
3
|
-
|
3
|
+
## The best thing to happen since bottled water
|
4
4
|
|
5
|
-
|
5
|
+
Wait, wasn't that [bottled_decorators?](https://github.com/John-Hayes-Reed/bottled_decorators),
|
6
|
+
or was it [bottled_services?](https://github.com/John-Hayes-Reed/bottled_services),
|
7
|
+
I am starting to lose track. Anyway! bottled_observers are an easy-to-use solution,
|
8
|
+
for the Observer / Subscribe-Publish pattern in ruby. all you need to worry about
|
9
|
+
is adding your business logic, let bottled_observers handle the rest!
|
6
10
|
|
7
11
|
## Installation
|
8
12
|
|
@@ -22,17 +26,143 @@ Or install it yourself as:
|
|
22
26
|
|
23
27
|
## Usage
|
24
28
|
|
25
|
-
|
29
|
+
bottled_observers has two main modules you need to be aware of; BottledObserver and BottledObservable.
|
30
|
+
|
31
|
+
#### BottledObserver
|
32
|
+
|
33
|
+
This module should be included in your observer class, and that observer class
|
34
|
+
should only contain a single public method: `#call`.
|
35
|
+
eg:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
class SendPushNotification
|
39
|
+
include BottledObserver # <-- just like this!
|
40
|
+
|
41
|
+
def call
|
42
|
+
# Some intense and awesome logic to notify the hell out of them users.
|
43
|
+
end
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
That is all there is to it, your observer is now ready to rock'n'roll.
|
48
|
+
|
49
|
+
#### BottledObservable
|
50
|
+
|
51
|
+
This module is included in any class that should allow observers to subscribe to it.
|
52
|
+
eg:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
class Product
|
56
|
+
include BottledObservable # <-- and just like this!
|
57
|
+
|
58
|
+
# The rest of the class....
|
59
|
+
end
|
60
|
+
```
|
61
|
+
|
62
|
+
And that is it, your whole bottled_observers subscribe/publish cycle is ready to go.
|
63
|
+
|
64
|
+
#### The Subscribe / Publish Cycle
|
65
|
+
|
66
|
+
There are three main methods available in your arsenal that you need to be aware of:
|
67
|
+
* `#add_subscription`
|
68
|
+
* `#modified`
|
69
|
+
* `#publish`
|
70
|
+
|
71
|
+
These are available to your observable class instances, so if we wanted to subscribe
|
72
|
+
our `SendPushNotification` observer to an instance of our `Product` class, we could
|
73
|
+
do the following:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
@product = Product.find(1) # <-- instantiate Product from a record in the database.
|
77
|
+
@product.add_subscription SendPushNotification # <-- add a subscription for the SendPushNotification class
|
78
|
+
```
|
79
|
+
|
80
|
+
We now have a `SendPushNotification` observer lying in wait for any publications from the `@product` instance.
|
81
|
+
Then, when we want to notify the observer(s) of any changes to the instance, just
|
82
|
+
set the state of the instance as *modified* using the `#modified` method, and publish
|
83
|
+
this change of state to its subscribers:
|
84
|
+
|
85
|
+
*Example case: sending push notifications after a successful save of the @product instance.*
|
86
|
+
```ruby
|
87
|
+
# The product is only in a modified state if the save is successful
|
88
|
+
@product.modified if @product.save
|
89
|
+
|
90
|
+
# If the @product's state is 'modified' the observers call method will be excecuted.
|
91
|
+
# If the state is not modified, nothing will happen.
|
92
|
+
@product.publish
|
93
|
+
```
|
94
|
+
|
95
|
+
**Super simple!**
|
96
|
+
|
97
|
+
Another thing that needs to be noted is, after publishing, the instances *modified*
|
98
|
+
state is reset, so to publish anything again, it must be set again.
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
@product.modified
|
102
|
+
@product.publish # <-- push notifications sent.
|
103
|
+
@product.publish # <-- push notifications not sent.
|
104
|
+
```
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
@product.modified
|
108
|
+
@product.publish # <-- push notifications sent.
|
109
|
+
@product.modified
|
110
|
+
@product.publish # <-- push notifications sent.
|
111
|
+
```
|
112
|
+
|
113
|
+
#### Extras
|
114
|
+
|
115
|
+
You can have as many observers subscribed to a single instance as you like:
|
116
|
+
```ruby
|
117
|
+
@product.add_subscription SendPushNotification
|
118
|
+
@product.add_subscription MailToMailingList
|
119
|
+
@product.add_subscription CreateRecentActivityRecord
|
120
|
+
|
121
|
+
@product.modified if @product.save
|
122
|
+
@product.publish
|
123
|
+
```
|
124
|
+
|
125
|
+
`#subscriptions` will return an array of current observer instances subscribed to the observable instance:
|
126
|
+
```ruby
|
127
|
+
@product.add_subscription SendPushNotification
|
128
|
+
@product.subscriptions #=> [#<SendPushNotification:0x00...>]
|
129
|
+
```
|
130
|
+
|
131
|
+
To remove observers of a particular class, use `#remove_subscription`:
|
132
|
+
```ruby
|
133
|
+
@product.add_subscription SendPushNotification
|
134
|
+
@product.subscriptions #=> [#<SendPushNotification:0x00...>]
|
135
|
+
@product.remove_subscription SendPushNotification
|
136
|
+
@product.subscriptions #=> []
|
137
|
+
```
|
138
|
+
|
139
|
+
Or use `#remove_subscriptions` *(notice the plural)* to, you guessed it, remove **all** observers:
|
140
|
+
```ruby
|
141
|
+
@product.add_subscription SendPushNotification
|
142
|
+
@product.add_subscription MailToMailingList
|
143
|
+
@product.add_subscription CreateRecentActivityRecord
|
144
|
+
@product.subscriptions #=> [#<SendPushNotification:0x00...>, ...]
|
145
|
+
|
146
|
+
@product.remove_subscriptions
|
147
|
+
@product.subscriptions #=> []
|
148
|
+
```
|
149
|
+
|
150
|
+
You can also check if your product is in a modified state by checking if it is `#publishable?`:
|
151
|
+
```ruby
|
152
|
+
@product.publishable? #=> false
|
153
|
+
@product.modified
|
154
|
+
@product.publishable? #=> true
|
155
|
+
```
|
26
156
|
|
27
157
|
## Development
|
28
158
|
|
29
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `
|
159
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
160
|
|
31
161
|
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).
|
32
162
|
|
33
163
|
## Contributing
|
34
164
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
165
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/John-Hayes-Reed/bottled_observers. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
166
|
|
37
167
|
## License
|
38
168
|
|
@@ -40,4 +170,4 @@ The gem is available as open source under the terms of the [MIT License](http://
|
|
40
170
|
|
41
171
|
## Code of Conduct
|
42
172
|
|
43
|
-
Everyone interacting in the BottledObservers project’s codebases, issue trackers, chat rooms and mailing lists is expected to
|
173
|
+
Everyone interacting in the BottledObservers project’s codebases, issue trackers, chat rooms and mailing lists is expected to have a beer or two before and enjoy life. Oh yeah and apparently this too: [code of conduct](https://github.com/[USERNAME]/bottled_observers/blob/master/CODE_OF_CONDUCT.md).
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bottled_observers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Hayes-Reed
|
@@ -91,9 +91,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
91
91
|
version: '0'
|
92
92
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- - "
|
94
|
+
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version:
|
96
|
+
version: '0'
|
97
97
|
requirements: []
|
98
98
|
rubyforge_project:
|
99
99
|
rubygems_version: 2.6.10
|