observable
Version, currently 0.0.23 versions
- 0.0.4latestJun 15, 2020
- 0.0.3not indexedApr 10, 2023
- 0.0.2not indexedApr 10, 2023
github.com/TPei/observable
Implementation of the Observer pattern in crystal
9 stars
0 dependents
License: MIT
Nothing has been indexed for 0.0.2 yet. The tag is recorded, its shard.yml has not been read, so the manifest and dependency list below are empty because they are unknown rather than because they are absent.
Installation
# Add this to your shard.yml
dependencies:
observable:
github: TPei/observable
version: ~> 0.0.2Then run:
shards installshard.yml
No shard.yml has been indexed for 0.0.2. You can read it on the repository.
Dependencies
Unknown: the shard.yml for this version has not been read yet.
README
This README is the one indexed from the repository at its latest ref, not from the tag for this version.
# observable [](https://travis-ci.org/TPei/observable)
Simple implementation of the Observer pattern in Crystal. It is loosely based on the [Observable](http://ruby-doc.org/stdlib-2.0.0/libdoc/observer/rdoc/Observable.html) module from Ruby :)
The observer pattern is used to avoid tightly coupling objects that
stand in a one-to-many relationship.
It allows you to notify dependents about changes that happened to your
object, without having hard-coded dependencies :)
## Installation
Add to your shard.yml
```yaml
dependencies:
observable:
github: tpei/observable
branch: master
```
and then install the library into your project with
```bash
$ crystal deps
```
## Usage
Include observable in the class you want to observe:
```crystal
require "observable/observable"
class ToObserve
include Observable
end
```
This provides you with the following methods:
- `add_observer(observer : Observer)`
- `changed(state=true)`
- `changed?`
- `count_observers`
- `delete_observer(observer : Observer)`
- `delete_observers`
- `notify_observers`
and as an added bonus, because I dislike the forced `changed` call precondition:
- `notify_observers!`, which notifies no matter if changed or not
Then you can include observer in the class you want observing:
```crystal
require "observable/observer"
class Observing
include Observer
def update(observable : Observable)
# do whatever you want to do if your observable changes
end
end
```
This only gives you an `update(observable : Observable)` method you should override, because it willl raise an error otherwise.
Unfortunately we don't have ruby's verbosity and dynamism here so it's all a little more static. Especially the `update` method can really only pass on the `observable` object.
Documentation
Built from the current release. The first visit to a release nobody has asked for starts its build.
Links
This release
- Version
0.0.2- Tagged
- Apr 10, 2023
- Commit
c1919bb27adc- Indexed
- not yet
Dependents
No indexed shard depends on this one yet.
Repository
github.com/TPei/observable
Metadata
- Created
- Aug 12, 2026
- Updated
- Aug 12, 2026
- Synced
- Aug 12, 2026
- Versions
- 3