no_conditionals 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +24 -46
- data/lib/no_conditionals.rb +1 -1
- data/lib/no_conditionals/version.rb +1 -1
- data/no_conditionals.gemspec +1 -1
- data/test/test_no_conditionals.rb +1 -1
- data/test/test_no_conditionals_falseyness.rb +2 -2
- metadata +8 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 76146fdeef609f14800e4a1c4e1d1a19cb3e1931
|
4
|
+
data.tar.gz: e28ada290b6f79294f2f57fe5529b763559ce54a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fa9214ba744750522a51f643bfeb963cf8ab50e32322f286d245a85e6b40ecd583647b394482fe7a0977f9dc3053098ae7b01a7391e44db0af6914e270e85749
|
7
|
+
data.tar.gz: 53e7e7e6124981a409d433f0b04f39c4d00bae8ed0e66eef1b21498cf19dff554650fedf0c3d8d2e697d90348dbfc2450260234a492b73a8ecad2a391f6d0cb0
|
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# NoConditionals
|
2
2
|
|
3
|
-
|
3
|
+
Conditional statements, like if/else, have it's place, under libraries and frameworks, but not in application code. This library implements a collection of techniques to avoid if/else, initially by extending "Truthy" and "Falsey" classes with expressive yet Ruby idiomatic operations. Please look forward for more features to come.
|
4
4
|
|
5
5
|
## Getting started
|
6
6
|
|
@@ -47,55 +47,41 @@ Evaluates block if "Truthy" otherwise returns "Falsey"
|
|
47
47
|
```ruby
|
48
48
|
using NoConditionals
|
49
49
|
|
50
|
-
true.hence { "Yes" }
|
51
|
-
# => "Yes"
|
50
|
+
true.hence { "Yes" } # => "Yes"
|
52
51
|
|
53
|
-
false.hence { "Yes" }
|
54
|
-
# => false
|
52
|
+
false.hence { "Yes" } # => false
|
55
53
|
|
56
|
-
nil.hence { "Yes" }
|
57
|
-
# => nil
|
54
|
+
nil.hence { "Yes" } # => nil
|
58
55
|
|
59
|
-
[1,2,3,4,5].find {|n| n.odd? }.hence {|odd| "found #{odd}" }
|
60
|
-
# => "found 1"
|
56
|
+
[1,2,3,4,5].find {|n| n.odd? }.hence {|odd| "found #{odd}" } # => "found 1"
|
61
57
|
|
62
|
-
|
63
|
-
names.first.hence(&:empty?)
|
64
|
-
# => true
|
58
|
+
["", "Ruby", "Smalltalk", nil ].first.hence(&:empty?) # => true
|
65
59
|
```
|
66
60
|
### #otherwise method
|
67
61
|
Returns self if "Truthy" otherwise evaluates block.
|
68
62
|
```ruby
|
69
63
|
using NoConditionals
|
70
64
|
|
71
|
-
true.otherwise { "No" }
|
72
|
-
# => true
|
65
|
+
true.otherwise { "No" } # => true
|
73
66
|
|
74
|
-
false.otherwise { "No" }
|
75
|
-
# => "No"
|
67
|
+
false.otherwise { "No" } # => "No"
|
76
68
|
|
77
|
-
nil.otherwise { "No" }
|
78
|
-
# => "No"
|
69
|
+
nil.otherwise { "No" } # => "No"
|
79
70
|
```
|
80
71
|
### #maybe method
|
81
72
|
Returns first argument if "Truthy". If "Falsey" it either returns second argument or returns "Falsey" when only one argument.
|
82
73
|
```ruby
|
83
74
|
using NoConditionals
|
84
75
|
|
85
|
-
(3 > 2).maybe "Yes", maybe: "No"
|
86
|
-
# => "Yes"
|
76
|
+
(3 > 2).maybe "Yes", maybe: "No" # => "Yes"
|
87
77
|
|
88
|
-
(1 > 2).maybe "Yes", maybe: "No"
|
89
|
-
# => "No"
|
78
|
+
(1 > 2).maybe "Yes", maybe: "No" # => "No"
|
90
79
|
|
91
|
-
true.maybe "Yes"
|
92
|
-
# => "Yes"
|
80
|
+
true.maybe "Yes" # => "Yes"
|
93
81
|
|
94
|
-
false.maybe "Yes"
|
95
|
-
# => false
|
82
|
+
false.maybe "Yes" # => false
|
96
83
|
|
97
|
-
nil.maybe "Yes"
|
98
|
-
# => nil
|
84
|
+
nil.maybe "Yes" # => nil
|
99
85
|
```
|
100
86
|
|
101
87
|
### #maybe! method
|
@@ -103,23 +89,15 @@ Calls first argument if "Truthy". If "Falsey" it either calls second argument or
|
|
103
89
|
```ruby
|
104
90
|
using NoConditionals
|
105
91
|
|
106
|
-
(3 > 2).maybe! -> { puts "True"
|
107
|
-
# True
|
108
|
-
# => nil
|
92
|
+
(3 > 2).maybe! -> { puts "True" }, maybe: -> { puts "False" } # True
|
109
93
|
|
110
|
-
(1 > 2).maybe! -> { puts "True"
|
111
|
-
# False
|
112
|
-
# => nil
|
94
|
+
(1 > 2).maybe! -> { puts "True" }, maybe: -> { puts "False" } # False
|
113
95
|
|
114
|
-
true.maybe! -> { puts "True"
|
115
|
-
# True
|
116
|
-
# => nil
|
96
|
+
true.maybe! -> { puts "True" } # True
|
117
97
|
|
118
|
-
false.maybe! -> { puts "True"
|
119
|
-
# => nil
|
98
|
+
false.maybe! -> { puts "True" } # => false
|
120
99
|
|
121
|
-
nil.maybe! -> { puts "True"
|
122
|
-
# => nil
|
100
|
+
nil.maybe! -> { puts "True" } # => nil
|
123
101
|
```
|
124
102
|
|
125
103
|
## Sample codes
|
@@ -127,7 +105,7 @@ nil.maybe! -> { puts "True" }
|
|
127
105
|
### Replacing if-else conditions in controllers
|
128
106
|
```ruby
|
129
107
|
class SessionsController
|
130
|
-
|
108
|
+
...
|
131
109
|
using NoConditionals
|
132
110
|
|
133
111
|
def create
|
@@ -137,7 +115,7 @@ class SessionsController
|
|
137
115
|
.hence { logged_in(user) }
|
138
116
|
.otherwise { unauthenticated }
|
139
117
|
end
|
140
|
-
|
118
|
+
...
|
141
119
|
end
|
142
120
|
```
|
143
121
|
|
@@ -145,18 +123,18 @@ end
|
|
145
123
|
```ruby
|
146
124
|
class User
|
147
125
|
include NoConditionals::Truthiness
|
148
|
-
|
126
|
+
...
|
149
127
|
end
|
150
128
|
|
151
129
|
class MissingUser
|
152
130
|
include NoConditionals::Falseyness
|
153
|
-
|
131
|
+
...
|
154
132
|
end
|
155
133
|
```
|
156
134
|
|
157
135
|
### Replacing Object#try() in Rails application views
|
158
136
|
```erb
|
159
|
-
<%= @article.author.hence(&:name).otherwise {
|
137
|
+
<%= @article.author.hence(&:name).otherwise { "Anonymous" } %>
|
160
138
|
|
161
139
|
<%= current_user
|
162
140
|
.hence(&:admin?)
|
data/lib/no_conditionals.rb
CHANGED
data/no_conditionals.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["Ritchie Paul Buitre"]
|
10
10
|
spec.email = ["ritchie@richorelse.com"]
|
11
11
|
spec.summary = %q{A support library, to aid in eliminating conditionals from application code, for Ruby 2.1 and above.}
|
12
|
-
spec.description = %q{
|
12
|
+
spec.description = %q{Conditional statements, like if/else, have it's place, under libraries and frameworks, but not in application code. This library implements a collection of techniques to avoid if/else, initially by extending "Truthy" and "Falsey" classes with expressive yet Ruby idiomatic operations. Please look forward for more features to come.}
|
13
13
|
spec.homepage = "https://github.com/RichOrElse/no-conditionals"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
@@ -20,7 +20,7 @@ class TestNoConditionals < Minitest::Test
|
|
20
20
|
|
21
21
|
def test_it_refines_falsey_types
|
22
22
|
[ nil, false, (1 > 2)].each do |flsy|
|
23
|
-
assert_equal
|
23
|
+
assert_equal flsy, flsy.maybe!(-> {'Yes'})
|
24
24
|
assert_equal 'No', flsy.maybe!(-> {'Yes'}, maybe: -> {'No'})
|
25
25
|
assert_equal flsy, flsy.maybe('Yes')
|
26
26
|
assert_equal 'No', flsy.maybe('Yes', maybe: 'No')
|
@@ -36,8 +36,8 @@ describe NoConditionals::Falseyness do
|
|
36
36
|
end
|
37
37
|
|
38
38
|
describe "#maybe! with one argument" do
|
39
|
-
it "calls nothing" do
|
40
|
-
@falsey.maybe!(-> { :yes }).must_be_same_as
|
39
|
+
it "calls nothing, returns itself" do
|
40
|
+
@falsey.maybe!(-> { :yes }).must_be_same_as @falsey
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: no_conditionals
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ritchie Paul Buitre
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-01-
|
11
|
+
date: 2016-01-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,10 +52,11 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '5.0'
|
55
|
-
description:
|
56
|
-
|
57
|
-
|
58
|
-
with expressive yet Ruby idiomatic operations.
|
55
|
+
description: Conditional statements, like if/else, have it's place, under libraries
|
56
|
+
and frameworks, but not in application code. This library implements a collection
|
57
|
+
of techniques to avoid if/else, initially by extending "Truthy" and "Falsey" classes
|
58
|
+
with expressive yet Ruby idiomatic operations. Please look forward for more features
|
59
|
+
to come.
|
59
60
|
email:
|
60
61
|
- ritchie@richorelse.com
|
61
62
|
executables: []
|
@@ -64,6 +65,7 @@ extra_rdoc_files: []
|
|
64
65
|
files:
|
65
66
|
- ".gitignore"
|
66
67
|
- ".travis.yml"
|
68
|
+
- CHANGELOG.md
|
67
69
|
- Gemfile
|
68
70
|
- LICENSE.txt
|
69
71
|
- README.md
|