maybe2 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: a084b6f8abf8b1fae7dba9732dad6f39c6256ce6
4
+ data.tar.gz: 8a06ad0660a4219be68730dbfa2a1c2410d55ad6
5
+ SHA512:
6
+ metadata.gz: 914023b2e80af3952117ba0ad52d6a5672d247cdde79cb8910d9c33a21d49f309bc99fc5ff0523d75977e6107cb408cfd87d0478893f392a4757bec9b2d9bb98
7
+ data.tar.gz: b98f835236d21cd23df3cb1277cb546bf3b8caa33092df95594f0cd4ea3f796def802f0b2afa5579fa721569266e325dc0635398dc5ad69f64c2cd6b95153889
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ *.gem
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --require spec_helper.rb
data/.travis.yml ADDED
@@ -0,0 +1,21 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.2.2
5
+ - 2.1.6
6
+ - 2.0.0
7
+ - 1.9.3
8
+ - jruby-19mode
9
+
10
+ branches:
11
+ only:
12
+ - master
13
+
14
+ notifications:
15
+ email:
16
+ on_success: change
17
+ on_failure: always
18
+
19
+ before_install: "gem install bundler -v 1.10.5"
20
+ install: "bundle --jobs 4"
21
+ script: "bundle exec rspec"
data/.yardopts ADDED
@@ -0,0 +1,7 @@
1
+ --protected
2
+ --private
3
+ --embed-mixins
4
+ lib/**/*.rb
5
+ -
6
+ README.md
7
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in maybe2.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Oldrich Vetesnik
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,76 @@
1
+ # Maybe2 [![Build Status](https://img.shields.io/travis/ollie/maybe2/master.svg)](https://travis-ci.org/ollie/maybe2) [![Code Climate](https://img.shields.io/codeclimate/github/ollie/maybe2.svg)](https://codeclimate.com/github/ollie/maybe2)
2
+
3
+ Wrap a value into a Maybe monad (or Optional type). Since monads are chainable
4
+ and every value is wrapped in one, things don't blow up with `NoMethodError`
5
+ on `nil`s.
6
+
7
+ Useful for retrieving something in deeply nested structures where `nil`s
8
+ could be produced along the way.
9
+
10
+ ## Usage
11
+
12
+ ```ruby
13
+ maybe = Maybe.new('Hello world').upcase.reverse
14
+ maybe.class # => Maybe
15
+ maybe.unwrap # => 'Hello world'
16
+
17
+ maybe = Maybe.new(nil).upcase.reverse
18
+ maybe.class # => Maybe
19
+ maybe.unwrap # => nil
20
+
21
+ maybe = Maybe.new('Hello world').make_nil.upcase.reverse
22
+ maybe.class # => Maybe
23
+ maybe.unwrap # => nil
24
+ ```
25
+
26
+ JSON example evaluation to a string:
27
+
28
+ ```ruby
29
+ response = {
30
+ data: {
31
+ item: {
32
+ name: 'Hello!'
33
+ }
34
+ }
35
+ }
36
+
37
+ Maybe.new(good_response)[:data][:item][:name].upcase.unwrap # => 'HELLO!'
38
+ ```
39
+
40
+ JSON example evaluating to nil:
41
+
42
+ ```ruby
43
+ response = {
44
+ data: {}
45
+ }
46
+
47
+ Maybe.new(good_response)[:data][:item][:name].upcase.unwrap # => nil
48
+ ```
49
+
50
+ ## Installation
51
+
52
+ Add this line to your application's Gemfile:
53
+
54
+ ```ruby
55
+ gem 'maybe2'
56
+ ```
57
+
58
+ And then execute:
59
+
60
+ $ bundle
61
+
62
+ Or install it yourself as:
63
+
64
+ $ gem install maybe2
65
+
66
+ ## Development
67
+
68
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
69
+
70
+ ## Contributing
71
+
72
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ollie/maybe2.
73
+
74
+ ## License
75
+
76
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ task default: :combo
4
+
5
+ desc 'Run tests, rubocop and generate documentation'
6
+ task :combo do
7
+ sh 'bundle exec rspec'
8
+ sh('bundle exec rubocop') {} # ignore status > 0
9
+ sh 'bundle exec yardoc'
10
+ end
11
+
12
+ desc 'Same as :combo but build a gem, too'
13
+ task mega_combo: :combo do
14
+ sh 'gem build maybe2.gemspec'
15
+ end
data/bin/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'maybe2'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ require 'pry'
10
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/lib/maybe.rb ADDED
@@ -0,0 +1,113 @@
1
+ require 'maybe/version'
2
+
3
+ # Wrap a value into a Maybe monad (or Optional type). Since monads are chainable
4
+ # and every value is wrapped in one, things don't blow up with +NoMethodError+
5
+ # on +nil+s.
6
+ #
7
+ # Useful for retrieving something in deeply nested structures where +nil+s
8
+ # could be produced along the way.
9
+ #
10
+ # @example Simple usage.
11
+ # maybe = Maybe.new('Hello world').upcase.reverse
12
+ # maybe.class # => Maybe
13
+ # maybe.unwrap # => 'Hello world'
14
+ #
15
+ # maybe = Maybe.new(nil).upcase.reverse
16
+ # maybe.class # => Maybe
17
+ # maybe.unwrap # => nil
18
+ #
19
+ # maybe = Maybe.new('Hello world').make_nil.upcase.reverse
20
+ # maybe.class # => Maybe
21
+ # maybe.unwrap # => nil
22
+ #
23
+ # @example JSON example evaluation to a string.
24
+ # response = {
25
+ # data: {
26
+ # item: {
27
+ # name: 'Hello!'
28
+ # }
29
+ # }
30
+ # }
31
+ #
32
+ # Maybe.new(good_response)[:data][:item][:name].upcase.unwrap # => 'HELLO!'
33
+ #
34
+ # @example JSON example evaluating to nil.
35
+ # response = {
36
+ # data: {}
37
+ # }
38
+ #
39
+ # Maybe.new(good_response)[:data][:item][:name].upcase.unwrap # => nil
40
+ class Maybe
41
+ # Wrap a value into a new +Maybe+.
42
+ #
43
+ # @param value [Object] Anything.
44
+ def initialize(value)
45
+ @value = value
46
+ end
47
+
48
+ # Return the wrapped value.
49
+ #
50
+ # @example
51
+ # Maybe.new('Hello').unwrap # => 'Hello'
52
+ # Maybe.new('Hello').upcase.unwrap # => 'HELLO'
53
+ # Maybe.new(nil).upcase.unwrap # => nil
54
+ #
55
+ # @return [Object]
56
+ def unwrap
57
+ @value
58
+ end
59
+
60
+ # Chain another action and return a new value wrapped in a new +Maybe+ or
61
+ # the same +Maybe+ if the old value is a +nil+. Calls +chain+ under the hood.
62
+ #
63
+ # @see #chain
64
+ #
65
+ # @example
66
+ # Maybe.new('Hello world').upcase.reverse
67
+ #
68
+ # @return [Maybe]
69
+ def method_missing(*args, &block)
70
+ chain do |value|
71
+ value.public_send(*args, &block)
72
+ end
73
+ end
74
+
75
+ # Also support +respond_to?+.
76
+ #
77
+ # @example
78
+ # Maybe.new('Hello world').upcase.reverse.respond_to?(:size) # => true
79
+ # Maybe.new(nil).upcase.reverse.respond_to?(:size) # => false
80
+ #
81
+ # @return [Bool]
82
+ def respond_to_missing?(method_name, include_private = false)
83
+ super || @value.respond_to?(method_name, include_private)
84
+ end
85
+
86
+ # Chain another action and return a new value wrapped in a new +Maybe+ or
87
+ # the same +Maybe+ if the old value is a +nil+.
88
+ #
89
+ # @example
90
+ # value = Maybe.new('Hello world')
91
+ # .chain(&:upcase)
92
+ # .chain(&:reverse)
93
+ # .unwrap # => 'DLROW OLLEH'
94
+ #
95
+ # value = Maybe.new('Hello world')
96
+ # .chain { |_string| nil }
97
+ # .chain(&:reverse)
98
+ # .unwrap # => nil
99
+ #
100
+ # @yieldparam value [Object] Old value goes in the block.
101
+ # @yieldreturn [Object] New value comes out of the block.
102
+ #
103
+ # @return [Maybe]
104
+ def chain
105
+ # Don't call the block if we are wrapping a nil (hint: that would cause
106
+ # a +NoMethodError+).
107
+ return self if @value.nil?
108
+
109
+ # Call the passed block to get the new value and wrap it again.
110
+ new_value = yield @value
111
+ self.class.new(new_value)
112
+ end
113
+ end
@@ -0,0 +1,4 @@
1
+ class Maybe
2
+ # Version number, happy now?
3
+ VERSION = '0.0.1'
4
+ end
data/lib/maybe2.rb ADDED
@@ -0,0 +1,2 @@
1
+ # I don't want the constants to be named Maybe2 but rather Maybe.
2
+ require 'maybe'
data/maybe2.gemspec ADDED
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'maybe/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'maybe2'
8
+ spec.version = Maybe::VERSION
9
+ spec.authors = ['Oldrich Vetesnik']
10
+ spec.email = ['oldrich.vetesnik@gmail.com']
11
+
12
+ spec.summary = 'Yet another Maybe monad implementation.'
13
+ spec.description = 'Be able to chain method calls without raising ' \
14
+ 'a NoMethodError if there is a nil somewhere along ' \
15
+ 'the way.'
16
+ spec.homepage = 'https://github.com/ollie/maybe2'
17
+ spec.license = 'MIT'
18
+
19
+ # rubocop:disable Metrics/LineLength
20
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ # System
26
+ spec.add_development_dependency 'bundler', '~> 1.10'
27
+
28
+ # Test
29
+ spec.add_development_dependency 'rspec', '~> 3.3'
30
+ spec.add_development_dependency 'simplecov', '~> 0.10'
31
+
32
+ # Code style, debugging, docs
33
+ spec.add_development_dependency 'rubocop', '~> 0.32'
34
+ spec.add_development_dependency 'pry', '~> 0.10'
35
+ spec.add_development_dependency 'yard', '~> 0.8'
36
+ spec.add_development_dependency 'rake', '~> 10.4'
37
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: maybe2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Oldrich Vetesnik
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-09 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.3'
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.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.32'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.32'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.10'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.10'
83
+ - !ruby/object:Gem::Dependency
84
+ name: yard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.8'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.8'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '10.4'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '10.4'
111
+ description: Be able to chain method calls without raising a NoMethodError if there
112
+ is a nil somewhere along the way.
113
+ email:
114
+ - oldrich.vetesnik@gmail.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".travis.yml"
122
+ - ".yardopts"
123
+ - Gemfile
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/setup
129
+ - lib/maybe.rb
130
+ - lib/maybe/version.rb
131
+ - lib/maybe2.rb
132
+ - maybe2.gemspec
133
+ homepage: https://github.com/ollie/maybe2
134
+ licenses:
135
+ - MIT
136
+ metadata: {}
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubyforge_project:
153
+ rubygems_version: 2.4.8
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: Yet another Maybe monad implementation.
157
+ test_files: []
158
+ has_rdoc: