probably 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6b8b28306080e9a070f9e0ac0376ae725c25e8e5
4
+ data.tar.gz: 4c7386aa29a5e8cae34724156a335aad25a32d4e
5
+ SHA512:
6
+ metadata.gz: e4511ea755487976ec503b81e6ce2dc997b9aa8e55ff4a99dbaf05ab418cd7e1d3e1e51c213fa5f2f34c62663f4fef7c313cb18423ec3b76acc85c78045de5f4
7
+ data.tar.gz: 80229fc66dc551135bba8ffa214f3d26d66b4dec24bbbe055fbbeeb2ad021725b4a2c8d7fb424d522d4362eb3a00a98086da667fdc6e45df3733f7de607361d4
@@ -0,0 +1,25 @@
1
+ Copyright (c)2014, Yi Wei
2
+
3
+ All rights reserved.
4
+
5
+ Permission is hereby granted, free of charge, to any person
6
+ obtaining a copy of this software and associated documentation
7
+ files (the "Software"), to deal in the Software without
8
+ restriction, including without limitation the rights to use,
9
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the
11
+ Software is furnished to do so, subject to the following
12
+ conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24
+ OTHER DEALINGS IN THE SOFTWARE.
25
+
@@ -0,0 +1,36 @@
1
+ Probably
2
+ =============
3
+
4
+ Probably is an extremely lightweight and simple tool. It only exists for one reason: to avoid checking for nil or exception in a calls chain. So, instead of writing like this:
5
+ ```ruby
6
+ visitors.most_recent.try(:last).first_name || "NoSuchAnUser"
7
+ ```
8
+ you write like this
9
+ ```ruby
10
+ visitors.probably.most_recent.last.first_name.or("NoSuchAnUser")
11
+ ```
12
+ or in a more natural way:
13
+ ```ruby
14
+ probably(visitors).most_recent.last.first_name.or("NoSuchAnUser")
15
+ ```
16
+
17
+
18
+ Usage
19
+ _____
20
+
21
+ The simplest scenario
22
+
23
+ ```ruby
24
+ class Statistics
25
+ include Probably
26
+ attr_reader :visitors
27
+
28
+ def latest_visitor
29
+ probably(visitors).most_recent.last.first_name.or("NoSuchAnUser")
30
+ end
31
+ end
32
+
33
+ end
34
+ ```
35
+ you many want to check probably_spec.rb for more examples, they are quite simple and intutive!
36
+
@@ -0,0 +1,9 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require 'probably/delayed_invocation'
4
+
5
+ module Probably
6
+ def probably(target = nil)
7
+ DelayedInvocation.new(target || self)
8
+ end
9
+ end
@@ -0,0 +1,29 @@
1
+ class DelayedInvocation
2
+ def initialize(target)
3
+ @target = target
4
+ @methods = []
5
+ @args = []
6
+ @blocks = []
7
+ end
8
+
9
+ def method_missing(name, *args, &block)
10
+ @methods << name
11
+ @args << args
12
+ @blocks << (block.nil? ? nil : block)
13
+ self
14
+ end
15
+
16
+ def or(value)
17
+ @methods.reduce(@target) do |t, m|
18
+ block = @blocks.shift
19
+ unless block.nil?
20
+ t.__send__(m, *(@args.shift), &block)
21
+ else
22
+ t.__send__(m, *(@args.shift))
23
+ end
24
+ end
25
+ rescue
26
+ value
27
+ end
28
+ end
29
+
@@ -0,0 +1,9 @@
1
+ module Probably#:nodoc:
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ TINY = 0
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
@@ -0,0 +1,66 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper.rb')
2
+
3
+ require 'probably'
4
+
5
+ describe Probably, "Basic Behaviour" do
6
+ context "basic syntax" do
7
+ before do
8
+ class Object
9
+ include Probably
10
+ end
11
+ end
12
+
13
+ it "triggers methods when the source is not nil" do
14
+ expect(5.probably.succ.or(0)).to eq 6
15
+ expect(0.probably.succ.or(7)).to eq 1
16
+ expect(5.probably.succ.succ.or(1)).to eq 7
17
+
18
+ expect("test string".probably.split.count.or(1)).to eq 2
19
+ expect("internationalization".probably.split('n').count.or(1)).to eq 4
20
+
21
+ expect(15.probably.succ.or(10).pred).to eq 15
22
+ expect([1, 2, 3].probably.reduce(0) {|sum, i| sum = sum + i }.or(17)).to eq 6
23
+ end
24
+
25
+ it "returns 'or' value when nil occurs in the calls chain" do
26
+ expect(nil.probably.succ.succ.or(5)).to eq 5
27
+ expect(nil.probably.split.count.or(0)).to eq 0
28
+ end
29
+
30
+ it "return 'or' value when exception occurs" do
31
+ expect(15.probably.split.or(12)).to eq 12
32
+ expect(false.probably.succ.or(true)).to eq true
33
+ end
34
+ end
35
+
36
+ context "more natural syntax" do
37
+ before do
38
+ class Object
39
+ include Probably
40
+ end
41
+ end
42
+
43
+ it "triggers methods when the source is not nil" do
44
+ expect(probably(5).succ.or(0)).to eq 6
45
+ expect(probably(0).succ.or(7)).to eq 1
46
+ expect(probably(5).succ.succ.or(1)).to eq 7
47
+
48
+ expect(probably("test string").split.count.or(1)).to eq 2
49
+ expect(probably("internationalization").split('n').count.or(1)).to eq 4
50
+
51
+ expect(probably(15).succ.or(10).pred).to eq 15
52
+ expect(probably([1, 2, 3]).reduce(0) {|sum, i| sum = sum + i }.or(17)).to eq 6
53
+ end
54
+
55
+ it "returns 'or' value when nil occurs in the calls chain" do
56
+ expect(probably(nil).succ.succ.or(5)).to eq 5
57
+ expect(probably(nil).split.count.or(0)).to eq 0
58
+ end
59
+
60
+ it "return 'or' value when exception occurs" do
61
+ expect(probably(15).split.or(12)).to eq 12
62
+ expect(probably(false).succ.or(true)).to eq true
63
+ end
64
+
65
+ end
66
+ end
@@ -0,0 +1,2 @@
1
+ require 'rspec'
2
+ require File.dirname(__FILE__) + '/../lib/probably'
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: probably
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yi Wei
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: ''
14
+ email: yiwei.in.cyber@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files:
18
+ - README.md
19
+ files:
20
+ - License.txt
21
+ - README.md
22
+ - lib/probably.rb
23
+ - lib/probably/delayed_invocation.rb
24
+ - lib/probably/version.rb
25
+ - spec/probably_spec.rb
26
+ - spec/spec_helper.rb
27
+ homepage: http://github.com/zeninpalm/probably/
28
+ licenses: []
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options:
32
+ - "--main"
33
+ - README.md
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 2.2.2
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: Enables you to chain calls without explicitly checking nils or exceptions
52
+ test_files:
53
+ - spec/probably_spec.rb
54
+ - spec/spec_helper.rb