publisher 1.1.1 → 1.1.2

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.
@@ -0,0 +1,12 @@
1
+ pkg
2
+ *.swp
3
+ *.log
4
+ doc
5
+ coverage
6
+ .DS_Store
7
+ .yardoc
8
+ tmp
9
+ tags
10
+ ctags
11
+ .bundle
12
+ *.lock
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in publisher.gemspec
4
+ gemspec
@@ -1,6 +1,6 @@
1
1
  History.txt
2
2
  Manifest.txt
3
- README.txt
3
+ README.rdoc
4
4
  Rakefile
5
5
  lib/publisher.rb
6
6
  test/publisher_test.rb
@@ -2,20 +2,21 @@
2
2
 
3
3
  * http://atomicobject.github.com/publisher
4
4
  * http://github.com/atomicobject/publisher
5
- * http://gemcutter.com/gems/publisher
5
+ * http://rubygems.org/gems/publisher
6
+ * http://rubydoc.info/gems/publisher/frames
6
7
 
7
- == DESCRIPTION:
8
+ == Description
8
9
 
9
10
  publisher is a module for extending a class with event subscription and firing capabilities. This is helpful for implementing objects that participate in the Observer design pattern.
10
11
 
11
- == FEATURES:
12
+ == Features
12
13
 
13
14
  * Nice syntax for declaring events that can be subscribed for
14
15
  * Convenient event firing syntax
15
16
  * Subscribe / unsubscribe functionality
16
17
  * Several method name aliases give you the flexibility to make your code more readable (eg, *fire*, *notify*, *emit*)
17
18
 
18
- == SYNOPSIS:
19
+ == Synopsis
19
20
 
20
21
  require 'rubygems'
21
22
  require 'publisher'
@@ -47,15 +48,15 @@ publisher is a module for extending a class with event subscription and firing c
47
48
  holder.add_customer("Matt")
48
49
  holder.clear_list
49
50
 
50
- == INSTALL:
51
+ == Install
51
52
 
52
- * sudo gem install publisher
53
+ * gem install publisher
53
54
 
54
- == LICENSE:
55
+ == License
55
56
 
56
57
  (The MIT License)
57
58
 
58
- Copyright (c) 2007 Atomic Object
59
+ Copyright (c) 2011 Atomic Object
59
60
 
60
61
  Permission is hereby granted, free of charge, to any person obtaining
61
62
  a copy of this software and associated documentation files (the
data/Rakefile CHANGED
@@ -1,7 +1,5 @@
1
- require 'rubygems'
2
- require 'hoe'
3
- require './lib/publisher.rb'
4
- require 'rake'
1
+ require "bundler/gem_tasks"
2
+
5
3
  require 'rake/testtask'
6
4
 
7
5
  task :default => [ :test ]
@@ -13,19 +11,7 @@ Rake::TestTask.new("test") { |t|
13
11
  t.verbose = true
14
12
  }
15
13
 
16
- #
17
- # Hoe stuff: rubyforge project release
18
- #
19
- Hoe.new('publisher', Publisher::VERSION) do |p|
20
- p.rubyforge_name = 'atomicobjectrb'
21
- p.author = 'Atomic Object'
22
- p.email = 'dev@atomicobject.com'
23
- p.summary = 'Event subscription and firing mechanism'
24
- p.description = p.paragraphs_of('README.txt', 2..5).join("\n\n")
25
- p.url = p.paragraphs_of('README.txt', 1).first.gsub(/\* /,'').split(/\n/)
26
- p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
27
- end
28
-
14
+ # See crosby or fletcher about these tasks
29
15
  if File.exists?("../tools/")
30
16
  load "../tools/tasks/homepage.rake"
31
17
  load "../tools/tasks/release_tagging.rake"
@@ -0,0 +1,37 @@
1
+ require 'rubygems'
2
+ require 'hoe'
3
+ require './lib/publisher.rb'
4
+ require 'rake'
5
+ require 'rake/testtask'
6
+
7
+ task :default => [ :test ]
8
+
9
+ desc "Run the unit tests in test"
10
+ Rake::TestTask.new("test") { |t|
11
+ t.libs << "test"
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ }
15
+
16
+ #
17
+ # Hoe stuff: rubyforge project release
18
+ #
19
+ Hoe.new('publisher', Publisher::VERSION) do |p|
20
+ p.rubyforge_name = 'atomicobjectrb'
21
+ p.author = 'Atomic Object'
22
+ p.email = 'dev@atomicobject.com'
23
+ p.summary = 'Event subscription and firing mechanism'
24
+ p.description = 'publisher is a module for extending a class with event subscription and firing capabilities. This is helpful for implementing objects that participate in the Observer design pattern.'
25
+ p.url = 'http://atomicobject.github.com/publisher'
26
+
27
+ p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
28
+ end
29
+
30
+ if File.exists?("../tools/")
31
+ load "../tools/tasks/homepage.rake"
32
+ load "../tools/tasks/release_tagging.rake"
33
+ ReleaseTagging.new do |t|
34
+ t.package = "publisher"
35
+ t.version = Publisher::VERSION
36
+ end
37
+ end
@@ -1,7 +1,5 @@
1
- # See README.txt for synopsis
1
+ # See README.rdoc for synopsis
2
2
  module Publisher
3
- VERSION = "1.1.1" #:nodoc:#
4
-
5
3
  # Use this method (or one of the aliases) to declare which events you support
6
4
  # Once invoked, your class will have the neccessary supporting methods for subscribing and firing.
7
5
  def has_events(*args)
@@ -42,12 +40,16 @@ module Publisher
42
40
  # Sign up a code block to be executed when an event is fired.
43
41
  # It's important to know the signature of the event, as your proc needs
44
42
  # to accept incoming parameters accordingly.
45
- def subscribe(event, &block)
43
+ def subscribe(event, target=nil, callback=nil, &block)
46
44
  ensure_valid event
47
45
  @subscriptions ||= {}
48
46
  listeners = @subscriptions[event]
49
47
  listeners ||= []
50
- listeners << block
48
+ if target && callback
49
+ listeners << [target, callback]
50
+ else
51
+ listeners << block
52
+ end
51
53
  @subscriptions[event] = listeners
52
54
  end
53
55
  alias :when :subscribe
@@ -59,8 +61,12 @@ module Publisher
59
61
  def unsubscribe(event, listener)
60
62
  ensure_valid event
61
63
  if @subscriptions && @subscriptions[event]
62
- @subscriptions[event].delete_if do |block|
63
- eval('self',block.binding).equal?(listener)
64
+ @subscriptions[event].delete_if do |block_or_target|
65
+ if block_or_target.is_a? Proc
66
+ eval('self',block_or_target.binding).equal?(listener)
67
+ else
68
+ block_or_target[0] == listener
69
+ end
64
70
  end
65
71
  end
66
72
  end
@@ -70,7 +76,13 @@ module Publisher
70
76
  def fire(event, *args) #:nod
71
77
  ensure_valid event
72
78
  listeners = @subscriptions[event] if @subscriptions
73
- listeners.each do |l| l.call(*args) end if listeners
79
+ listeners.each do |l|
80
+ if l.is_a? Array
81
+ l[0].send(l[1],*args)
82
+ else
83
+ l.call(*args)
84
+ end
85
+ end if listeners
74
86
  end
75
87
  alias :emit :fire
76
88
  alias :notify :fire
@@ -0,0 +1,3 @@
1
+ module Publisher
2
+ VERSION = "1.1.2"
3
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "publisher/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.rubyforge_project = "publisher"
7
+ s.name = "publisher"
8
+ s.version = Publisher::VERSION
9
+ s.authors = ["Atomic Object"]
10
+ s.email = ["dev@atomicobject.com"]
11
+ s.homepage = 'http://atomicobject.github.com/publisher'
12
+
13
+ s.summary = 'Event subscription and firing mechanism'
14
+ s.description = 'publisher is a module for extending a class with event subscription and firing capabilities. This is helpful for implementing objects that participate in the Observer design pattern.'
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'publisher'
3
+
4
+ class CustomerListHolder
5
+ extend Publisher
6
+ can_fire :customer_added, :list_cleared
7
+
8
+ def add_customer(cust)
9
+ (@list ||= []) << cust
10
+ fire :customer_added, cust
11
+ end
12
+
13
+ def clear_list
14
+ @list.clear
15
+ fire :list_cleared
16
+ end
17
+ end
18
+
19
+ holder = CustomerListHolder.new
20
+ holder.when :customer_added do |cust|
21
+ puts "Customer added! #{cust}"
22
+ end
23
+ holder.when :list_cleared do
24
+ puts "All gone."
25
+ end
26
+
27
+ holder.add_customer("Croz")
28
+ holder.add_customer("Matt")
29
+ holder.clear_list
30
+
@@ -284,11 +284,32 @@ class PublisherTest < Test::Unit::TestCase
284
284
 
285
285
  assert_equal [ "surprise", "hooray" ], a
286
286
  end
287
+
288
+ def test_new_subscribe_syntax
289
+ @args = nil
290
+ shawn = Shawn.new
291
+ shawn.when :boom, self, :do_it
292
+ shawn.go(:a, :b)
293
+ assert_equal @args, [:a, :b]
294
+ end
295
+
296
+ def test_new_unsubscribe_syntax
297
+ @args = nil
298
+ shawn = Shawn.new
299
+ shawn.when :boom, self, :do_it
300
+ shawn.unsubscribe :boom, self
301
+ shawn.go(:a, :b)
302
+ assert_nil @args
303
+ end
287
304
 
288
305
  #
289
306
  # HELPERS
290
307
  #
291
308
 
309
+ def do_it(*args)
310
+ @args = args
311
+ end
312
+
292
313
  class SomethingWatcher
293
314
  attr_reader :observations
294
315
  def initialize(something)
@@ -425,4 +446,12 @@ class PublisherTest < Test::Unit::TestCase
425
446
  end
426
447
  end
427
448
 
449
+ class Shawn
450
+ extend Publisher
451
+ can_fire :boom
452
+ def go(*args)
453
+ fire :boom, *args
454
+ end
455
+ end
456
+
428
457
  end
metadata CHANGED
@@ -1,88 +1,62 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: publisher
3
- version: !ruby/object:Gem::Version
4
- version: 1.1.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.2
5
+ prerelease:
5
6
  platform: ruby
6
- authors:
7
+ authors:
7
8
  - Atomic Object
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
-
12
- date: 2009-11-08 00:00:00 -05:00
13
- default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: hoe
17
- type: :development
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: 2.3.3
24
- version:
25
- description: |-
26
- == DESCRIPTION:
27
-
28
- publisher is a module for extending a class with event subscription and firing capabilities. This is helpful for implementing objects that participate in the Observer design pattern.
29
-
30
- == FEATURES:
31
-
32
- * Nice syntax for declaring events that can be subscribed for
33
- * Convenient event firing syntax
34
- * Subscribe / unsubscribe functionality
35
- * Several method name aliases give you the flexibility to make your code more readable (eg, *fire*, *notify*, *emit*)
36
-
37
- == SYNOPSIS:
38
-
39
- require 'rubygems'
40
- require 'publisher'
41
- email: dev@atomicobject.com
12
+ date: 2011-09-27 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: publisher is a module for extending a class with event subscription and
15
+ firing capabilities. This is helpful for implementing objects that participate
16
+ in the Observer design pattern.
17
+ email:
18
+ - dev@atomicobject.com
42
19
  executables: []
43
-
44
20
  extensions: []
45
-
46
- extra_rdoc_files:
21
+ extra_rdoc_files: []
22
+ files:
23
+ - .gitignore
24
+ - Gemfile
47
25
  - History.txt
48
26
  - Manifest.txt
49
- - README.txt
50
- files:
51
- - History.txt
52
- - Manifest.txt
53
- - README.txt
27
+ - README.rdoc
54
28
  - Rakefile
29
+ - Rakefile.old
55
30
  - lib/publisher.rb
31
+ - lib/publisher/version.rb
32
+ - publisher.gemspec
33
+ - sample_code/synopsis.rb
56
34
  - test/publisher_test.rb
57
35
  - test/test_helper.rb
58
- has_rdoc: true
59
36
  homepage: http://atomicobject.github.com/publisher
60
37
  licenses: []
61
-
62
38
  post_install_message:
63
- rdoc_options:
64
- - --main
65
- - README.txt
66
- require_paths:
39
+ rdoc_options: []
40
+ require_paths:
67
41
  - lib
68
- required_ruby_version: !ruby/object:Gem::Requirement
69
- requirements:
70
- - - ">="
71
- - !ruby/object:Gem::Version
72
- version: "0"
73
- version:
74
- required_rubygems_version: !ruby/object:Gem::Requirement
75
- requirements:
76
- - - ">="
77
- - !ruby/object:Gem::Version
78
- version: "0"
79
- version:
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
80
54
  requirements: []
81
-
82
- rubyforge_project: atomicobjectrb
83
- rubygems_version: 1.3.5
55
+ rubyforge_project: publisher
56
+ rubygems_version: 1.8.6
84
57
  signing_key:
85
58
  specification_version: 3
86
59
  summary: Event subscription and firing mechanism
87
- test_files:
60
+ test_files:
61
+ - test/publisher_test.rb
88
62
  - test/test_helper.rb