normally 0.0.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.
- data/License.txt +21 -0
- data/README.md +52 -0
- data/lib/normally.rb +15 -0
- metadata +50 -0
data/License.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 Ben Christel
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# Normally
|
2
|
+
|
3
|
+
## Prioritizing the Happy Path
|
4
|
+
|
5
|
+
Much of the time when talking about conditional expressions, the concepts of a _happy path_ and a _sad path_ are useful. The happy path is the branch of the conditional that executes during the normal operation of your program. The sad path is there only to deal with errors or invalid input. When test-driving code, I normally focus on the happy path first. When reading code, I like to see the happy path first because the happy path tells me what the code is supposed to do, while the sad path just tells me how the code avoids doing things it's not supposed to do.
|
6
|
+
|
7
|
+
Despite the importance of the happy path, the sad path is often the most visible in code with conditionals. Consider the following:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
def response_data(response)
|
11
|
+
if response.success?
|
12
|
+
JSON.parse(response.body)
|
13
|
+
else
|
14
|
+
nil
|
15
|
+
end
|
16
|
+
end
|
17
|
+
```
|
18
|
+
|
19
|
+
The first thing we see when we look at this method is not the happy path, but the conditional that separates the normal case from the unexpected case. You have to read and understand this conditional before you can even tell which of its branches is the normal case.
|
20
|
+
|
21
|
+
What we'd really like is something like this:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
def response_data(response)
|
25
|
+
normally { JSON.parse(response.body) }
|
26
|
+
.but_if (response.error?) { nil }
|
27
|
+
end
|
28
|
+
```
|
29
|
+
|
30
|
+
Now the happy path is front and center. As you may have guessed, the `normally` gem makes this code actually work.
|
31
|
+
|
32
|
+
## Installation
|
33
|
+
|
34
|
+
You know the drill. If you're using [bundler](https://rubygems.org/gems/bundler), add this to your `Gemfile`:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
gem 'normally'
|
38
|
+
```
|
39
|
+
|
40
|
+
To install globally, do `gem install normally`
|
41
|
+
|
42
|
+
The `normally` method is part of the `Normally` module, which must be included either in the global namespace or in the class where you want to use the `normally` syntax. For example:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
class Foo
|
46
|
+
include Normally
|
47
|
+
|
48
|
+
def do_something
|
49
|
+
normally { 'foo' }.but_if(error?) { 'fubar' }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
```
|
data/lib/normally.rb
ADDED
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: normally
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ben Christel
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-06-13 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Prioritize the happy path.
|
15
|
+
email:
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files:
|
19
|
+
- README.md
|
20
|
+
files:
|
21
|
+
- lib/normally.rb
|
22
|
+
- License.txt
|
23
|
+
- README.md
|
24
|
+
homepage: http://github.com/benchristel/normally
|
25
|
+
licenses:
|
26
|
+
- MIT
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options:
|
29
|
+
- --charset=UTF-8
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubyforge_project:
|
46
|
+
rubygems_version: 1.8.23
|
47
|
+
signing_key:
|
48
|
+
specification_version: 3
|
49
|
+
summary: normally-0.0.0
|
50
|
+
test_files: []
|