maybe-chain 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in maybe-chain.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 joker1007
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,43 @@
1
+ # Maybe::Chain
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ gem 'maybe-chain'
8
+
9
+ And then execute:
10
+
11
+ $ bundle
12
+
13
+ Or install it yourself as:
14
+
15
+ $ gem install maybe-chain
16
+
17
+ ## Usage
18
+
19
+ ```ruby
20
+ m1 = "a".to_maybe.upcase.gsub(/A/, "B")
21
+
22
+ maybe(m1) do |str|
23
+ puts str # => a
24
+ end
25
+
26
+ m2 = nil.to_maybe.upcase.gsub(/A/, "B")
27
+
28
+ maybe(m2) do |str|
29
+ puts str # No Execute
30
+ end
31
+
32
+ maybe(m2, "a") do |str|
33
+ puts str # => a
34
+ end
35
+ ```
36
+
37
+ ## Contributing
38
+
39
+ 1. Fork it
40
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
41
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
42
+ 4. Push to the branch (`git push origin my-new-feature`)
43
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,77 @@
1
+ require "maybe-chain/version"
2
+ require "delegate"
3
+ require "singleton"
4
+
5
+ module MaybeChain
6
+ class MaybeWrapper < Delegator
7
+ def initialize(obj)
8
+ if obj.nil?
9
+ @obj = Nothing.instance
10
+ else
11
+ @obj = obj
12
+ end
13
+ end
14
+
15
+ def __getobj__
16
+ @obj
17
+ end
18
+
19
+ def method_missing(m, *args, &block)
20
+ if nothing?
21
+ self.__getobj__.__send__(m, *args, &block)
22
+ else
23
+ MaybeWrapper.new(super)
24
+ end
25
+ end
26
+
27
+ def nothing?
28
+ @obj.class == MaybeChain::Nothing
29
+ end
30
+
31
+ def just?
32
+ !nothing?
33
+ end
34
+
35
+ def value
36
+ if nothing?
37
+ nil
38
+ else
39
+ @obj
40
+ end
41
+ end
42
+ end
43
+
44
+ class Nothing
45
+ include Singleton
46
+
47
+ def method_missing(method, *args, &block)
48
+ MaybeWrapper.new(self)
49
+ end
50
+ end
51
+
52
+ module ObjectExtend
53
+ def to_maybe
54
+ MaybeChain::MaybeWrapper.new(self)
55
+ end
56
+ end
57
+
58
+ module KernelExtend
59
+ def maybe(maybe_wrapper, default = nil, &block)
60
+ if maybe_wrapper.just?
61
+ block.call(maybe_wrapper.value)
62
+ elsif default
63
+ block.call(default)
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ Object.__send__ :include, MaybeChain::ObjectExtend
70
+
71
+ module Kernel
72
+ include MaybeChain::KernelExtend
73
+ end
74
+
75
+ class << self
76
+ include MaybeChain::KernelExtend
77
+ end
@@ -0,0 +1,3 @@
1
+ module MaybeChain
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/maybe-chain/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["joker1007"]
6
+ gem.email = ["kakyoin.hierophant@gmail.com"]
7
+ gem.description = %q{In method chain, This gem makes failure more graceful }
8
+ gem.summary = %q{In method chain, This gem makes failure more graceful }
9
+ gem.homepage = "https://github.com/joker1007/maybe-chain"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "maybe-chain"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = MaybeChain::VERSION
17
+
18
+ gem.add_development_dependency('rspec', '~>2.11.0')
19
+ gem.add_development_dependency('rake')
20
+ end
@@ -0,0 +1,55 @@
1
+ $: << File.join(File.dirname(File.expand_path(__FILE__)), "..", "..", "lib")
2
+
3
+ require "maybe-chain"
4
+
5
+ describe MaybeChain do
6
+ describe "Object Extension" do
7
+ subject { "a".to_maybe }
8
+ it { should be_a(MaybeChain::MaybeWrapper) }
9
+ end
10
+
11
+ describe "method delegation" do
12
+ subject { "a".to_maybe.upcase }
13
+ its(:value) { should eq "A" }
14
+
15
+ context "method returns nil" do
16
+ let(:string) { "a".tap {|s| s.extend(MaybeChain::TestMethod)} }
17
+
18
+ subject { string.to_maybe.return_nil.upcase }
19
+ its(:value) { should be_nil }
20
+ end
21
+ end
22
+
23
+ describe "maybe extraction" do
24
+ include Kernel
25
+
26
+ context "maybe is just" do
27
+ let(:maybe_obj) { "a".to_maybe.upcase }
28
+ it "execute block" do
29
+ i = 0
30
+ expect { maybe(maybe_obj) {i += 1} }.to change {i}.by(1)
31
+ end
32
+ end
33
+
34
+ context "maybe is nothing" do
35
+ let(:maybe_obj) { nil.to_maybe.upcase }
36
+ it "no execute block" do
37
+ i = 0
38
+ expect { maybe(maybe_obj) {i += 1} }.not_to change {i}
39
+ end
40
+
41
+ context "with default value" do
42
+ it "execute block" do
43
+ i = 0
44
+ expect { maybe(maybe_obj, "a") {i += 1} }.to change {i}.by(1)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ module MaybeChain::TestMethod
52
+ def return_nil
53
+ nil
54
+ end
55
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: maybe-chain
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - joker1007
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.11.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.11.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: ! 'In method chain, This gem makes failure more graceful '
47
+ email:
48
+ - kakyoin.hierophant@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE
56
+ - README.md
57
+ - Rakefile
58
+ - lib/maybe-chain.rb
59
+ - lib/maybe-chain/version.rb
60
+ - maybe-chain.gemspec
61
+ - spec/lib/maybe-chain_spec.rb
62
+ homepage: https://github.com/joker1007/maybe-chain
63
+ licenses: []
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 1.8.23
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: In method chain, This gem makes failure more graceful
86
+ test_files:
87
+ - spec/lib/maybe-chain_spec.rb
88
+ has_rdoc: