observe 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ 1.9.2-p290
data/README.md CHANGED
@@ -26,6 +26,32 @@ __DIFFERENCES__
26
26
  If you want to notify observers, go ahead & use `notify_observers`.
27
27
  If there is state that needs to be true before observers can be notified, you can use `if(…)`.
28
28
 
29
+ __FULL DISCLOSURE__
30
+
31
+ It can help to know what methods and state is introduced by Observe so your class or module can
32
+ avoid stepping on the toes of Observe by accident.
33
+ When you include Observe into a class or module it adds four methods to your class/module:
34
+
35
+
36
+ * add\_observer
37
+ Adds an observer for the including class/module.
38
+
39
+ * notify\_observers
40
+ Notify observers registered for the including class/module.
41
+
42
+ * destroy\_observers
43
+ Destroy observers registered for the incluing class/module.
44
+
45
+ * observers
46
+ You shouldn't need to use this method.
47
+ It is used internally by the above methods.
48
+
49
+
50
+ It adds the following instance variable:
51
+
52
+ * @observers
53
+ Nothing special, a list of observers used by the 'observers' method.
54
+
29
55
  __EXAMPLE__
30
56
 
31
57
  class Car
@@ -22,9 +22,9 @@ module Observe
22
22
  #
23
23
  def add_observer group, observer=nil, &block
24
24
  if block_given?
25
- observers << Observe.observer.new(group.to_sym, block)
25
+ observers[group.to_sym] << Observe.observer.new(group.to_sym, block)
26
26
  elsif observer
27
- observers << Observe.observer.new(group.to_sym, observer)
27
+ observers[group.to_sym] << Observe.observer.new(group.to_sym, observer)
28
28
  else
29
29
  raise ArgumentError, "A block or object is expected, but none given."
30
30
  end
@@ -43,10 +43,8 @@ module Observe
43
43
  # @return [void]
44
44
  #
45
45
  def notify_observers group, *args
46
- observers.each do |observer|
47
- if observer.group == group.to_sym
48
- observer.object.call(*args)
49
- end
46
+ observers[group.to_sym].each do |observer|
47
+ observer.object.call(*args)
50
48
  end
51
49
  end
52
50
  private :notify_observers
@@ -67,15 +65,19 @@ module Observe
67
65
  if group.nil?
68
66
  observers.clear
69
67
  else
70
- observers.delete_if do |observer|
71
- observer.group == group.to_sym
72
- end
68
+ observers.delete(group.to_sym)
73
69
  end
74
70
  end
75
71
  private :destroy_observers
76
72
 
73
+ #
74
+ # A list of observers registered for a including class or module.
75
+ # There's no need to use this method directly.
76
+ #
77
+ # @return [Hash<Symbol, Array>]
78
+ #
77
79
  def observers
78
- @observers = @observers || []
80
+ @observers = @observers || Hash.new { |h,k| h[k] = [] }
79
81
  end
80
82
  private :observers
81
83
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "observe"
5
- s.version = "0.1.0"
5
+ s.version = "0.2.0"
6
6
  s.authors = ["Rob Gleeson"]
7
7
  s.email = ["rob@flowof.info"]
8
8
  s.homepage = "https://github.com/robgleeson/observe"
@@ -24,7 +24,7 @@ describe Observe do
24
24
  @subject.add_observer(:group) { }
25
25
  @subject.add_observer(:fail) { }
26
26
 
27
- @subject.observers.select { |o| o.group == :group }.size.must_equal(2)
27
+ @subject.observers[:group].size.must_equal(2)
28
28
  end
29
29
 
30
30
  it 'must raise an ArgumentError if no block or object is given.' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: observe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-21 00:00:00.000000000 Z
12
+ date: 2011-11-02 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
16
- requirement: &70221281267820 !ruby/object:Gem::Requirement
16
+ requirement: &70196288545480 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '2.5'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70221281267820
24
+ version_requirements: *70196288545480
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &70221281267300 !ruby/object:Gem::Requirement
27
+ requirement: &70196288544800 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 0.9.2
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70221281267300
35
+ version_requirements: *70196288544800
36
36
  description: A simple interface for adding observers to any class.
37
37
  email:
38
38
  - rob@flowof.info
@@ -41,6 +41,7 @@ extensions: []
41
41
  extra_rdoc_files: []
42
42
  files:
43
43
  - .gitignore
44
+ - .rbenv-version
44
45
  - .travis.yml
45
46
  - .yardopts
46
47
  - Gemfile
@@ -65,7 +66,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
65
66
  version: '0'
66
67
  segments:
67
68
  - 0
68
- hash: -3874655113757391792
69
+ hash: 2329257265388588306
69
70
  required_rubygems_version: !ruby/object:Gem::Requirement
70
71
  none: false
71
72
  requirements:
@@ -74,10 +75,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
75
  version: '0'
75
76
  segments:
76
77
  - 0
77
- hash: -3874655113757391792
78
+ hash: 2329257265388588306
78
79
  requirements: []
79
80
  rubyforge_project: observe
80
- rubygems_version: 1.8.10
81
+ rubygems_version: 1.8.11
81
82
  signing_key:
82
83
  specification_version: 3
83
84
  summary: A simple interface for adding observers to any class.