Skip to content

Commit

Permalink
moving to ruby 2.0 syntax of using refinements
Browse files Browse the repository at this point in the history
  • Loading branch information
stephan-nordnes-eriksen committed Feb 16, 2015
1 parent 905c5d4 commit 418d75c
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 77 deletions.
29 changes: 22 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,21 @@ Or install it yourself as:

$ gem install event_aggregator

#TODO: MAKE A MUCH BETTER README.
It is very confusing now, I know, sorry.

## Usage

Version 2.X. For version 1.X see below
Version 2.X syntax below. Only works with ruby 2.0+. For version 1.X of event_aggregator see below.

#!/usr/bin/ruby

require "rubygems"
require "event_aggregator"

using EventAggregator #This will enable the shorthand notation shown here. Without this, you must use the 1.X style syntax. It uses ruby 2.0 refinements syntax. Read up on how this works in relation to namespaces. One good article: http://timelessrepo.com/refinements-in-ruby


class Foo
#The receiving method is used to register which events you want to receive when they are "published"
#Multiple event types can be chained to the same callback.
Expand All @@ -61,27 +67,36 @@ Version 2.X. For version 1.X see below
def ip_address(data)
return "magically get IP address here"
end

#Used as an example here of how to unregister for types
def unregister_from_type(type)
event_type_unregister(type)
end
end

f = Foo.new



#Events are published to the world. These events are asynchronous by default.
event_publish("foo", "some data")
#=> some data
#The same can be achieved out of context of the using refinement by
EventAggregator::Event.new("foo", "some data").publish
#=> some data
EA::Event.new("bar", 1).publish
EA::E.new("bar", 1).publish #Or by shorthand
#=> Event is handled:
#=> 1
EA::E.new("foo3", "data").publish
event_publish("foo3", "data") #nothing will happen
#=> []
#TODO: The below is currently un-implemented
f.foo_unregister("foo2")
EA::E.new("foo2", "data").publish
f.unregister_from_type("foo2")
event_publish("foo2", "data")
#=> []

#The world is queried for the following. These events are synchroneus by default.
EA::E.new("speed of light", nil).request
#=> 299792458
EA::E.new("speed of sound", nil).request
event_request("speed of sound", nil)
#=> nil

#The results can be stored to variables because the request is synchroneus.
Expand Down
7 changes: 1 addition & 6 deletions lib/event_aggregator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@
require "event_aggregator/listener"
require "event_aggregator/event"
require "event_aggregator/event_job"
require "event_aggregator/patches"



module EventAggregator

end

EA = EventAggregator

require "event_aggregator/patches"
110 changes: 57 additions & 53 deletions lib/event_aggregator/patches.rb
Original file line number Diff line number Diff line change
@@ -1,74 +1,78 @@
module EventAggregator

#In this file you will find patches to different objects and classes for conveniency classes.
#In this file you will find patches to different objects and classes for conveniency classes.

# Public: Monkey patching the Class class to add the awesome attr_accessor style methods to other classes
#
# Examples
#
# class Foo
# receiving "I want this event type", "and this", lambda{|e| puts "and do this with them"}
# receiving "Ohh!! And also this", lambda{|e| puts "but only do this"}
# responding "This one I know!", lambda{|e| puts "the answer is #{e+1}"}
# responding "This also", "and this", lambda{|e| puts "the answer is #{e+2}"}
# end
# foo = Foo.new
#
class Class
def receiving(*args)
type_callback = verify_event_aggregator_args(args)
# Public: Monkey patching the Class class to add the awesome attr_accessor style methods to other classes
#
# Examples
#
# class Foo
# using EventAggregator
# receiving "I want this event type", "and this", lambda{|e| puts "and do this with them"}
# receiving "Ohh!! And also this", lambda{|e| puts "but only do this"}
# responding "This one I know!", lambda{|e| puts "the answer is #{e+1}"}
# responding "This also", "and this", lambda{|e| puts "the answer is #{e+2}"}
# end
# foo = Foo.new
#
refine Class do
def receiving(*args)
type_callback = verify_event_aggregator_args(args)

self.class_eval do
original_method = instance_method(:initialize)
define_method(:initialize) do |*args, &block|
original_method.bind(self).call(*args, &block)

type_callback[:types].each do |type|
EA::Aggregator.register( self, type, type_callback[:callback])
self.class_eval do
original_method = instance_method(:initialize)
define_method(:initialize) do |*args, &block|
original_method.bind(self).call(*args, &block)

type_callback[:types].each do |type|
EA::Aggregator.register( self, type, type_callback[:callback])
end
end
end
end
end

def receive_all(*args)
type_callback = verify_event_aggregator_args(args)
def receive_all(*args)
type_callback = verify_event_aggregator_args(args)

self.class_eval do
original_method = instance_method(:initialize)
self.class_eval do
original_method = instance_method(:initialize)

define_method(:initialize) do |*args, &block|
original_method.bind(self).call(*args, &block)
EA::Aggregator.register_all(self, type_callback[:callback])
define_method(:initialize) do |*args, &block|
original_method.bind(self).call(*args, &block)
EA::Aggregator.register_all(self, type_callback[:callback])
end
end
end
end

def responding(*args)
type_callback = verify_event_aggregator_args(args)
def responding(*args)
type_callback = verify_event_aggregator_args(args)

self.class_eval do
original_method = instance_method(:initialize)
define_method(:initialize) do |*args, &block|
original_method.bind(self).call(*args, &block)

type_callback[:types].each do |type|
EA::Aggregator.register_producer(self, type, type_callback[:callback])
self.class_eval do
original_method = instance_method(:initialize)
define_method(:initialize) do |*args, &block|
original_method.bind(self).call(*args, &block)

type_callback[:types].each do |type|
EA::Aggregator.register_producer(self, type, type_callback[:callback])
end
end
end
end

private
def verify_event_aggregator_args(args)
raise "No callback provided" unless args[-1].respond_to?(:call) || args[-1].is_a?(String) || args[-1].is_a?(Symbol)

return {
:types => args[0..-2],
:callback => args[-1]
}
end
end

private
def verify_event_aggregator_args(args)
raise "No callback provided" unless args[-1].respond_to?(:call) || args[-1].is_a?(String) || args[-1].is_a?(Symbol)

return {
:types => args[0..-2],
:callback => args[-1]
}
#TODO: Consider if this is a sin or not. It can interfere with other stuff. Maybe rename to something like event_aggregator_publish
refine Object do
include EventAggregator::Listener
end
end

#TODO: Consider if this is a sin or not. It can interfere with other stuff. Maybe rename to something like event_aggregator_publish
class Object
include EventAggregator::Listener
end
12 changes: 4 additions & 8 deletions spec/lib/event_aggregator/listener_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
describe EventAggregator::Listener do
let(:listener) { (Class.new { include EventAggregator::Listener }).new }
let(:listener_class) { Class.new { include EventAggregator::Listener } }
let(:event_type) { Faker::Name.name }
let(:callback) { lambda { |data| } }
let(:event_type) { Faker::Name.name }
let(:callback) { lambda { |data| } }
let(:data) { Faker::Name.name }
let(:recieve_all_method) { lambda { |event| } }

Expand Down Expand Up @@ -73,23 +73,19 @@

describe "event_publish(type, data, async = true, consisten_data = true)" do
it "creates and publishes new Event" do
a = Object.new

message_spy = spy("message spy")
expect(EA::E).to receive(:new).with("type", "data", true, true).and_return(message_spy)
expect(message_spy).to receive(:publish)
a.send(:event_publish, "type", "data")
listener.send(:event_publish, "type", "data")
end
end

describe "event_request(type, data)" do
it "creates and request new Event" do
a = Object.new

message_spy = spy("message spy")
expect(EA::E).to receive(:new).with("type", "data").and_return(message_spy)
expect(message_spy).to receive(:request)
a.send(:event_request, "type", "data")
listener.send(:event_request, "type", "data")
end
end
end
Loading

0 comments on commit 418d75c

Please sign in to comment.