maybelline 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/README.md +21 -0
- data/Rakefile +1 -0
- data/lib/maybelline.rb +33 -0
- data/lib/maybelline/version.rb +3 -0
- data/maybelline.gemspec +24 -0
- data/spec/maybelline_spec.rb +53 -0
- data/spec/spec_helper.rb +13 -0
- metadata +70 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# Maybelline
|
2
|
+
|
3
|
+
An implementation of the maybe monad.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
``` ruby
|
8
|
+
class B
|
9
|
+
def b
|
10
|
+
nil
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# if any method in the chain of calls returns nil, the
|
15
|
+
# block will return nil
|
16
|
+
Maybe(B){|b| b.new.b.this_method_wont_get_called} #=> nil
|
17
|
+
B.maybe{|b| b.new.b.this_method_wont_get_called} #=> nil
|
18
|
+
|
19
|
+
# if it is successful, it will return the value
|
20
|
+
1.maybe{|n| n.to_s(16).to_i.to_s(8)} #=> 1
|
21
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/maybelline.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require "maybelline/version"
|
2
|
+
|
3
|
+
module Maybelline
|
4
|
+
def maybe
|
5
|
+
Maybe(self, &Proc.new)
|
6
|
+
end
|
7
|
+
|
8
|
+
def Maybe(object)
|
9
|
+
catch :nothing do
|
10
|
+
result = yield Maybe.new(object)
|
11
|
+
|
12
|
+
result.respond_to?(:__object__) ? result.__object__ : result
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class Maybe < BasicObject
|
17
|
+
attr_reader :__object__
|
18
|
+
|
19
|
+
def initialize(object)
|
20
|
+
@__object__ = object
|
21
|
+
end
|
22
|
+
|
23
|
+
def method_missing(method, *args)
|
24
|
+
result = @__object__.send(method, *args)
|
25
|
+
|
26
|
+
::Object.new.send :throw, :nothing if result.nil?
|
27
|
+
|
28
|
+
Maybe.new(result)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
include Maybelline
|
data/maybelline.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "maybelline/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "maybelline"
|
7
|
+
s.version = Maybelline::VERSION
|
8
|
+
s.authors = ["Allen Madsen"]
|
9
|
+
s.email = ["blatyo@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Maybe you should start using the maybe monad, stop checking for nil, and be confident}
|
12
|
+
s.description = %q{Maybe you should start using the maybe monad, stop checking for nil, and be confident}
|
13
|
+
|
14
|
+
s.rubyforge_project = "maybelline"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
s.add_development_dependency "rspec"
|
23
|
+
# s.add_runtime_dependency "rest-client"
|
24
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Maybelline::Maybe do
|
4
|
+
let(:object) do
|
5
|
+
mock(:object,
|
6
|
+
:i_wanna_nil => nil
|
7
|
+
)
|
8
|
+
end
|
9
|
+
subject{Maybelline::Maybe.new(object)}
|
10
|
+
|
11
|
+
it "should delegate methods to the inner object" do
|
12
|
+
object.should_receive(:a_method).and_return(1)
|
13
|
+
|
14
|
+
subject.a_method
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should throw :nothing if the method called on it returns nil" do
|
18
|
+
expect{subject.i_wanna_nil}.to throw_symbol
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should expose the internal object with __object__" do
|
22
|
+
subject.__object__.should == object
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "#Maybe" do
|
27
|
+
let(:object) do
|
28
|
+
mock(:object).tap do |object|
|
29
|
+
object.stub_chain(:bob, :rob, :jack).and_return(nil)
|
30
|
+
object.stub_chain(:rob, :bob, :jack).and_return(1)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should return nil if any method in the chain returns nil and not execute the subsequent methods" do
|
35
|
+
result = Maybe(object){|o| o.bob.rob.jack.not_called}
|
36
|
+
result.should be_nil
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should return the value if the chain of methods is successful" do
|
40
|
+
result = Maybe(object){|o| o.rob.bob.jack}
|
41
|
+
result.should == 1
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "#maybe" do
|
46
|
+
let(:proc){:to_s.to_proc}
|
47
|
+
|
48
|
+
it "should call Maybe with the object maybe is called on" do
|
49
|
+
Maybelline::Maybe.should_receive(:new).with(1, &proc)
|
50
|
+
|
51
|
+
1.maybe(&proc)
|
52
|
+
end
|
53
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require File.expand_path('../../lib/maybelline', __FILE__)
|
2
|
+
|
3
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
4
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
5
|
+
# Require this file using `require "spec_helper.rb"` to ensure that it is only
|
6
|
+
# loaded once.
|
7
|
+
#
|
8
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
9
|
+
RSpec.configure do |config|
|
10
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
11
|
+
config.run_all_when_everything_filtered = true
|
12
|
+
config.filter_run :focus
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: maybelline
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Allen Madsen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70244550945600 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70244550945600
|
25
|
+
description: Maybe you should start using the maybe monad, stop checking for nil,
|
26
|
+
and be confident
|
27
|
+
email:
|
28
|
+
- blatyo@gmail.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- .gitignore
|
34
|
+
- .rspec
|
35
|
+
- Gemfile
|
36
|
+
- README.md
|
37
|
+
- Rakefile
|
38
|
+
- lib/maybelline.rb
|
39
|
+
- lib/maybelline/version.rb
|
40
|
+
- maybelline.gemspec
|
41
|
+
- spec/maybelline_spec.rb
|
42
|
+
- spec/spec_helper.rb
|
43
|
+
homepage: ''
|
44
|
+
licenses: []
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubyforge_project: maybelline
|
63
|
+
rubygems_version: 1.8.17
|
64
|
+
signing_key:
|
65
|
+
specification_version: 3
|
66
|
+
summary: Maybe you should start using the maybe monad, stop checking for nil, and
|
67
|
+
be confident
|
68
|
+
test_files:
|
69
|
+
- spec/maybelline_spec.rb
|
70
|
+
- spec/spec_helper.rb
|