reference.rb 0.1.0
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.
- checksums.yaml +7 -0
- data/.gitignore +35 -0
- data/.travis.yml +6 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +37 -0
- data/LICENSE +22 -0
- data/README.md +4 -0
- data/Rakefile +8 -0
- data/lib/reference.rb +80 -0
- data/reference.gemspec +19 -0
- data/spec/reference_spec.rb +141 -0
- data/spec/spec_helper.rb +22 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 575fb0531323489e5058f79f595cb839126afe92
|
4
|
+
data.tar.gz: 77d6d8827153866de510069deba9c67a60b9c8ed
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 65bc14a7a2faf5d2bee36d6fb355e4b09ea8a5f38398061efec8e455a6fcbb64795b3cfeaad7559ec2a51f43d6b6dc6ab15f83fc1a0cdbf88ef0219a9ec1d99b
|
7
|
+
data.tar.gz: dee886219161facdda8475b6ee2c549f40684269d465ac9519f55897ce649cb1e3bb18002098f79f02bfad46b6e70e739bd49b62750eecb3f90d37a98be02065
|
data/.gitignore
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/test/tmp/
|
9
|
+
/test/version_tmp/
|
10
|
+
/tmp/
|
11
|
+
|
12
|
+
## Specific to RubyMotion:
|
13
|
+
.dat*
|
14
|
+
.repl_history
|
15
|
+
build/
|
16
|
+
|
17
|
+
## Documentation cache and generated files:
|
18
|
+
/.yardoc/
|
19
|
+
/_yardoc/
|
20
|
+
/doc/
|
21
|
+
/rdoc/
|
22
|
+
|
23
|
+
## Environment normalisation:
|
24
|
+
/.bundle/
|
25
|
+
/vendor/bundle
|
26
|
+
/lib/bundler/man/
|
27
|
+
|
28
|
+
# for a library or gem, you might want to ignore these files since the code is
|
29
|
+
# intended to run in multiple environments; otherwise, check them in:
|
30
|
+
# Gemfile.lock
|
31
|
+
# .ruby-version
|
32
|
+
# .ruby-gemset
|
33
|
+
|
34
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
35
|
+
.rvmrc
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
reference.rb (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
awesome_print (1.6.1)
|
10
|
+
diff-lcs (1.2.1)
|
11
|
+
multi_json (1.7.2)
|
12
|
+
rake (10.0.4)
|
13
|
+
rspec (2.13.0)
|
14
|
+
rspec-core (~> 2.13.0)
|
15
|
+
rspec-expectations (~> 2.13.0)
|
16
|
+
rspec-mocks (~> 2.13.0)
|
17
|
+
rspec-core (2.13.1)
|
18
|
+
rspec-expectations (2.13.0)
|
19
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
20
|
+
rspec-mocks (2.13.0)
|
21
|
+
simplecov (0.7.1)
|
22
|
+
multi_json (~> 1.0)
|
23
|
+
simplecov-html (~> 0.7.1)
|
24
|
+
simplecov-html (0.7.1)
|
25
|
+
|
26
|
+
PLATFORMS
|
27
|
+
ruby
|
28
|
+
|
29
|
+
DEPENDENCIES
|
30
|
+
awesome_print
|
31
|
+
rake
|
32
|
+
reference.rb!
|
33
|
+
rspec
|
34
|
+
simplecov
|
35
|
+
|
36
|
+
BUNDLED WITH
|
37
|
+
1.10.5
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Keith Kim
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
data/Rakefile
ADDED
data/lib/reference.rb
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
class Reference
|
4
|
+
Error = Class.new(RuntimeError)
|
5
|
+
|
6
|
+
attr_reader :state, :value, :reason
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@state = :pending
|
10
|
+
@on_fulfill_callbacks = []
|
11
|
+
@on_reject_callbacks = []
|
12
|
+
@backtrace
|
13
|
+
end
|
14
|
+
|
15
|
+
def pending?
|
16
|
+
@state.equal?(:pending)
|
17
|
+
end
|
18
|
+
|
19
|
+
def fulfilled?
|
20
|
+
@state.equal?(:fulfilled)
|
21
|
+
end
|
22
|
+
|
23
|
+
def rejected?
|
24
|
+
@state.equal?(:rejected)
|
25
|
+
end
|
26
|
+
|
27
|
+
def on_fulfill(&callback)
|
28
|
+
@on_fulfill_callbacks << callback
|
29
|
+
if fulfilled?
|
30
|
+
run_fulfill_callback(callback)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def on_reject(&callback)
|
35
|
+
@on_reject_callbacks << callback
|
36
|
+
if rejected?
|
37
|
+
run_reject_callback(callback)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def fulfill(value = nil, backtrace = nil)
|
42
|
+
return unless pending?
|
43
|
+
|
44
|
+
@state = :fulfilled
|
45
|
+
@value = value
|
46
|
+
@backtrace = backtrace
|
47
|
+
|
48
|
+
run_fulfill_callbacks
|
49
|
+
value
|
50
|
+
end
|
51
|
+
|
52
|
+
def reject(reason = nil, backtrace = nil)
|
53
|
+
return unless pending?
|
54
|
+
|
55
|
+
@state = :rejected
|
56
|
+
@reason = reason || Error
|
57
|
+
@backtrace = backtrace
|
58
|
+
|
59
|
+
run_reject_callbacks
|
60
|
+
reason
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def run_fulfill_callbacks
|
66
|
+
@on_fulfill_callbacks.each { |callback| run_fulfill_callback(callback) }
|
67
|
+
end
|
68
|
+
|
69
|
+
def run_reject_callbacks
|
70
|
+
@on_reject_callbacks.each { |callback| run_reject_callback(callback) }
|
71
|
+
end
|
72
|
+
|
73
|
+
def run_fulfill_callback(callback)
|
74
|
+
callback.call(@value)
|
75
|
+
end
|
76
|
+
|
77
|
+
def run_reject_callback(callback)
|
78
|
+
callback.call(@reason)
|
79
|
+
end
|
80
|
+
end
|
data/reference.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Gem::Specification.new do |spec|
|
2
|
+
spec.name = 'reference.rb'
|
3
|
+
spec.version = '0.1.0'
|
4
|
+
spec.date = '2015-09-13'
|
5
|
+
spec.summary = "A value holder. Like pointers in other languages, or a mutable Optional."
|
6
|
+
spec.description = "A value holder. Like pointers in other languages, or a mutable Optional."
|
7
|
+
spec.authors = ["Keith Kim"]
|
8
|
+
spec.email = ['keith.karmakaze@gmail.com']
|
9
|
+
spec.files = `git ls-files`.split($\)
|
10
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
11
|
+
spec.require_paths = ["lib"]
|
12
|
+
spec.homepage = 'http://github.com/karmakaze/reference.rb'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.add_development_dependency "rake"
|
16
|
+
spec.add_development_dependency "rspec"
|
17
|
+
spec.add_development_dependency "simplecov"
|
18
|
+
spec.add_development_dependency "awesome_print"
|
19
|
+
end
|
@@ -0,0 +1,141 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Reference do
|
6
|
+
subject { Reference.new }
|
7
|
+
|
8
|
+
let(:value) { double('value') }
|
9
|
+
let(:other_value) { double('other_value') }
|
10
|
+
|
11
|
+
let(:backtrace) { caller }
|
12
|
+
let(:reason) do
|
13
|
+
StandardError.new('reason').tap { |ex| ex.set_backtrace(backtrace) }
|
14
|
+
end
|
15
|
+
let(:other_reason) do
|
16
|
+
StandardError.new('other_reason').tap { |ex| ex.set_backtrace(backtrace) }
|
17
|
+
end
|
18
|
+
|
19
|
+
describe '3.1.1 pending' do
|
20
|
+
it 'transitions to fulfilled' do
|
21
|
+
subject.fulfill(value)
|
22
|
+
expect(subject).to be_fulfilled
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'transitions to rejected' do
|
26
|
+
subject.reject(reason)
|
27
|
+
expect(subject).to be_rejected
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe '3.1.2 fulfilled' do
|
32
|
+
it 'does not transition to other states' do
|
33
|
+
subject.fulfill(value)
|
34
|
+
subject.reject(reason)
|
35
|
+
expect(subject).to be_fulfilled
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'has a value' do
|
39
|
+
subject.fulfill(value)
|
40
|
+
expect(subject.value).to eq(value)
|
41
|
+
|
42
|
+
subject.fulfill(other_value)
|
43
|
+
expect(subject.value).to eq(value)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe '3.1.3 rejected' do
|
48
|
+
it 'does not transition to other states' do
|
49
|
+
subject.reject(reason)
|
50
|
+
subject.fulfill(value)
|
51
|
+
expect(subject).to be_rejected
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'has a reason' do
|
55
|
+
subject.reject(reason)
|
56
|
+
expect(subject.reason).to eq(reason)
|
57
|
+
|
58
|
+
subject.reject(other_reason)
|
59
|
+
expect(subject.reason).to eq(reason)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe '3.2.5' do
|
64
|
+
it 'calls multiple on_fulfill callbacks in order of definition' do
|
65
|
+
order = []
|
66
|
+
|
67
|
+
subject.on_fulfill do |val|
|
68
|
+
order << 1
|
69
|
+
expect(val).to eq(value)
|
70
|
+
end
|
71
|
+
|
72
|
+
subject.on_fulfill do |val|
|
73
|
+
order << 2
|
74
|
+
expect(val).to eq(value)
|
75
|
+
end
|
76
|
+
|
77
|
+
subject.fulfill(value)
|
78
|
+
|
79
|
+
subject.on_fulfill do |val|
|
80
|
+
order << 3
|
81
|
+
expect(val).to eq(value)
|
82
|
+
end
|
83
|
+
|
84
|
+
expect(order).to eq([1, 2, 3])
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'calls multiple on_reject callbacks in order of definition' do
|
88
|
+
order = []
|
89
|
+
on_reject = lambda do |i|
|
90
|
+
order << i
|
91
|
+
|
92
|
+
return lambda do |reas|
|
93
|
+
expect(reas).to eq(reason)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
subject.on_reject do |reas|
|
98
|
+
order << 1
|
99
|
+
expect(reas).to eq(reason)
|
100
|
+
end
|
101
|
+
|
102
|
+
subject.on_reject do |reas|
|
103
|
+
order << 2
|
104
|
+
expect(reas).to eq(reason)
|
105
|
+
end
|
106
|
+
|
107
|
+
subject.reject(reason)
|
108
|
+
|
109
|
+
subject.on_reject do |reas|
|
110
|
+
order << 3
|
111
|
+
expect(reas).to eq(reason)
|
112
|
+
end
|
113
|
+
|
114
|
+
expect(order).to eq([1, 2, 3])
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
describe 'extras' do
|
119
|
+
describe '#fulfill' do
|
120
|
+
it 'does not return anything' do
|
121
|
+
expect(subject.fulfill(nil)).to eq(nil)
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'does not require a value' do
|
125
|
+
subject.fulfill
|
126
|
+
expect(subject.value).to be(nil)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
describe '#reject' do
|
131
|
+
it 'does not return anything' do
|
132
|
+
expect(subject.reject(nil)).to eq(nil)
|
133
|
+
end
|
134
|
+
|
135
|
+
it 'does not require a reason' do
|
136
|
+
subject.reject
|
137
|
+
expect(subject.reason).to be(Reference::Error)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
if ENV['COVERAGE'] == 'true'
|
4
|
+
require 'simplecov'
|
5
|
+
require 'coveralls'
|
6
|
+
|
7
|
+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
8
|
+
SimpleCov::Formatter::HTMLFormatter,
|
9
|
+
Coveralls::SimpleCov::Formatter
|
10
|
+
]
|
11
|
+
|
12
|
+
SimpleCov.start do
|
13
|
+
command_name 'spec:unit'
|
14
|
+
|
15
|
+
add_filter 'config'
|
16
|
+
add_filter 'spec'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'reference'
|
21
|
+
|
22
|
+
require 'awesome_print'
|
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: reference.rb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Keith Kim
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-09-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: simplecov
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: awesome_print
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: A value holder. Like pointers in other languages, or a mutable Optional.
|
70
|
+
email:
|
71
|
+
- keith.karmakaze@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- Gemfile
|
79
|
+
- Gemfile.lock
|
80
|
+
- LICENSE
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- lib/reference.rb
|
84
|
+
- reference.gemspec
|
85
|
+
- spec/reference_spec.rb
|
86
|
+
- spec/spec_helper.rb
|
87
|
+
homepage: http://github.com/karmakaze/reference.rb
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
metadata: {}
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 2.4.6
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: A value holder. Like pointers in other languages, or a mutable Optional.
|
111
|
+
test_files:
|
112
|
+
- spec/reference_spec.rb
|
113
|
+
- spec/spec_helper.rb
|
114
|
+
has_rdoc:
|