spray_vent 1.0.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 +7 -0
- data/lib/spray_vent.rb +27 -0
- data/lib/spray_vent/errors/invalid_handler_error.rb +10 -0
- data/lib/spray_vent/event.rb +29 -0
- data/lib/spray_vent/mediator.rb +72 -0
- metadata +48 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9cebd3fc39cdcc5279ffcf9510357f90a4bb509e
|
4
|
+
data.tar.gz: 711b031778657fcd7c5c4a53393c3ed70a74d62e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c544f24ea297c4236502c58b941e8bc77d07172075065efd4ad63eae9735dc33f2a7fe4d84c6e96036cadaee4d3128698782b2cb8e4d7387713e9cfe482870f0
|
7
|
+
data.tar.gz: 648e3af25a0946814b125bdd541ed6f0042b6bfa6d3c19d94a128e12e2fd516b73e1086eaeda487c5c390ef0668b94880bfee5b164acd8fa8bfdf0af7462a571
|
data/lib/spray_vent.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# @author Charles Sprayberry
|
2
|
+
# @copyright Copyright (c) 2013 Charles Sprayberry
|
3
|
+
# @license OSI MIT
|
4
|
+
|
5
|
+
Dir[File.dirname(__FILE__) + "/**/*.rb"].each {|f| require f}
|
6
|
+
|
7
|
+
# The module that exposes the primary API that allows any object to easily handle events being added and triggered on it
|
8
|
+
#
|
9
|
+
# Please note that we are using prefixes on instance variables ON PURPOSE; please do not remove the instance prefixes
|
10
|
+
module SprayVent
|
11
|
+
|
12
|
+
# Sets up the event mediator that will be used to manage the instances events
|
13
|
+
#
|
14
|
+
# @return void
|
15
|
+
def event_init
|
16
|
+
@eventable_events = Mediator.new(self)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Returns the event mediator attached to the instance that handles the adding and triggering of events
|
20
|
+
#
|
21
|
+
# @return Eventable::Mediator
|
22
|
+
def events
|
23
|
+
event_init if @eventable_events.nil?
|
24
|
+
@eventable_events
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
#
|
2
|
+
# @author Charles Sprayberry
|
3
|
+
# @copyright Copyright (c) 2013 Charles Sprayberry
|
4
|
+
# @license OSI MIT
|
5
|
+
|
6
|
+
module SprayVent
|
7
|
+
class Event
|
8
|
+
|
9
|
+
attr_reader :target, :name, :args
|
10
|
+
@cancel = false
|
11
|
+
|
12
|
+
def initialize(name, target, args = nil)
|
13
|
+
@name = name
|
14
|
+
@target = target
|
15
|
+
args ||= []
|
16
|
+
@args = args
|
17
|
+
end
|
18
|
+
|
19
|
+
def cancel
|
20
|
+
@cancel = true
|
21
|
+
end
|
22
|
+
|
23
|
+
def cancel?
|
24
|
+
@cancel
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# @author Charles Sprayberry
|
2
|
+
# @copyright Copyright (c) 2013 Charles Sprayberry, Dyana Sprayberry
|
3
|
+
# @license OSI MIT
|
4
|
+
|
5
|
+
module SprayVent
|
6
|
+
|
7
|
+
# Object responsible for storing and triggering handlers associated to particular events.
|
8
|
+
#
|
9
|
+
# This is the primarily used object when working Eventable.
|
10
|
+
class Mediator
|
11
|
+
|
12
|
+
# Ensures that a collection of events is ready.
|
13
|
+
def initialize(target)
|
14
|
+
@events = {}
|
15
|
+
@target = target
|
16
|
+
end
|
17
|
+
|
18
|
+
# Adds an event handler attached to the event associated to name symbol.
|
19
|
+
#
|
20
|
+
# Although this implementation allows passing multiple parameters only the 2nd parameter
|
21
|
+
# passed will be used and that value MUST respond_to :call.
|
22
|
+
#
|
23
|
+
# @param name [String] the name of the event you want to invoke when
|
24
|
+
# @param args [Array] an array of arguments passed to the method
|
25
|
+
# @raises [Eventable::InvalidHandlerError]
|
26
|
+
def method_missing(name, *args)
|
27
|
+
handler = block_given? ? Proc.new : args[0]
|
28
|
+
raise invalid_handler if !handler.respond_to? :call
|
29
|
+
|
30
|
+
name = name.to_sym
|
31
|
+
@events[name] = [] if @events[name].nil?
|
32
|
+
@events[name] << handler
|
33
|
+
end
|
34
|
+
|
35
|
+
# Retrieves a collection of event handlers associated to the event_name passed.
|
36
|
+
#
|
37
|
+
# @param event_name [String] the name of the event collection you are wanting to retrieve
|
38
|
+
# @param event_name [Symbol] the name of the event collection you are wanting to retrieve
|
39
|
+
# @return [Array]
|
40
|
+
def [](event_name)
|
41
|
+
@events[event_name.to_sym]
|
42
|
+
end
|
43
|
+
|
44
|
+
# Will create an Eventable::Event associated to event_name and invoke all handlers associated to that
|
45
|
+
# event_name.
|
46
|
+
#
|
47
|
+
# @param event_name [String] handlers associated to event name passed
|
48
|
+
# @param event_name [Symbol] handlers associated to event name passed
|
49
|
+
# @return [Eventable::Event]
|
50
|
+
def trigger(event_name, *args)
|
51
|
+
name = event_name.to_sym
|
52
|
+
# we are creating this outside of the unless block because we want to return the event every time
|
53
|
+
event = SprayVent::Event.new name, @target, args
|
54
|
+
unless @events[name].nil?
|
55
|
+
@events[name].each do |handler|
|
56
|
+
break if event.cancel?
|
57
|
+
handler.call event
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def invalid_handler
|
65
|
+
InvalidHandlerError.new 'An object that responds to :call must be provided to Eventable::Mediator when adding event handlers'
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: spray_vent
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Charles Sprayberry
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-19 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A library that allows you to easily mixin spray_vent behavior on a variety
|
14
|
+
of objects
|
15
|
+
email: cspray+spray_vent-ruby@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/spray_vent.rb
|
21
|
+
- lib/spray_vent/errors/invalid_handler_error.rb
|
22
|
+
- lib/spray_vent/event.rb
|
23
|
+
- lib/spray_vent/mediator.rb
|
24
|
+
homepage: https://github.com/cspray/spray_vent
|
25
|
+
licenses:
|
26
|
+
- MIT
|
27
|
+
metadata: {}
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubyforge_project:
|
44
|
+
rubygems_version: 2.2.1
|
45
|
+
signing_key:
|
46
|
+
specification_version: 4
|
47
|
+
summary: spray_vent
|
48
|
+
test_files: []
|