foca-beacon 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. data/.gitignore +2 -0
  2. data/LICENSE +23 -0
  3. data/README.rdoc +57 -0
  4. data/Rakefile +34 -0
  5. data/beacon.gemspec +30 -0
  6. data/lib/beacon.rb +41 -0
  7. metadata +77 -0
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ doc
2
+ *.gem
data/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2008-2009 Nicolas Sanguinetti, entp.com
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 NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
data/README.rdoc ADDED
@@ -0,0 +1,57 @@
1
+ = Beacon
2
+
3
+ Simple observers and notifiers for your code.
4
+
5
+ The library aims to provide the simplest implementation possible. You get
6
+ <i>beacons</i>, which are always global and can be "lit up". Whenever you
7
+ light a beacon up, everyone watching will act as instructed.
8
+
9
+ == How to use this?
10
+
11
+ Whenever you want to fire an event from your code, call <tt>Beacon.fire</tt>:
12
+
13
+ def do_something
14
+ Beacon.fire(:before_doing_something)
15
+ # do your thing
16
+ end
17
+
18
+ In order to register listeners for that event, call <tt>Beacon.watch</tt>:
19
+
20
+ Beacon.watch :before_doing_something do
21
+ logger.info "I'm about to do something"
22
+ end
23
+
24
+ Each time you call <tt>watch</tt> with a given name, you register a different
25
+ handler for that name. Each time you call <tt>fire</tt> <b>all</b> handlers
26
+ for that event are run, in the order they were registered.
27
+
28
+ == Passing arguments
29
+
30
+ If you want to pass arguments to the watchers, just pass them along in
31
+ <tt>Beacon.fire</tt>:
32
+
33
+ Beacon.fire(:an_event, "cuack", 3)
34
+
35
+ And you'll get them as arguments on the block that handles the message:
36
+
37
+ Beacon.watch :an_event do |object, index|
38
+ # here object == "cuack", index == 3
39
+ end
40
+
41
+ == Advanced handlers
42
+
43
+ Instead of blocks, <tt>Beacon.watch</tt> can receive any object that responds
44
+ to <tt>call</tt>, so if you need any advanced logic in your handlers, you can
45
+ declare them like this:
46
+
47
+ class MyHandler
48
+ def call(foo, bar=0)
49
+ puts foo.inspect, bar
50
+ end
51
+ end
52
+
53
+ Beacon.watch :an_event, MyHandler.new
54
+
55
+ == License
56
+
57
+ The code is licensed under an MIT license. Check the LICENSE file for details.
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ require 'spec/rake/spectask'
2
+
3
+ begin
4
+ require "hanna/rdoctask"
5
+ rescue LoadError
6
+ require "rake/rdoctask"
7
+ end
8
+
9
+ begin
10
+ require "metric_fu"
11
+ rescue LoadError
12
+ end
13
+
14
+ begin
15
+ require "mg"
16
+ MG.new("beacon.gemspec")
17
+ rescue LoadError
18
+ end
19
+
20
+ desc "Default: run all tests"
21
+ task :default => :spec
22
+
23
+ desc "Run library tests"
24
+ Spec::Rake::SpecTask.new do |t|
25
+ t.spec_files = FileList['spec/**/*_spec.rb']
26
+ t.spec_opts = %w(-fs --color)
27
+ end
28
+
29
+ Rake::RDocTask.new do |rd|
30
+ rd.main = "README"
31
+ rd.title = "Beacon Documentation"
32
+ rd.rdoc_files.include("README.rdoc", "LICENSE", "lib/**/*.rb")
33
+ rd.rdoc_dir = "doc"
34
+ end
data/beacon.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "beacon"
3
+ s.version = "0.1"
4
+ s.date = "2009-04-25"
5
+
6
+ s.description = "Simple and straightforward observers for your code"
7
+ s.summary = "Simple and straightforward observers for your code"
8
+ s.homepage = "http://github.com/foca/beacon"
9
+
10
+ s.authors = ["Nicolás Sanguinetti"]
11
+ s.email = "contacto@nicolassanguinetti.info"
12
+
13
+ s.require_paths = ["lib"]
14
+ s.has_rdoc = true
15
+ s.rubygems_version = "1.3.1"
16
+
17
+ if s.respond_to?(:add_development_dependency)
18
+ s.add_development_dependency "sr-mg"
19
+ s.add_development_dependency "rspec"
20
+ end
21
+
22
+ s.files = %w[
23
+ .gitignore
24
+ LICENSE
25
+ README.rdoc
26
+ Rakefile
27
+ beacon.gemspec
28
+ lib/beacon.rb
29
+ ]
30
+ end
data/lib/beacon.rb ADDED
@@ -0,0 +1,41 @@
1
+ module Beacon
2
+ # Fire an event to be processed by all the watchers. You pass the event name
3
+ # and any arguments you want passed to the event handlers.
4
+ #
5
+ # Beacon.fire(:some_event, "an argument", 2, "another")
6
+ def self.fire(event, *args)
7
+ events[event].each do |callback|
8
+ callback.call(*args)
9
+ end
10
+ end
11
+
12
+ # Register a callback for a given event. Each time you call <tt>fire</tt> then
13
+ # all the callbacks registered for that name will be called in order.
14
+ #
15
+ # Beacon.watch :some_event do |foo, bar, baz|
16
+ # # do stuff with foo, bar, and baz
17
+ # end
18
+ #
19
+ # Instead of passing a block, you can pass any object that responds to
20
+ # <tt>#call</tt>. Like this:
21
+ #
22
+ # class MyHandler
23
+ # def call(foo=1, bar=2, baz=3)
24
+ # puts foo, bar, baz
25
+ # end
26
+ # end
27
+ #
28
+ # Beacon.watch :some_event, MyHandler.new
29
+ def self.watch(event, handler=nil, &default_handler)
30
+ if handler && block_given?
31
+ raise ArgumentError, "You cannot register a handler with both a block and an object"
32
+ end
33
+ handler = handler || default_handler || raise(ArgumentError, "You must provide a handler")
34
+ events[event] << handler
35
+ end
36
+
37
+ def self.events
38
+ @events ||= Hash.new {|h,k| h[k] = [] }
39
+ end
40
+ private_class_method :events
41
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: foca-beacon
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - "Nicol\xC3\xA1s Sanguinetti"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-25 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: sr-mg
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description: Simple and straightforward observers for your code
36
+ email: contacto@nicolassanguinetti.info
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - .gitignore
45
+ - LICENSE
46
+ - README.rdoc
47
+ - Rakefile
48
+ - beacon.gemspec
49
+ - lib/beacon.rb
50
+ has_rdoc: true
51
+ homepage: http://github.com/foca/beacon
52
+ post_install_message:
53
+ rdoc_options: []
54
+
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.2.0
73
+ signing_key:
74
+ specification_version: 2
75
+ summary: Simple and straightforward observers for your code
76
+ test_files: []
77
+