lorikeet 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +25 -0
- data/Rakefile +5 -0
- data/lib/lorikeet.rb +50 -0
- data/lib/lorikeet/version.rb +3 -0
- data/lorikeet.gemspec +12 -0
- data/spec/lorikeet_spec.rb +118 -0
- metadata +52 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 850cc78e4587fbdd4ccea67ea78e1012bd2db6ec
|
4
|
+
data.tar.gz: 7339a55d2000abccbd90361f3ccef332a88de2ed
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3f28f4f72b3170388482eff31b66e0744cc69e925500edf4b2801426f1a314e6435247e4e311a7ade3c5b54c395f28492433dbe4010958fe899301a9de4c5b48
|
7
|
+
data.tar.gz: 047ce50587497a2ec3d094998e766d2a6ec8c076dc3c582d3484b6a85a2c7059e42521afaa93fa2b4256edb68e2eae27fc2c54a8b1f49d5da15cdc460ffc58b1
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Steven Weiss
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
Lorikeet
|
2
|
+
========
|
3
|
+
|
4
|
+
Lorikeet is a tiny library for implementing the observer pattern.
|
5
|
+
|
6
|
+
Installation
|
7
|
+
------------
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'lorikeet'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then you can install Lorikeet with bundler with via:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or without bundler:
|
20
|
+
|
21
|
+
$ gem install lorikeet
|
22
|
+
|
23
|
+
For RubyMotion, you must also require the gem inside your Gemfile:
|
24
|
+
|
25
|
+
$ require 'lorikeet'
|
data/Rakefile
ADDED
data/lib/lorikeet.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
module Lorikeet
|
2
|
+
class Observable
|
3
|
+
def initialize(context, observers = [])
|
4
|
+
@context = context
|
5
|
+
@observers = observers
|
6
|
+
end
|
7
|
+
|
8
|
+
def include?(observer)
|
9
|
+
observers.include?(observer)
|
10
|
+
end
|
11
|
+
|
12
|
+
def <<(observer)
|
13
|
+
observers << observer unless observers.include?(observer)
|
14
|
+
end
|
15
|
+
|
16
|
+
def delete(observer)
|
17
|
+
observers.delete(observer)
|
18
|
+
end
|
19
|
+
|
20
|
+
def action
|
21
|
+
yield(context)
|
22
|
+
|
23
|
+
observers.each { |o| o.send(o.callback_id, context) }
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
attr_reader :context, :observers
|
29
|
+
end
|
30
|
+
|
31
|
+
module Observer
|
32
|
+
DEFAULT_CALLBACK_ID = :callback
|
33
|
+
|
34
|
+
def observe(observable, callback_id = nil)
|
35
|
+
if callback_id
|
36
|
+
self.callback_id = callback_id
|
37
|
+
end
|
38
|
+
|
39
|
+
observable << self
|
40
|
+
end
|
41
|
+
|
42
|
+
def callback_id
|
43
|
+
@callback_id ||= DEFAULT_CALLBACK_ID
|
44
|
+
end
|
45
|
+
|
46
|
+
def callback_id=(sym)
|
47
|
+
@callback_id = sym
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/lorikeet.gemspec
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require_relative './lib/lorikeet/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'lorikeet'
|
5
|
+
s.summary = 'Lorikeet'
|
6
|
+
s.version = Lorikeet::VERSION
|
7
|
+
s.authors = ['Steve Weiss']
|
8
|
+
s.email = ['weissst@mail.gvsu.edu']
|
9
|
+
s.homepage = 'https://github.com/sirscriptalot/lorikeet'
|
10
|
+
s.license = 'MIT'
|
11
|
+
s.files = `git ls-files`.split("\n")
|
12
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require_relative '../lib/lorikeet'
|
3
|
+
|
4
|
+
class Counter
|
5
|
+
attr_reader :count
|
6
|
+
|
7
|
+
def initialize(count = 0)
|
8
|
+
@count = 0
|
9
|
+
end
|
10
|
+
|
11
|
+
def increment
|
12
|
+
@count += 1
|
13
|
+
end
|
14
|
+
|
15
|
+
def decrement
|
16
|
+
@count -= 1
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class Observer
|
21
|
+
include Lorikeet::Observer
|
22
|
+
|
23
|
+
attr_reader :received
|
24
|
+
|
25
|
+
def callback(context)
|
26
|
+
@received = context
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe 'Lorikeet::Observable' do
|
31
|
+
before do
|
32
|
+
@observable = Lorikeet::Observable.new(Counter.new)
|
33
|
+
@observer = Observer.new
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '#include?' do
|
37
|
+
it 'returns bool for if observer is in observers' do
|
38
|
+
refute @observable.include?(@observer)
|
39
|
+
|
40
|
+
@observable.instance_variable_set(:@observers, [@observer])
|
41
|
+
|
42
|
+
assert @observable.include?(@observer)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe '#<<' do
|
47
|
+
it 'adds the observer if it does not exist' do
|
48
|
+
refute @observable.include?(@observer)
|
49
|
+
|
50
|
+
@observable << @observer
|
51
|
+
|
52
|
+
assert @observable.include?(@observer)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe '#delete' do
|
57
|
+
it 'removes the observer from observers' do
|
58
|
+
@observable << @observer
|
59
|
+
|
60
|
+
assert @observable.include?(@observer)
|
61
|
+
|
62
|
+
@observable.delete(@observer)
|
63
|
+
|
64
|
+
refute @observable.include?(@observer)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe '#action' do
|
69
|
+
before do
|
70
|
+
@observer.observe(@observable)
|
71
|
+
|
72
|
+
@observable.action do |context|
|
73
|
+
@context = context
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'passes context to the given block' do
|
78
|
+
assert @context == @observable.instance_variable_get(:@context)
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'passes context to observer callbacks' do
|
82
|
+
assert @observer.received == @observable.instance_variable_get(:@context)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
describe 'Lorikeet::Observer' do
|
88
|
+
before do
|
89
|
+
@observable = Lorikeet::Observable.new(Counter.new)
|
90
|
+
@observer = Observer.new
|
91
|
+
end
|
92
|
+
|
93
|
+
describe '#observe' do
|
94
|
+
it 'adds self to the observable\'s observers' do
|
95
|
+
@observer.observe @observable
|
96
|
+
|
97
|
+
assert @observable.include?(@observer)
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'sets the callback_id if provided' do
|
101
|
+
@observer.observe @observable
|
102
|
+
|
103
|
+
assert @observer.callback_id == Lorikeet::Observer::DEFAULT_CALLBACK_ID
|
104
|
+
|
105
|
+
@observer.observe @observable, :custom_callback_id
|
106
|
+
|
107
|
+
assert @observer.callback_id == :custom_callback_id
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
describe '#callback_id' do
|
112
|
+
it 'returns DEFAULT_CALLBACK_ID when nil' do
|
113
|
+
@observer.instance_variable_set(:@callback_id, nil)
|
114
|
+
|
115
|
+
assert @observer.callback_id == Lorikeet::Observer::DEFAULT_CALLBACK_ID
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
metadata
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lorikeet
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Steve Weiss
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-01-07 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- weissst@mail.gvsu.edu
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- Gemfile
|
21
|
+
- LICENSE.txt
|
22
|
+
- README.md
|
23
|
+
- Rakefile
|
24
|
+
- lib/lorikeet.rb
|
25
|
+
- lib/lorikeet/version.rb
|
26
|
+
- lorikeet.gemspec
|
27
|
+
- spec/lorikeet_spec.rb
|
28
|
+
homepage: https://github.com/sirscriptalot/lorikeet
|
29
|
+
licenses:
|
30
|
+
- MIT
|
31
|
+
metadata: {}
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
requirements: []
|
47
|
+
rubyforge_project:
|
48
|
+
rubygems_version: 2.5.1
|
49
|
+
signing_key:
|
50
|
+
specification_version: 4
|
51
|
+
summary: Lorikeet
|
52
|
+
test_files: []
|