horizon 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in horizon.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Aaron Jensen
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,65 @@
1
+ # Activeevent
2
+
3
+ Add domain events to your models.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'horizon'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install horizon
18
+
19
+ ## Usage
20
+
21
+ First, publish some domain events:
22
+
23
+ ```ruby
24
+ class Dog < ActiveRecord::Base
25
+ include Horizon::Publisher
26
+
27
+ def feed
28
+ self.hungry = false
29
+ publish :dog_fed, self
30
+ end
31
+ end
32
+ ```
33
+
34
+ Then, handle them:
35
+
36
+ ```ruby
37
+ class DogOwnerNotifier
38
+ include Horizon::Handler
39
+
40
+ def dog_fed(dog)
41
+ mail_owner(dog)
42
+ end
43
+ handler :dog_fed
44
+
45
+ # or
46
+
47
+ handler :dog_fed do |dog|
48
+ mail_owner(dog)
49
+ end
50
+
51
+ # or
52
+
53
+ handler dog_fed: :mail_owner
54
+
55
+ # ...
56
+ end
57
+ ```
58
+
59
+ ## Contributing
60
+
61
+ 1. Fork it
62
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
63
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
64
+ 4. Push to the branch (`git push origin my-new-feature`)
65
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'horizon/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "horizon"
8
+ spec.version = Horizon::VERSION
9
+ spec.authors = ["Aaron Jensen"]
10
+ spec.email = ["aaronjensen@gmail.com"]
11
+ spec.description = %q{horizon}
12
+ spec.summary = %q{horizon}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activesupport", ">= 3.2.0"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec", "~> 2.14.rc1"
26
+ spec.add_development_dependency "pry"
27
+ end
@@ -0,0 +1,8 @@
1
+ require "horizon/version"
2
+ require "horizon/context"
3
+ require "horizon/publisher"
4
+ require "horizon/handler"
5
+ require "horizon/railtie" if defined?(Rails)
6
+
7
+ module Horizon
8
+ end
@@ -0,0 +1,38 @@
1
+ require 'horizon/thread_local_context_store'
2
+
3
+ module Horizon
4
+ class Context
5
+ attr_reader :store
6
+ def self.store
7
+ @store ||= ThreadLocalContextStore.new
8
+ end
9
+
10
+ def self.current
11
+ store.current_context
12
+ end
13
+
14
+ def self.current=(context)
15
+ store.current_context = context
16
+ end
17
+
18
+ def self.reset!
19
+ store.current_context = nil
20
+ end
21
+
22
+ def event_map
23
+ @event_map ||= Hash.new { |h, k| h[k] = Set.new }
24
+ end
25
+
26
+ def add_handler(handler)
27
+ handler.events_handled.each do |event|
28
+ event_map[event] << handler
29
+ end
30
+ end
31
+
32
+ def publish(event, *args)
33
+ event_map[event].each do |handler|
34
+ handler.handle event, *args
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,35 @@
1
+ require 'active_support/concern'
2
+
3
+ module Horizon
4
+ module Handler
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+ def handlers
9
+ @handlers ||= Hash.new { |h, k| h[k] = [] }
10
+ end
11
+
12
+ def events_handled
13
+ handlers.keys
14
+ end
15
+
16
+ def handler(event)
17
+ handlers[event] << lambda { |handler, *args| handler.send event, *args }
18
+ end
19
+ end
20
+
21
+ def events_handled
22
+ self.class.events_handled
23
+ end
24
+
25
+ def handlers
26
+ @handlers ||= curry_handlers
27
+ end
28
+
29
+ def handle(event, *args)
30
+ self.class.handlers[event].each do |handler|
31
+ handler.call self, *args
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,12 @@
1
+ require 'active_support/concern'
2
+ require 'horizon/context'
3
+
4
+ module Horizon
5
+ module Publisher
6
+ extend ActiveSupport::Concern
7
+
8
+ def publish(event, *args)
9
+ Horizon::Context.current.publish event, *args
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ module Horizon
2
+ class Railtie < Rails::Railtie
3
+ # initializer "horizon.set_up_subscriber" do
4
+ # suffix = /\.horizon\Z/
5
+ # ActiveSupport::Notifications.subscribe suffix do |event, *args|
6
+ # event_name = event.gsub(suffix, '').to_sym
7
+ # SubscriptionContext.current.publish event_name, *args
8
+ # end
9
+ # end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Horizon
2
+ class ThreadLocalContextStore
3
+ def current_context=(context)
4
+ Thread.current[:horizon_context] = context
5
+ end
6
+
7
+ def current_context
8
+ Thread.current[:horizon_context] ||= Horizon::Context.new
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module Horizon
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+ require 'horizon'
3
+
4
+ describe "horizon" do
5
+ class Dog
6
+ include Horizon::Publisher
7
+ attr_accessor :hungry
8
+ alias_method :hungry?, :hungry
9
+
10
+ def initialize
11
+ self.hungry = true
12
+ end
13
+
14
+ def feed
15
+ return unless hungry?
16
+
17
+ self.hungry = false
18
+ publish :dog_fed, self
19
+ end
20
+ end
21
+
22
+ class DogOwnerNotifier
23
+ include Horizon::Handler
24
+
25
+ def dog_fed(dog)
26
+ mail_owner(dog)
27
+ end
28
+ handler :dog_fed
29
+
30
+ def mail_owner
31
+ end
32
+ end
33
+
34
+ it "lets you publish and handle domain events" do
35
+ context = Horizon::Context.current
36
+ handler = DogOwnerNotifier.new
37
+ context.add_handler handler
38
+
39
+ handler.stub(:mail_owner)
40
+
41
+ dog = Dog.new
42
+ dog.feed
43
+
44
+ handler.should have_received(:mail_owner).with(dog)
45
+ end
46
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+ require 'horizon/context'
3
+
4
+ describe Horizon::Context do
5
+ let(:context) { Horizon::Context.new }
6
+ let(:handler) { double(:handler, events_handled: [:my_event]).as_null_object }
7
+
8
+ it 'maps published events to handlers' do
9
+ context.add_handler(handler)
10
+
11
+ context.publish(:my_event, 4)
12
+
13
+ handler.should have_received(:handle).with(:my_event, 4)
14
+ end
15
+
16
+ it 'does not send events to handlers that they do not handle' do
17
+ context.add_handler(handler)
18
+
19
+ context.publish(:my_other_event, 4)
20
+
21
+ handler.should_not have_received(:handle)
22
+ end
23
+
24
+ describe '.current' do
25
+ it 'defaults to dispatching thread local contexts' do
26
+ context = Horizon::Context.current
27
+
28
+ Horizon::Context.current.should == context
29
+ thread = Thread.new { Horizon::Context.current.should_not == context }
30
+ thread.join
31
+ end
32
+
33
+ it 'can be reset' do
34
+ context = Horizon::Context.current
35
+
36
+ Horizon::Context.reset!
37
+
38
+ Horizon::Context.current.should be
39
+ Horizon::Context.current.should_not == context
40
+ end
41
+
42
+ it 'can be overridden' do
43
+ context = double :context
44
+ Horizon::Context.current = context
45
+
46
+ Horizon::Context.current == context
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+ require 'horizon/handler'
3
+
4
+ describe Horizon::Handler do
5
+ class MyHandler
6
+ include Horizon::Handler
7
+
8
+ def my_event; end
9
+ handler :my_event
10
+ end
11
+
12
+ describe '.handler' do
13
+ it 'adds handlers' do
14
+ MyHandler.events_handled.should == [:my_event]
15
+ MyHandler.new.events_handled.should == [:my_event]
16
+ end
17
+ end
18
+
19
+ describe '#handle' do
20
+ it 'calls handler for event' do
21
+ handler = MyHandler.new
22
+
23
+ handler.stub(:my_event)
24
+ handler.handle(:my_event, 4)
25
+
26
+ handler.should have_received(:my_event).with(4)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+ require 'horizon/publisher'
3
+
4
+ describe Horizon::Publisher do
5
+ class MyPublisher
6
+ include Horizon::Publisher
7
+ end
8
+
9
+ it 'can publish events' do
10
+ publisher = MyPublisher.new
11
+ context = double(:context).as_null_object
12
+ Horizon::Context.current = context
13
+
14
+ publisher.publish(:my_event, 4)
15
+ context.should have_received(:publish).with(:my_event, 4)
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ require 'pry'
2
+
3
+ RSpec.configure do |config|
4
+ config.treat_symbols_as_metadata_keys_with_true_values = true
5
+ config.run_all_when_everything_filtered = true
6
+ config.filter_run :focus
7
+
8
+ # Run specs in random order to surface order dependencies. If you find an
9
+ # order dependency and want to debug it, you can fix the order by providing
10
+ # the seed, which is printed after each run.
11
+ # --seed 1234
12
+ config.order = 'random'
13
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: horizon
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Aaron Jensen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ version_requirements: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.0
22
+ requirement: !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ! '>='
26
+ - !ruby/object:Gem::Version
27
+ version: 3.2.0
28
+ type: :runtime
29
+ prerelease: false
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ version_requirements: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
38
+ requirement: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '1.3'
44
+ type: :development
45
+ prerelease: false
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ version_requirements: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirement: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ type: :development
61
+ prerelease: false
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 2.14.rc1
70
+ requirement: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 2.14.rc1
76
+ type: :development
77
+ prerelease: false
78
+ - !ruby/object:Gem::Dependency
79
+ name: pry
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirement: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ type: :development
93
+ prerelease: false
94
+ description: horizon
95
+ email:
96
+ - aaronjensen@gmail.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - Gemfile
103
+ - LICENSE.txt
104
+ - README.md
105
+ - Rakefile
106
+ - horizon.gemspec
107
+ - lib/horizon.rb
108
+ - lib/horizon/context.rb
109
+ - lib/horizon/handler.rb
110
+ - lib/horizon/publisher.rb
111
+ - lib/horizon/railtie.rb
112
+ - lib/horizon/thread_local_context_store.rb
113
+ - lib/horizon/version.rb
114
+ - spec/horizon_spec.rb
115
+ - spec/lib/horizon/context_spec.rb
116
+ - spec/lib/horizon/handler_spec.rb
117
+ - spec/lib/horizon/publisher_spec.rb
118
+ - spec/spec_helper.rb
119
+ homepage: ''
120
+ licenses:
121
+ - MIT
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ none: false
134
+ requirements:
135
+ - - ! '>='
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 1.8.25
141
+ signing_key:
142
+ specification_version: 3
143
+ summary: horizon
144
+ test_files:
145
+ - spec/horizon_spec.rb
146
+ - spec/lib/horizon/context_spec.rb
147
+ - spec/lib/horizon/handler_spec.rb
148
+ - spec/lib/horizon/publisher_spec.rb
149
+ - spec/spec_helper.rb