no_conditionals 1.0.0

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: b7c19ed19ce85ee0c791b543837920b743d92378
4
+ data.tar.gz: 5cdd188d84ae5b8a93d31869a943bb84e9bc7d14
5
+ SHA512:
6
+ metadata.gz: 71ee521865241d45d263de67a227199bede9c5ffff9091dea4fe86a265b1da227bd459ab060222e717e6f327bdc197dc59e90a1af83c3315b3228484df57dbfc
7
+ data.tar.gz: 881be1d1e812634f9a79e49641c9c8059dc4bf708ffab8b449690defa64ed7aa28ab0e2520f7883bb1cd2be44f3723e805295f3492145a7024400af57ecb671d
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ - 2.2.4
5
+ - 2.1.8
6
+ - 2.1.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in no_conditionals.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2016 Ritchie Paul Buitre
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,188 @@
1
+ # NoConditionals
2
+
3
+ Because conditional statements, such as 'if-then-else', belong to lower levels of abstraction (ie. libraries or frameworks), and not in higher level domains like your application. This gem provides extentions for "Truthy" and "Falsey" classes with expressive yet Ruby idiomatic operations.
4
+
5
+ ## Getting started
6
+
7
+ Make sure you have Ruby 2.1 or above, and then add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'no_conditionals'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install no_conditionals
20
+
21
+
22
+ ## Monkey patching Rails application
23
+ This library uses refinement to extend objects. Alternatively, you may also do the following:
24
+
25
+ Save this code to the Rails application folder as 'config/initializers/no_conditionals.rb'.
26
+ ```ruby
27
+ # config/initializers/no_conditionals.rb
28
+
29
+ Object.class_eval do
30
+ include NoConditionals::Truthiness
31
+ end
32
+
33
+ NilClass.class_eval do
34
+ include NoConditionals::Falseyness
35
+ end
36
+
37
+ FalseClass.class_eval do
38
+ include NoConditionals::Falseyness
39
+ end
40
+ ```
41
+ And then restart the server.
42
+
43
+ ## Usage
44
+
45
+ ### #hence method
46
+ Evaluates block if "Truthy" otherwise returns "Falsey"
47
+ ```ruby
48
+ using NoConditionals
49
+
50
+ true.hence { "Yes" }
51
+ # => "Yes"
52
+
53
+ false.hence { "Yes" }
54
+ # => false
55
+
56
+ nil.hence { "Yes" }
57
+ # => nil
58
+
59
+ [1,2,3,4,5].find {|n| n.odd? }.hence {|odd| "found #{odd}" }
60
+ # => "found 1"
61
+
62
+ names = ["", "Ruby", "Smalltalk", nil ]
63
+ names.first.hence(&:empty?)
64
+ # => true
65
+ ```
66
+ ### #otherwise method
67
+ Returns self if "Truthy" otherwise evaluates block.
68
+ ```ruby
69
+ using NoConditionals
70
+
71
+ true.otherwise { "No" }
72
+ # => true
73
+
74
+ false.otherwise { "No" }
75
+ # => "No"
76
+
77
+ nil.otherwise { "No" }
78
+ # => "No"
79
+ ```
80
+ ### #maybe method
81
+ Returns first argument if "Truthy". If "Falsey" it either returns second argument or returns "Falsey" when only one argument.
82
+ ```ruby
83
+ using NoConditionals
84
+
85
+ (3 > 2).maybe "Yes", maybe: "No"
86
+ # => "Yes"
87
+
88
+ (1 > 2).maybe "Yes", maybe: "No"
89
+ # => "No"
90
+
91
+ true.maybe "Yes"
92
+ # => "Yes"
93
+
94
+ false.maybe "Yes"
95
+ # => false
96
+
97
+ nil.maybe "Yes"
98
+ # => nil
99
+ ```
100
+
101
+ ### #maybe! method
102
+ Calls first argument if "Truthy". If "Falsey" it either calls second argument or calls nothing when only one argument.
103
+ ```ruby
104
+ using NoConditionals
105
+
106
+ (3 > 2).maybe! -> { puts "True" }, maybe: -> { puts "False" }
107
+ # True
108
+ # => nil
109
+
110
+ (1 > 2).maybe! -> { puts "True" }, maybe: -> { puts "False" }
111
+ # False
112
+ # => nil
113
+
114
+ true.maybe! -> { puts "True" }
115
+ # True
116
+ # => nil
117
+
118
+ false.maybe! -> { puts "True" }
119
+ # => nil
120
+
121
+ nil.maybe! -> { puts "True" }
122
+ # => nil
123
+ ```
124
+
125
+ ## Sample codes
126
+
127
+ ### Replacing if-else conditions in controllers
128
+ ```ruby
129
+ class SessionsController
130
+ # ...
131
+ using NoConditionals
132
+
133
+ def create
134
+ user = User.find_by_email params[:email]
135
+ user
136
+ .authenticate(params[:password])
137
+ .hence { logged_in(user) }
138
+ .otherwise { unauthenticated }
139
+ end
140
+ # ...
141
+ end
142
+ ```
143
+
144
+ ### Extending models and null objects
145
+ ```ruby
146
+ class User
147
+ include NoConditionals::Truthiness
148
+ # ...
149
+ end
150
+
151
+ class MissingUser
152
+ include NoConditionals::Falseyness
153
+ # ...
154
+ end
155
+ ```
156
+
157
+ ### Replacing Object#try() in Rails application views
158
+ ```erb
159
+ <%= @article.author.hence(&:name).otherwise { "Anonymous" } %>
160
+
161
+ <%= current_user
162
+ .hence(&:admin?)
163
+ .hence { link_to 'Edit, edit_article_path(@article) }
164
+ .otherwise { 'Read Only' } %>
165
+ ```
166
+ ### Chaining methods
167
+ ```ruby
168
+ class WeatherService
169
+ using NoConditionals
170
+
171
+ def weather_for(report)
172
+ report
173
+ .hence( &:source )
174
+ .hence( &:location )
175
+ .hence( &:city )
176
+ .hence( &:weather )
177
+ .otherwise { 'unknown weather' }
178
+ end
179
+ end
180
+ ```
181
+
182
+ ## Contributing
183
+
184
+ 1. Fork it ( https://github.com/RichOrElse/no-conditionals/fork )
185
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
186
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
187
+ 4. Push to the branch (`git push origin my-new-feature`)
188
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ end
7
+
8
+ task :default => :test
9
+
@@ -0,0 +1,3 @@
1
+ module NoConditionals
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,65 @@
1
+ require "no_conditionals/version"
2
+
3
+ # Refines truthy and falsey types
4
+ module NoConditionals
5
+
6
+ # Mixin for truthy behavior
7
+ module Truthiness
8
+
9
+ # evaluates a block, passes object
10
+ def hence
11
+ yield self
12
+ end
13
+
14
+ # just returns object, ignores block
15
+ def otherwise
16
+ self
17
+ end
18
+
19
+ # just returns first parameter (so), ignores optional second parameter (maybe)
20
+ def maybe so, maybe: self
21
+ so
22
+ end
23
+
24
+ # calls first parameter (so)
25
+ def maybe! so, maybe: -> {}
26
+ so.call
27
+ end
28
+ end
29
+
30
+ # Mixin for falsey behavior
31
+ module Falseyness
32
+
33
+ # just returns object, ignores block
34
+ def hence
35
+ self
36
+ end
37
+
38
+ # evaluates a block
39
+ def otherwise
40
+ yield self
41
+ end
42
+
43
+ # ignores first parameter (so), returns either object or keyword argument (maybe)
44
+ def maybe so, maybe: self
45
+ maybe
46
+ end
47
+
48
+ # calls keyword argument (maybe) or nothing
49
+ def maybe! so, maybe: -> {}
50
+ maybe.call
51
+ end
52
+ end
53
+
54
+ refine Object do
55
+ include Truthiness
56
+ end
57
+
58
+ refine NilClass do
59
+ include Falseyness
60
+ end
61
+
62
+ refine FalseClass do
63
+ include Falseyness
64
+ end
65
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'no_conditionals/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "no_conditionals"
8
+ spec.version = NoConditionals::VERSION
9
+ spec.authors = ["Ritchie Paul Buitre"]
10
+ spec.email = ["ritchie@richorelse.com"]
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{Because conditional statements, such as 'if-then-else', belong to lower levels of abstraction (ie. libraries or frameworks), and not in higher level domains like your application. This gem provides extentions for "Truthy" and "Falsey" classes with expressive yet Ruby idiomatic operations.}
13
+ spec.homepage = "https://github.com/RichOrElse/no-conditionals"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+ spec.required_ruby_version = ">= 2.1"
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.5"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "minitest", "~> 5.0"
25
+ end
@@ -0,0 +1,5 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'no_conditionals'
3
+
4
+ require 'minitest/spec'
5
+ require 'minitest/autorun'
@@ -0,0 +1,31 @@
1
+ require 'minitest_helper'
2
+
3
+ class TestNoConditionals < Minitest::Test
4
+ def test_that_it_has_a_version_number
5
+ refute_nil ::NoConditionals::VERSION
6
+ end
7
+
8
+ using NoConditionals
9
+
10
+ def test_it_refines_truthy_types
11
+ [ [], {}, "", 100, 0.5, 1..20, true, (3 > 2) ].each do |truthy|
12
+ assert_equal 'Yes', truthy.maybe!(-> {'Yes'})
13
+ assert_equal 'Yes', truthy.maybe!(-> {'Yes'}, maybe: -> {'No'})
14
+ assert_equal 'Yes', truthy.maybe('Yes')
15
+ assert_equal 'Yes', truthy.maybe('Yes', maybe: 'No')
16
+ assert_equal 'Yes', truthy.hence { 'Yes' }
17
+ assert_equal truthy, truthy.otherwise { 'No' }
18
+ end
19
+ end
20
+
21
+ def test_it_refines_falsey_types
22
+ [ nil, false, (1 > 2)].each do |flsy|
23
+ assert_equal nil, flsy.maybe!(-> {'Yes'})
24
+ assert_equal 'No', flsy.maybe!(-> {'Yes'}, maybe: -> {'No'})
25
+ assert_equal flsy, flsy.maybe('Yes')
26
+ assert_equal 'No', flsy.maybe('Yes', maybe: 'No')
27
+ assert_equal flsy, flsy.hence { 'Yes' }
28
+ assert_equal 'No', flsy.otherwise { 'No' }
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,53 @@
1
+ require 'minitest_helper'
2
+
3
+ describe NoConditionals::Falseyness do
4
+ before do
5
+ @falsey = Object.new.extend NoConditionals::Falseyness
6
+ end
7
+
8
+ describe "#hence" do
9
+ it "just returns itself" do
10
+ @falsey.hence { :yes }.must_be_same_as @falsey
11
+ @falsey.hence { |falsey| :yes }.must_be_same_as @falsey
12
+ end
13
+ end
14
+
15
+ describe "#otherwise" do
16
+ it "evaluates block" do
17
+ @falsey.otherwise { :no }.must_be_same_as :no
18
+ @falsey.otherwise { |falsey| falsey }.must_be_same_as @falsey
19
+ end
20
+ end
21
+
22
+ describe "#maybe with one argument" do
23
+ it "returns itself" do
24
+ @falsey.maybe(:yes).must_be_same_as @falsey
25
+ end
26
+ end
27
+
28
+ describe "#maybe with two arguments" do
29
+ it "returns last argument" do
30
+ @falsey.maybe(:yes, maybe: :no).must_be_same_as :no
31
+ end
32
+
33
+ it "raises error for wrong keyword argument" do
34
+ -> { @falsey.maybe :yes, otherwise: :no }.must_raise ArgumentError
35
+ end
36
+ end
37
+
38
+ describe "#maybe! with one argument" do
39
+ it "calls nothing" do
40
+ @falsey.maybe!(-> { :yes }).must_be_same_as nil
41
+ end
42
+ end
43
+
44
+ describe "#maybe! with two arguments" do
45
+ it "calls last argument" do
46
+ @falsey.maybe!(-> { :yes }, maybe: -> { :no }).must_be_same_as :no
47
+ end
48
+
49
+ it "raises error for wrong keyword argument" do
50
+ -> { @falsey.maybe! -> { :yes }, otherwise: -> { :no } }.must_raise ArgumentError
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,43 @@
1
+ require 'minitest_helper'
2
+
3
+ describe NoConditionals::Truthiness do
4
+ before do
5
+ @truthy = Object.new.extend NoConditionals::Truthiness
6
+ end
7
+
8
+ describe "#hence" do
9
+ it "evaluates a block" do
10
+ @truthy.hence { :yes }.must_be_same_as :yes
11
+ @truthy.hence { |truthy| truthy }.must_be_same_as @truthy
12
+ end
13
+ end
14
+
15
+ describe "#otherwise" do
16
+ it "just returns itself" do
17
+ @truthy.otherwise { :no }.must_be_same_as @truthy
18
+ @truthy.otherwise { |truthy| :no }.must_be_same_as @truthy
19
+ end
20
+ end
21
+
22
+ describe "#maybe" do
23
+ it "just returns first argument" do
24
+ @truthy.maybe(:yes).must_be_same_as :yes
25
+ @truthy.maybe(:yes, maybe: :no).must_be_same_as :yes
26
+ end
27
+
28
+ it "raises error for wrong keyword argument" do
29
+ -> { @truthy.maybe :yes, otherwise: :no }.must_raise ArgumentError
30
+ end
31
+ end
32
+
33
+ describe "#maybe!" do
34
+ it "calls first argument" do
35
+ @truthy.maybe!(-> { :yes }).must_be_same_as :yes
36
+ @truthy.maybe!(-> { :yes }, maybe: -> { :no }).must_be_same_as :yes
37
+ end
38
+
39
+ it "raises error for wrong keyword argument" do
40
+ -> { @truthy.maybe! -> { :yes }, otherwise: -> { :no } }.must_raise ArgumentError
41
+ end
42
+ end
43
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: no_conditionals
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ritchie Paul Buitre
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-26 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.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: Because conditional statements, such as 'if-then-else', belong to lower
56
+ levels of abstraction (ie. libraries or frameworks), and not in higher level domains
57
+ like your application. This gem provides extentions for "Truthy" and "Falsey" classes
58
+ with expressive yet Ruby idiomatic operations.
59
+ email:
60
+ - ritchie@richorelse.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - ".gitignore"
66
+ - ".travis.yml"
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - lib/no_conditionals.rb
72
+ - lib/no_conditionals/version.rb
73
+ - no_conditionals.gemspec
74
+ - test/minitest_helper.rb
75
+ - test/test_no_conditionals.rb
76
+ - test/test_no_conditionals_falseyness.rb
77
+ - test/test_no_conditionals_truthiness.rb
78
+ homepage: https://github.com/RichOrElse/no-conditionals
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '2.1'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.2.3
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: A support library, to aid in eliminating conditionals from application code,
102
+ for Ruby 2.1 and above.
103
+ test_files:
104
+ - test/minitest_helper.rb
105
+ - test/test_no_conditionals.rb
106
+ - test/test_no_conditionals_falseyness.rb
107
+ - test/test_no_conditionals_truthiness.rb