confident_ruby 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +166 -0
- data/Rakefile +2 -0
- data/confident_ruby.gemspec +23 -0
- data/lib/confident.rb +5 -0
- data/lib/confident/version.rb +3 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f02a5207d69e5ec3aeccccc3ef3984b474b62d51
|
4
|
+
data.tar.gz: 9c5e15fed060781bb3389a48828de782b5756ed6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a2d015875987eb01a9e87538b466714ff5917c9bc4d920ccc10d8e4141dfcdd48796fa64afbab1a03f5e6171be36d285058153ce9919857d066f9f80414435e3
|
7
|
+
data.tar.gz: c477da18f1db3a8d3ea3bb14af91fc37e846bc4ea5813a052e113678ab6c45c53a8bdc36bfcb0d44380157e1565228c56e41ec71029bc489e957b59c0ba10251
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Alex Fedorov
|
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,166 @@
|
|
1
|
+
# Confident.ruby
|
2
|
+
|
3
|
+
Be confident and narrative when writing code in ruby.
|
4
|
+
|
5
|
+
Gem contains useful abstractions for eliminating most condition and switch smells, treating anything just like a duck, implementing barricade and null object pattern efficiently.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'confident_ruby'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install confident_ruby
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
### Eliminating condition
|
26
|
+
|
27
|
+
Given this code:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
class Order
|
31
|
+
# ...
|
32
|
+
|
33
|
+
def charge
|
34
|
+
if date < SEASON_START || date > SEASON_END
|
35
|
+
quantity * normal_rate - discount
|
36
|
+
else
|
37
|
+
quantity * season_rate
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# ...
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
Becomes:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
class Order
|
49
|
+
# ...
|
50
|
+
|
51
|
+
def charge
|
52
|
+
Charge.lift(date).for(quantity)
|
53
|
+
end
|
54
|
+
|
55
|
+
# ...
|
56
|
+
end
|
57
|
+
|
58
|
+
Charge = Confident::Liftable.build do
|
59
|
+
lifts(to: :NormalCharge) { date < SEASON_START || date > SEASON_END }
|
60
|
+
lifts(to: :SeasonCharge) { true }
|
61
|
+
|
62
|
+
class NormalCharge < self
|
63
|
+
def for(quantity)
|
64
|
+
Money.lift(normal_rate) * quantity - Money.lift(discount)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
class SeasonCharge < self
|
69
|
+
def for(quantity)
|
70
|
+
Money.lift(season_rate) * quantity
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
76
|
+
### Eliminating switch
|
77
|
+
|
78
|
+
Given this code:
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
class Product
|
82
|
+
|
83
|
+
# ...
|
84
|
+
|
85
|
+
def cost
|
86
|
+
case kind
|
87
|
+
when "hosting"
|
88
|
+
hosting_cost
|
89
|
+
when "dedicated"
|
90
|
+
dedicated_cost
|
91
|
+
when "virtual"
|
92
|
+
virtual_cost
|
93
|
+
else
|
94
|
+
raise "Unreachable code"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# ...
|
99
|
+
|
100
|
+
end
|
101
|
+
```
|
102
|
+
|
103
|
+
becomes:
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
class Product
|
107
|
+
|
108
|
+
# ...
|
109
|
+
|
110
|
+
def cost
|
111
|
+
kind.cost
|
112
|
+
end
|
113
|
+
|
114
|
+
def kind
|
115
|
+
ProductKind.lift(@kind)
|
116
|
+
end
|
117
|
+
|
118
|
+
# ...
|
119
|
+
|
120
|
+
end
|
121
|
+
|
122
|
+
# could be abstracted even better, if the whole thing was in database source
|
123
|
+
ProductKind = Confident::Liftable.build do
|
124
|
+
lift_map "hosting" => :HostingProductKind,
|
125
|
+
"dedicated" => :DedicatedProductKind,
|
126
|
+
"virtual" => :VirtualProductKind
|
127
|
+
|
128
|
+
def cost
|
129
|
+
Confident::Result.error("Unknown product kind")
|
130
|
+
end
|
131
|
+
|
132
|
+
class HostingProductKind < self
|
133
|
+
def cost
|
134
|
+
Confident::Result.ok(Money.lift(hosting_kind))
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
class DedicatedProductKind < self
|
139
|
+
def cost
|
140
|
+
Confident::Result.ok(Money.lift(dedicated_kind))
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
class VirtualProductKind < self
|
145
|
+
def cost
|
146
|
+
Confident::Result.ok(Money.lift(virtual_kind))
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
```
|
151
|
+
|
152
|
+
Looks like we expanding our code and making it bigger at no time, but it is actually beneficial to us, because we don't need to duplicate knowledge on how to determine `kind` of product throughout a codebase, we just call `Product.lift(kind_string_value)`, and we are done with it. You can implement this stuff in raw ruby easily, but this library wants to cut of a boilerplate code involved in this usually.
|
153
|
+
|
154
|
+
### TODO (to specify in this README)
|
155
|
+
|
156
|
+
- lift & bind
|
157
|
+
- null object
|
158
|
+
- barricade
|
159
|
+
|
160
|
+
## Contributing
|
161
|
+
|
162
|
+
1. Fork it ( https://github.com/waterlink/confident.ruby/fork )
|
163
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
164
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
165
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
166
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'confident/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "confident_ruby"
|
8
|
+
spec.version = Confident::VERSION
|
9
|
+
spec.authors = ["Alexey Fedorov"]
|
10
|
+
spec.email = ["waterlink000@gmail.com"]
|
11
|
+
spec.summary = %q{Be confident and narrative when writing code in ruby.}
|
12
|
+
spec.description = %q{Gem contains useful abstractions for eliminating most condition and switch smells, treating anything just like a duck, implementing barricade and null object pattern efficiently.}
|
13
|
+
spec.homepage = "https://github.com/waterlink/confident.ruby"
|
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
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
end
|
data/lib/confident.rb
ADDED
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: confident_ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alexey Fedorov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-18 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.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
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
|
+
description: Gem contains useful abstractions for eliminating most condition and switch
|
42
|
+
smells, treating anything just like a duck, implementing barricade and null object
|
43
|
+
pattern efficiently.
|
44
|
+
email:
|
45
|
+
- waterlink000@gmail.com
|
46
|
+
executables: []
|
47
|
+
extensions: []
|
48
|
+
extra_rdoc_files: []
|
49
|
+
files:
|
50
|
+
- ".gitignore"
|
51
|
+
- Gemfile
|
52
|
+
- LICENSE.txt
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- confident_ruby.gemspec
|
56
|
+
- lib/confident.rb
|
57
|
+
- lib/confident/version.rb
|
58
|
+
homepage: https://github.com/waterlink/confident.ruby
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata: {}
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 2.2.2
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: Be confident and narrative when writing code in ruby.
|
82
|
+
test_files: []
|