or_else 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 00e2d91a9c0a68e99f45f8212e13bc1777943197
4
+ data.tar.gz: 64e2b6a86d14c64487c3896d73a22d4d982434ae
5
+ SHA512:
6
+ metadata.gz: 03817323e1e8bacd91c40710e250c4deca3f4c6a06e6a5717778dfadfa2cb0c5cd2dd2231415354b655b48140fd69a085a596146934a0d2f6a223bba32012c37
7
+ data.tar.gz: bb18dd5707ad2a0669b7946cb22d85d4817c8d5f114720aed76e4d10e9291c2001f7fba90b640015a01e168eb5fbf181baa5f6c46723d11350681e6876a7b786
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in or_else.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 alexpeachey
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # OrElse
2
+
3
+ A simple implementation of the Maybe (Option) monad.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'or_else'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install or_else
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ value = Maybe(nil) # => <OrElse::NothingClass>
23
+ value.empty? # => true
24
+ value.nil? # => true
25
+ value.exists? # => false
26
+ value.map { |v| v } # => <OrElse::NothingClass>
27
+ value.flat_map { |v| v } # => <OrElse::NothingClass>
28
+ value.or_else { 'foo' } # => 'foo'
29
+
30
+ value = Maybe('value') # => <OrElse::Just>
31
+ value.empty? # => false
32
+ value.nil? # => false
33
+ value.exists? # => true
34
+ value.map { |v| v } # => 'value'
35
+ value.flat_map { |v| v } # => 'value'
36
+ value.or_else { 'foo' } # => 'value'
37
+ ```
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it ( http://github.com/Originate/or_else/fork )
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
7
+
8
+ task :console do
9
+ require 'irb'
10
+ require 'irb/completion'
11
+ require 'or_else'
12
+ ARGV.clear
13
+ IRB.start
14
+ end
@@ -0,0 +1,34 @@
1
+ module OrElse
2
+ class Just < Maybe
3
+
4
+ attr_reader :value
5
+
6
+ def initialize(val)
7
+ @value = val
8
+ end
9
+
10
+ def map
11
+ yield Just.new(value)
12
+ end
13
+
14
+ def flat_map
15
+ yield value
16
+ end
17
+
18
+ def empty?
19
+ false
20
+ end
21
+
22
+ def exists?
23
+ !empty?
24
+ end
25
+
26
+ def or_else
27
+ self
28
+ end
29
+ end
30
+ end
31
+
32
+ def Just(val)
33
+ OrElse::Just.new(val)
34
+ end
@@ -0,0 +1,13 @@
1
+ require 'or_else/just'
2
+ require 'or_else/nothing_class'
3
+
4
+ def Maybe(val)
5
+ return Nothing if val.nil?
6
+ OrElse::Just.new(val)
7
+ end
8
+
9
+ class Object
10
+ def maybe
11
+ Maybe(self)
12
+ end
13
+ end
@@ -0,0 +1,33 @@
1
+ require 'singleton'
2
+
3
+ module OrElse
4
+ class NothingClass < Maybe
5
+ include Singleton
6
+
7
+ def map
8
+ Nothing
9
+ end
10
+
11
+ def flat_map
12
+ Nothing
13
+ end
14
+
15
+ def empty?
16
+ true
17
+ end
18
+
19
+ def nil?
20
+ empty?
21
+ end
22
+
23
+ def exists?
24
+ !empty?
25
+ end
26
+
27
+ def or_else
28
+ yield
29
+ end
30
+ end
31
+ end
32
+
33
+ Nothing = OrElse::NothingClass.instance
@@ -0,0 +1,3 @@
1
+ module OrElse
2
+ VERSION = "0.0.1"
3
+ end
data/lib/or_else.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'or_else/version'
2
+
3
+ module OrElse
4
+ class Maybe
5
+ end
6
+ end
7
+
8
+ require 'or_else/maybe'
data/or_else.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'or_else/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'or_else'
8
+ spec.version = OrElse::VERSION
9
+ spec.authors = ['alexpeachey']
10
+ spec.email = ['alex.peachey@originate.com']
11
+ spec.summary = %q{A simple Maybe/Option implementation.}
12
+ spec.description = %q{A simple Maybe/Option implementation.}
13
+ spec.homepage = 'https://github.com/Originate/or_else'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.5'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'rspec'
24
+ end
@@ -0,0 +1,80 @@
1
+ require 'spec_helper'
2
+
3
+ module OrElse
4
+ describe Just do
5
+ subject(:just) { Just.new(val) }
6
+
7
+ context 'when wrapping 1' do
8
+ specify { expect(Just(1)).to be_a Just }
9
+ end
10
+
11
+ describe '#value' do
12
+ context 'when initialized with 1' do
13
+ let(:val) { 1 }
14
+
15
+ specify { expect(just.value).to eq 1 }
16
+ end
17
+
18
+ context 'when initialized with "foo"' do
19
+ let(:val) { 'foo' }
20
+
21
+ specify { expect(just.value).to eq 'foo' }
22
+ end
23
+ end
24
+
25
+ describe '#map' do
26
+ context 'when initialized with 1' do
27
+ let(:val) { 1 }
28
+
29
+ specify { expect { |b| just.map(&b) }.to yield_with_args(Just) }
30
+
31
+ it 'yields a Just with the value 1' do
32
+ just.map { |j| expect(j.value).to eq 1 }
33
+ end
34
+ end
35
+
36
+ context 'when initialized with "foo"' do
37
+ let(:val) { 'foo' }
38
+
39
+ specify { expect { |b| just.map(&b) }.to yield_with_args(Just) }
40
+
41
+ it 'yields a Just with the value "foo"' do
42
+ just.map { |j| expect(j.value).to eq 'foo' }
43
+ end
44
+ end
45
+ end
46
+
47
+ describe '#flat_map' do
48
+ context 'when initialized with 1' do
49
+ let(:val) { 1 }
50
+
51
+ specify { expect { |b| just.flat_map(&b) }.to yield_with_args(1) }
52
+ end
53
+
54
+ context 'when initialized with "foo"' do
55
+ let(:val) { 'foo' }
56
+
57
+ specify { expect { |b| just.flat_map(&b) }.to yield_with_args('foo') }
58
+ end
59
+ end
60
+
61
+ describe '#empty?' do
62
+ let(:val) { 1 }
63
+
64
+ specify { expect(just.empty?).to be_false }
65
+ end
66
+
67
+ describe '#exists?' do
68
+ let(:val) { 1 }
69
+
70
+ specify { expect(just.exists?).to be_true }
71
+ end
72
+
73
+ describe '#or_else' do
74
+ let(:val) { 1 }
75
+
76
+ specify { expect(just.or_else).to eq just }
77
+ end
78
+
79
+ end
80
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ module OrElse
4
+ describe Maybe do
5
+ context 'when wrapping nil' do
6
+ specify { expect(Maybe(nil)).to eq Nothing }
7
+ end
8
+
9
+ context 'when wrapping 1' do
10
+ specify { expect(Maybe(1)).to be_a(Just) }
11
+ end
12
+
13
+ context 'when objects wrap themselves' do
14
+ specify { expect(nil.maybe).to eq Nothing }
15
+ specify { expect(1.maybe).to be_a(Just) }
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ module OrElse
4
+ describe Nothing do
5
+ subject(:nothing) { Nothing }
6
+
7
+ describe '#value' do
8
+ specify { expect { nothing.value }.to raise_error }
9
+ end
10
+
11
+ describe '#map' do
12
+ specify { expect(nothing.map).to eq Nothing }
13
+ end
14
+
15
+ describe '#flat_map' do
16
+ specify { expect(nothing.flat_map).to eq Nothing }
17
+ end
18
+
19
+ describe '#empty?' do
20
+ specify { expect(nothing.empty?).to be_true }
21
+ end
22
+
23
+ describe '#nil?' do
24
+ specify { expect(nothing.nil?).to be_true }
25
+ end
26
+
27
+ describe '#exists?' do
28
+ specify { expect(nothing.exists?).to be_false }
29
+ end
30
+
31
+ describe '#or_else' do
32
+ specify { expect { |b| nothing.or_else(&b) }.to yield_control }
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'or_else'
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: or_else
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - alexpeachey
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
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: rspec
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
+ description: A simple Maybe/Option implementation.
56
+ email:
57
+ - alex.peachey@originate.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/or_else.rb
70
+ - lib/or_else/just.rb
71
+ - lib/or_else/maybe.rb
72
+ - lib/or_else/nothing_class.rb
73
+ - lib/or_else/version.rb
74
+ - or_else.gemspec
75
+ - spec/or_else/just_spec.rb
76
+ - spec/or_else/maybe_spec.rb
77
+ - spec/or_else/nothing_spec.rb
78
+ - spec/spec_helper.rb
79
+ homepage: https://github.com/Originate/or_else
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.2.2
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: A simple Maybe/Option implementation.
103
+ test_files:
104
+ - spec/or_else/just_spec.rb
105
+ - spec/or_else/maybe_spec.rb
106
+ - spec/or_else/nothing_spec.rb
107
+ - spec/spec_helper.rb