├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── shard.yml ├── spec ├── integration │ └── evented_spec.cr └── spec_helper.cr └── src ├── evented.cr └── evented └── version.cr /.gitignore: -------------------------------------------------------------------------------- 1 | /.deps/ 2 | /libs/ 3 | /.crystal/ 4 | /doc/ 5 | 6 | 7 | # Libraries don't need dependency lock 8 | # Dependencies will be locked in application that uses them 9 | /.deps.lock 10 | 11 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: crystal 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Kris Leech 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. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Evented 2 | 3 | *A micro library providing Crystal objects with Publish-Subscribe capabilities* 4 | 5 | [![Build Status](https://travis-ci.org/krisleech/evented.png?branch=master)](https://travis-ci.org/krisleech/evented) 6 | 7 | * Decouple core business logic from external concerns in Hexagonal style architectures 8 | * Use as an alternative to callbacks and Observers 9 | * Connect objects based on context without permanence 10 | * React to events synchronously 11 | 12 | ## Installation 13 | 14 | Add this to your application's `shard.yml`: 15 | 16 | ```yaml 17 | dependencies: 18 | evented: 19 | github: krisleech/evented 20 | ``` 21 | 22 | ## Usage 23 | 24 | ### Publishing 25 | 26 | By including `Evented::Publisher` your objects get `broadcast` and 27 | `subscribe` methods. 28 | 29 | `broadcast` can be called from within your object whenever you want to 30 | broadcast a significant event. 31 | 32 | ```crystal 33 | require "evented" 34 | 35 | class MyPublisher 36 | include Evented::Publisher 37 | 38 | def call(input) 39 | result = do_something(input) 40 | broadcast(:something_happened, result) 41 | end 42 | end 43 | ``` 44 | 45 | ### Subscribing 46 | 47 | To subscribe an object to receive events include `Evented::Subscriber` and 48 | provide your own `on_event` method which will receive 2 arguments, the 49 | `event_name` and `payload`. 50 | 51 | ```crystal 52 | require "evented" 53 | 54 | class MySubscriber 55 | include Evented::Subscriber 56 | 57 | def on_event(event_name, payload) 58 | # ... 59 | end 60 | end 61 | ``` 62 | 63 | To subscribe the listener to a publisher: 64 | 65 | ```crystal 66 | publisher = MyPublisher.new 67 | publisher.subscribe(MySubscriber.new) 68 | 69 | publisher.call("hello") 70 | ``` 71 | 72 | In the above example the subscriber will have `on_event(:something_happened, "hello")` 73 | called. 74 | 75 | ## Development 76 | 77 | ### Specs 78 | 79 | ``` 80 | crystal spec 81 | ``` 82 | 83 | ### Automatically run Specs 84 | 85 | ``` 86 | ls ./**/*.cr | entr crystal spec 87 | ``` 88 | 89 | ## Contributing 90 | 91 | 1. Fork it ( https://github.com/krisleech/evented/fork ) 92 | 2. Create your feature branch (git checkout -b my-new-feature) 93 | 3. Commit your changes (git commit -am 'Add some feature') 94 | 4. Push to the branch (git push origin my-new-feature) 95 | 5. Create a new Pull Request 96 | 97 | ## Contributors 98 | 99 | - [Kris Leech](https://github.com/krisleech) - creator, maintainer 100 | -------------------------------------------------------------------------------- /shard.yml: -------------------------------------------------------------------------------- 1 | name: evented 2 | version: 1.0.0 3 | 4 | authors: 5 | - Kris Leech 6 | 7 | license: MIT 8 | -------------------------------------------------------------------------------- /spec/integration/evented_spec.cr: -------------------------------------------------------------------------------- 1 | require "../spec_helper" 2 | 3 | class MyPublisher 4 | include Evented::Publisher 5 | 6 | def call(input) 7 | broadcast(:it_happened, input) 8 | end 9 | end 10 | 11 | class MySubscriber 12 | include Evented::Subscriber 13 | getter :event_name, :payload 14 | 15 | def on_event(@event_name, @payload) 16 | end 17 | end 18 | 19 | describe "Publishing and Subscribing to events" do 20 | it "works" do 21 | input = "hello" 22 | publisher = MyPublisher.new 23 | subscriber = MySubscriber.new 24 | publisher.subscribe(subscriber) 25 | publisher.call(input) 26 | subscriber.event_name.should eq :it_happened 27 | subscriber.payload.should eq input 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /spec/spec_helper.cr: -------------------------------------------------------------------------------- 1 | require "spec" 2 | require "../src/evented" 3 | -------------------------------------------------------------------------------- /src/evented.cr: -------------------------------------------------------------------------------- 1 | require "./evented/*" 2 | 3 | module Evented 4 | module Subscriber 5 | end 6 | 7 | module Publisher 8 | def subscribe(listener) 9 | listeners << listener 10 | end 11 | 12 | def broadcast(event_name, *args) 13 | listeners.each do |listener| 14 | listener.on_event(event_name, *args) 15 | end 16 | end 17 | 18 | def listeners 19 | @listeners ||= [] of Evented::Subscriber 20 | end 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /src/evented/version.cr: -------------------------------------------------------------------------------- 1 | require "yaml" 2 | 3 | module Evented 4 | VERSION = (YAML.load(File.read("./shard.yml")) as Hash)["version"] 5 | end 6 | --------------------------------------------------------------------------------