observe 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+
6
+ /doc/
data/.travis.yml ADDED
@@ -0,0 +1,13 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - rbx-2.0
6
+ - jruby
7
+ - ruby-head
8
+ - ree
9
+
10
+ notifications:
11
+ irc: "irc.freenode.org#flowof.info"
12
+ recipients:
13
+ - rob@flowof.info
data/.yardopts ADDED
@@ -0,0 +1,10 @@
1
+ -M redcarpet
2
+ -m markdown
3
+ --private
4
+ --no-private
5
+ --hide-void-return
6
+ --no-cache
7
+ -
8
+ LICENSE.txt
9
+
10
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in captainhook.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (C) 2011 by Robert Gleeson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
20
+
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ __OVERVIEW__
2
+
3
+ | Project | Observe
4
+ |:----------------|:--------------------------------------------------
5
+ | Homepage | https://github.com/robgleeson/observe
6
+ | Documentation | http://rubydoc.info/gems/observe/frames
7
+ | Author | Rob Gleeson
8
+
9
+
10
+ __DESCRIPTION__
11
+
12
+ A simple interface for adding observers to any class.
13
+ The interface is similar to and takes ideas from the 'Observable' module in the Ruby standard library.
14
+
15
+ __DIFFERENCES__
16
+
17
+ There are a few differences between 'Observe' and the 'Observable' module:
18
+
19
+ * An observer is any object that responds to `call`.
20
+ Proc objects can be observers out of the box.
21
+
22
+ * Observers belong to 'groups'.
23
+ In the example below, `:ignition` is a group, as is `:brakes`.
24
+
25
+ * There is no `changed` method.
26
+ If you want to notify observers, go ahead & use `notify_observers`.
27
+ If there is state that needs to be true before observers can be notified, you can use `if(…)`.
28
+
29
+ __EXAMPLE__
30
+
31
+ class Car
32
+
33
+ include Observe
34
+
35
+ def initialize
36
+ add_observer(:ignition) { .. }
37
+ add_observer(:brakes) { .. }
38
+ end
39
+
40
+ def drive
41
+ notify_observers(:ignition)
42
+ puts "Vroom Vroom."
43
+ notify_observers(:brakes)
44
+ end
45
+
46
+ end
47
+
48
+ Car.new.drive
49
+
50
+ __INSTALL__
51
+
52
+ gem install observe
53
+
54
+ __LICENSE__
55
+
56
+
57
+ See LICENSE.txt
58
+
59
+
60
+
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.name = :test
6
+ t.test_files = FileList["test/*.rb"]
7
+ end
8
+
9
+ task :default => :test
data/lib/observe.rb ADDED
@@ -0,0 +1,82 @@
1
+ module Observe
2
+
3
+ @observer = Struct.new(:group, :object)
4
+
5
+ class << self
6
+ attr_reader :observer
7
+ end
8
+
9
+ #
10
+ # Add a observer to a class.
11
+ #
12
+ # @example
13
+ # add_observer(:group) { puts "Hello!" }
14
+ #
15
+ # @param [#to_sym] group
16
+ # The name of a group that an observer belongs to.
17
+ #
18
+ # @param [#call] observer
19
+ # Any object that can respond to call.
20
+ #
21
+ # @return [void]
22
+ #
23
+ def add_observer group, observer=nil, &block
24
+ if block_given?
25
+ observers << Observe.observer.new(group.to_sym, block)
26
+ elsif observer
27
+ observers << Observe.observer.new(group.to_sym, observer)
28
+ else
29
+ raise ArgumentError, "A block or object is expected, but none given."
30
+ end
31
+ end
32
+ private :add_observer
33
+
34
+ #
35
+ # Notify a observer.
36
+ #
37
+ # @example
38
+ # notify_observers :group
39
+ #
40
+ # @param [#to_sym] group
41
+ # The name of a group
42
+ #
43
+ # @return [void]
44
+ #
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
50
+ end
51
+ end
52
+ private :notify_observers
53
+
54
+ #
55
+ # Destroy a observer.
56
+ #
57
+ # @example
58
+ # destroy_observers :group
59
+ #
60
+ # @param [#to_sym] group
61
+ # The name of a group.
62
+ # Without this argument, all observers are destroyed.
63
+ #
64
+ # @return [void]
65
+ #
66
+ def destroy_observers group=nil
67
+ if group.nil?
68
+ observers.clear
69
+ else
70
+ observers.delete_if do |observer|
71
+ observer.group == group.to_sym
72
+ end
73
+ end
74
+ end
75
+ private :destroy_observers
76
+
77
+ def observers
78
+ @observers = @observers || []
79
+ end
80
+ private :observers
81
+
82
+ end
data/observe.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "observe"
5
+ s.version = "0.1.0"
6
+ s.authors = ["Rob Gleeson"]
7
+ s.email = ["rob@flowof.info"]
8
+ s.homepage = "https://github.com/robgleeson/observe"
9
+ s.summary = "A simple interface for adding observers to any class."
10
+ s.description = s.summary
11
+
12
+ s.rubyforge_project = "observe"
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ s.add_development_dependency "minitest", "~> 2.5"
20
+ s.add_development_dependency "rake" , "~> 0.9.2"
21
+ end
@@ -0,0 +1,85 @@
1
+ require File.expand_path("setup.rb", File.dirname(__FILE__))
2
+
3
+ describe Observe do
4
+
5
+ context 'add_observers' do
6
+ before do
7
+ klass = Class.new do
8
+ include Observe
9
+
10
+ public :add_observer
11
+ public :observers
12
+ end
13
+
14
+ @subject = klass.new
15
+ end
16
+
17
+ it "must add a observer." do
18
+ @subject.add_observer(:group) { }
19
+ @subject.observers.size.must_equal(1)
20
+ end
21
+
22
+ it "must add two observers to the same group." do
23
+ @subject.add_observer(:group) { }
24
+ @subject.add_observer(:group) { }
25
+ @subject.add_observer(:fail) { }
26
+
27
+ @subject.observers.select { |o| o.group == :group }.size.must_equal(2)
28
+ end
29
+
30
+ it 'must raise an ArgumentError if no block or object is given.' do
31
+ proc { @subject.add_observer(:group) }.must_raise(ArgumentError)
32
+ end
33
+ end
34
+
35
+ context 'notify_observers' do
36
+ before do
37
+ klass = Class.new do
38
+ include Observe
39
+ attr_reader :notified
40
+
41
+ def initialize
42
+ add_observer(:group) { @notified = true }
43
+ end
44
+
45
+ def notify!
46
+ notify_observers(:group)
47
+ end
48
+ end
49
+
50
+ @subject = klass.new
51
+ end
52
+
53
+ it 'must notify observers.' do
54
+ @subject.notify!
55
+ @subject.notified.must_equal(true)
56
+ end
57
+ end
58
+
59
+ context 'destroy_observers' do
60
+ before do
61
+ klass = Class.new do
62
+ include Observe
63
+
64
+ def initialize
65
+ add_observer(:group) { }
66
+ end
67
+
68
+ def destroy!
69
+ destroy_observers(:group)
70
+ end
71
+
72
+ public :observers
73
+ end
74
+
75
+ @subject = klass.new
76
+ end
77
+
78
+ it 'must destroy all observers.' do
79
+ @subject.destroy!
80
+ @subject.observers.must_be_empty
81
+ end
82
+ end
83
+
84
+ end
85
+
data/test/setup.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/setup'
2
+ require 'observe'
3
+ require 'minitest/spec'
4
+ require 'minitest/autorun'
5
+
6
+ alias :context :describe
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: observe
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rob Gleeson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: minitest
16
+ requirement: &70221281267820 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.5'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70221281267820
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &70221281267300 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 0.9.2
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70221281267300
36
+ description: A simple interface for adding observers to any class.
37
+ email:
38
+ - rob@flowof.info
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - .travis.yml
45
+ - .yardopts
46
+ - Gemfile
47
+ - LICENSE.txt
48
+ - README.md
49
+ - Rakefile
50
+ - lib/observe.rb
51
+ - observe.gemspec
52
+ - test/observe_test.rb
53
+ - test/setup.rb
54
+ homepage: https://github.com/robgleeson/observe
55
+ licenses: []
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ segments:
67
+ - 0
68
+ hash: -3874655113757391792
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ segments:
76
+ - 0
77
+ hash: -3874655113757391792
78
+ requirements: []
79
+ rubyforge_project: observe
80
+ rubygems_version: 1.8.10
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: A simple interface for adding observers to any class.
84
+ test_files:
85
+ - test/observe_test.rb
86
+ - test/setup.rb