policy 1.0.1
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 +7 -0
- data/.coveralls.yml +2 -0
- data/.metrics +5 -0
- data/.rspec +2 -0
- data/.rubocop.yml +2 -0
- data/.travis.yml +18 -0
- data/.yardopts +3 -0
- data/Gemfile +7 -0
- data/Guardfile +15 -0
- data/LICENSE +21 -0
- data/README.md +223 -0
- data/Rakefile +17 -0
- data/config/metrics/STYLEGUIDE +231 -0
- data/config/metrics/cane.yml +5 -0
- data/config/metrics/churn.yml +6 -0
- data/config/metrics/flay.yml +2 -0
- data/config/metrics/metric_fu.yml +14 -0
- data/config/metrics/pippi.yml +3 -0
- data/config/metrics/reek.yml +1 -0
- data/config/metrics/roodi.yml +24 -0
- data/config/metrics/rubocop.yml +87 -0
- data/config/metrics/saikuro.yml +3 -0
- data/config/metrics/simplecov.yml +5 -0
- data/config/metrics/yardstick.yml +37 -0
- data/lib/policy/follower/followed_policies.rb +45 -0
- data/lib/policy/follower/followed_policy.rb +104 -0
- data/lib/policy/follower/names.rb +29 -0
- data/lib/policy/follower.rb +143 -0
- data/lib/policy/interface.rb +48 -0
- data/lib/policy/validations.rb +28 -0
- data/lib/policy/version.rb +9 -0
- data/lib/policy/violation_error.rb +52 -0
- data/lib/policy.rb +40 -0
- data/policy.gemspec +23 -0
- data/spec/features/follower_spec.rb +95 -0
- data/spec/spec_helper.rb +10 -0
- data/spec/tests/policy/follower/followed_policies_spec.rb +87 -0
- data/spec/tests/policy/follower/followed_policy_spec.rb +117 -0
- data/spec/tests/policy/follower/names_spec.rb +19 -0
- data/spec/tests/policy/follower_spec.rb +220 -0
- data/spec/tests/policy/interface_spec.rb +83 -0
- data/spec/tests/policy/validations_spec.rb +13 -0
- data/spec/tests/policy/violation_error_spec.rb +75 -0
- data/spec/tests/policy_spec.rb +35 -0
- metadata +142 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 04a2d9deed6be482ce3cb6ce53a37794c63f9307
|
4
|
+
data.tar.gz: a3390592a7f9da64d2312d1deb2f252845145dd7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 822eb64145e1bd5b1eb50db79e806ad4bdcf4030cedada995854402331532ae62941d0eec77947555af0aeb201484698e502e3d14c434cd10ca09f3b14022ac3
|
7
|
+
data.tar.gz: df4bd42ef984ce5daf5465af0646536ff3a2683512f1e977d5482e12032a5380e9b447f5709633818de33c7dcfcea051befbf0942fa2dfe4616ac7e165a6b4d2
|
data/.coveralls.yml
ADDED
data/.metrics
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
---
|
2
|
+
language: ruby
|
3
|
+
bundler_args: --without=extras
|
4
|
+
script: rake test:coverage
|
5
|
+
matrix:
|
6
|
+
include:
|
7
|
+
- rvm: '2.0'
|
8
|
+
env: 'activemodel=3.1'
|
9
|
+
- rvm: 'ruby-head'
|
10
|
+
env: 'activemodel=master'
|
11
|
+
- rvm: 'rbx-2 --2.0'
|
12
|
+
env: 'activemodel=3.1'
|
13
|
+
- rvm: 'rbx-2 --2.0'
|
14
|
+
env: 'activemodel=master'
|
15
|
+
- rvm: 'jruby-20mode'
|
16
|
+
env: 'activemodel=3.1'
|
17
|
+
- rvm: 'jruby-head-20mode'
|
18
|
+
env: 'activemodel=master'
|
data/.yardopts
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
guard :rspec, cmd: "bundle exec rspec" do
|
4
|
+
|
5
|
+
watch("spec/spec_helper.rb") { "spec" }
|
6
|
+
|
7
|
+
watch("lib/policy.rb") { "spec" }
|
8
|
+
|
9
|
+
watch(/^lib(.+)\.rb$/) do |m|
|
10
|
+
"spec/tests#{ m[1] }_spec.rb"
|
11
|
+
end
|
12
|
+
|
13
|
+
watch(/^spec.+_spec\.rb$/)
|
14
|
+
|
15
|
+
end # guard :rspec
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2015 Andrew Kozin, https://github.com/nepalez
|
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,223 @@
|
|
1
|
+
Policy
|
2
|
+
======
|
3
|
+
|
4
|
+
[][gem]
|
5
|
+
[][travis]
|
6
|
+
[][gemnasium]
|
7
|
+
[][codeclimate]
|
8
|
+
[][coveralls]
|
9
|
+
[][inch]
|
10
|
+
|
11
|
+
[codeclimate]: https://codeclimate.com/github/nepalez/policy
|
12
|
+
[coveralls]: https://coveralls.io/r/nepalez/policy
|
13
|
+
[gem]: https://rubygems.org/gems/policy
|
14
|
+
[gemnasium]: https://gemnasium.com/nepalez/policy
|
15
|
+
[travis]: https://travis-ci.org/nepalez/policy
|
16
|
+
[inch]: https://inch-ci.org/github/nepalez/policy
|
17
|
+
|
18
|
+
A tiny library to implement a **Policy Object pattern**.
|
19
|
+
|
20
|
+
The gem was inspired by:
|
21
|
+
* the CodeClimate's blog post "[7 ways to decompose fat ActiveRecord module]"
|
22
|
+
* the part "How to Model Less Obvious Kinds of Concept" from the "[Domain-Driven Design]" by Eric Evans.
|
23
|
+
|
24
|
+
A **Policy Object** encapsulates a business rule in isolation from objects (such as entities or services) following it.
|
25
|
+
|
26
|
+
This separation provides a number of benefits:
|
27
|
+
|
28
|
+
* It makes business rules **explicit** instead of spreading and hiding them inside application objects.
|
29
|
+
* It makes the rules **reusable** in various context (think of the *transaction consistency* both in bank transfers and cach machine withdrawals).
|
30
|
+
* It allows definition of rules for **numerous attributes** that should correspond to each other in some way.
|
31
|
+
* It makes complex rules **testable** in isolation from even more complex objects.
|
32
|
+
|
33
|
+
[7 ways to decompose fat ActiveRecord module]: http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/
|
34
|
+
[Domain-Driven Design]: http://www.amazon.com/dp/B00794TAUG/
|
35
|
+
|
36
|
+
# Installation
|
37
|
+
|
38
|
+
Add this line to your application's Gemfile:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
gem "policy"
|
42
|
+
```
|
43
|
+
|
44
|
+
And then execute:
|
45
|
+
|
46
|
+
```
|
47
|
+
$ bundle
|
48
|
+
```
|
49
|
+
|
50
|
+
Or install it yourself as:
|
51
|
+
|
52
|
+
```
|
53
|
+
$ gem install policy
|
54
|
+
```
|
55
|
+
|
56
|
+
# Usage
|
57
|
+
|
58
|
+
## The Model for Illustration
|
59
|
+
|
60
|
+
Suppose an over-simplified model of bank account transactions and account-to-account transfers.
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
# The account transaction (either enrollment or witdrawal)
|
64
|
+
class Transaction < Struct.new(:sum); end
|
65
|
+
|
66
|
+
# The transfer, connecting two separate transactions
|
67
|
+
# (maybe this isn't an optimal model, but helpful for the subject)
|
68
|
+
class Transfer < Struct.new(:withdrawal, :enrollment); end
|
69
|
+
```
|
70
|
+
|
71
|
+
What we need is to apply the simple policy (invariant):
|
72
|
+
|
73
|
+
**The sum of withdrawal's and enrollment's sums should be 0.**
|
74
|
+
|
75
|
+
Let's do it with Policy Objects!
|
76
|
+
|
77
|
+
## Policy Declaration
|
78
|
+
|
79
|
+
Define policies with a list of necessary attributes like using [Struct].
|
80
|
+
|
81
|
+
Tnen use [ActiveModel::Validations] methods to describe its rules:
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
# An arbitrary namespace for financial policies
|
85
|
+
module Policies::Financial
|
86
|
+
|
87
|
+
# Withdrawal from one account should be equal to enrollment to another
|
88
|
+
class Consistency < Policy.new(:withdrawal, :enrollment)
|
89
|
+
|
90
|
+
validates :withdrawal, :enrollment, presence: true
|
91
|
+
validates :total_sum, numericality: { equal_to: 0 }
|
92
|
+
|
93
|
+
private
|
94
|
+
|
95
|
+
def total_sum
|
96
|
+
withdrawal.sum + enrollment.sum
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
```
|
101
|
+
|
102
|
+
Note a policy knows nothing about the complex nature of its attributes until their quack like transactions with `#sum` method defined.
|
103
|
+
|
104
|
+
[Struct]: http://ruby-doc.org//core-2.2.0/Struct.html
|
105
|
+
[ActiveModel::Validations]: http://apidock.com/rails/ActiveModel/Validations
|
106
|
+
|
107
|
+
## Following a Policy
|
108
|
+
|
109
|
+
Include the `Policy::Follower` module to the class and apply policies to corresponding attributes with `follow_policy` **class** method.
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
class Transfer < Struct.new(:withdrawal, :enrollment)
|
113
|
+
include Policy::Follower # also includes ActiveModel::Validations
|
114
|
+
|
115
|
+
follow_policy Policies::Financial::Consistency, :withdrawal, :enrollment
|
116
|
+
end
|
117
|
+
```
|
118
|
+
|
119
|
+
The order of attributes should correspond to the policy definition.
|
120
|
+
|
121
|
+
You can swap attributes (this is ok for our example)...
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
follow_policy Policies::Financial::Consistency, :enrollment, :withdrawal
|
125
|
+
```
|
126
|
+
|
127
|
+
...or use the same attribute several times when necessary (not in our example, though):
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
follow_policy Policies::Financial::Consistency, :withdrawal, :withdrawal
|
131
|
+
```
|
132
|
+
|
133
|
+
Applied policies can be grouped by namespaces (useful when the object should follow many policies):
|
134
|
+
|
135
|
+
```ruby
|
136
|
+
use_policies Policies::Financial do
|
137
|
+
follow_policy :Consistency, :withdrawal, :enrollment
|
138
|
+
end
|
139
|
+
```
|
140
|
+
|
141
|
+
## Policies Verification
|
142
|
+
|
143
|
+
To verify object use `#follow_policies?` or `#follow_policies!` **instance** methods.
|
144
|
+
|
145
|
+
```ruby
|
146
|
+
Transaction = Struct.new(:account, :sum)
|
147
|
+
withdrawal = Transaction.new(account_1, -100)
|
148
|
+
enrollment = Transaction.new(account_2, 1000)
|
149
|
+
|
150
|
+
transfer = Transfer.new withdrawal, enrollment
|
151
|
+
|
152
|
+
transfer.follow_policies?
|
153
|
+
# => false
|
154
|
+
|
155
|
+
transfer.follow_policies!
|
156
|
+
# => raises <Policy::ViolationError>
|
157
|
+
```
|
158
|
+
|
159
|
+
The policies are verified one-by-one until the first break - in just the same order they were declared.
|
160
|
+
|
161
|
+
### Asyncronous Verification
|
162
|
+
|
163
|
+
Define names for policies using `as:` option. The names should be unique in the class' scope:
|
164
|
+
|
165
|
+
```ruby
|
166
|
+
class Transfer < Struct.new(:withdrawal, :enrollment)
|
167
|
+
include Policy::Follower
|
168
|
+
|
169
|
+
use_policies Policies::Financial do
|
170
|
+
follow_policy :Consistency, :withdrawal, :enrollment, as: :consistency
|
171
|
+
end
|
172
|
+
end
|
173
|
+
```
|
174
|
+
|
175
|
+
Check policies by names (you can also use singular forms `follow_policy?` and `follow_policy!`):
|
176
|
+
|
177
|
+
```ruby
|
178
|
+
# Checks only consistency and skips all other policies
|
179
|
+
transaction.follow_policy? :consistency
|
180
|
+
transaction.follow_policy! :consistency
|
181
|
+
```
|
182
|
+
|
183
|
+
The set of policies can be checked at once:
|
184
|
+
|
185
|
+
```ruby
|
186
|
+
transaction.follow_policies? :consistency, ...
|
187
|
+
```
|
188
|
+
|
189
|
+
Now the policies are verified one-by-one in **given order** (it may differ from the order of policies declaration) until the first break.
|
190
|
+
|
191
|
+
# Compatibility
|
192
|
+
|
193
|
+
Tested under rubies, compatible with MRI 2.0+:
|
194
|
+
|
195
|
+
* MRI rubies 2.0+
|
196
|
+
* Rubinius 2+ (2.0+ mode)
|
197
|
+
* JRuby 1.7+ (2.0+ mode)
|
198
|
+
|
199
|
+
Rubies with API 1.9 are not supported.
|
200
|
+
|
201
|
+
Uses [ActiveModel::Validations] - tested for 3.1+
|
202
|
+
|
203
|
+
Uses [RSpec] 3.0+ for testing and [hexx-suit] for dev/test tools collection.
|
204
|
+
|
205
|
+
[RSpec]: http://rspec.info/
|
206
|
+
[hexx-suit]: https://github.com/nepalez/hexx-suit/
|
207
|
+
[ActiveModel::Validations]: http://apidock.com/rails/v3.1.0/ActiveModel/Validations
|
208
|
+
|
209
|
+
# Contributing
|
210
|
+
|
211
|
+
* Fork the project.
|
212
|
+
* Read the [STYLEGUIDE](config/metrics/STYLEGUIDE).
|
213
|
+
* Make your feature addition or bug fix.
|
214
|
+
* Add tests for it. This is important so I don't break it in a
|
215
|
+
future version unintentionally.
|
216
|
+
* Commit, do not mess with Rakefile or version
|
217
|
+
(if you want to have your own version, that is fine but bump version
|
218
|
+
in a commit by itself I can ignore when I pull)
|
219
|
+
* Send me a pull request. Bonus points for topic branches.
|
220
|
+
|
221
|
+
# License
|
222
|
+
|
223
|
+
See [MIT LICENSE](LICENSE).
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
begin
|
3
|
+
require "bundler/setup"
|
4
|
+
rescue LoadError
|
5
|
+
puts "You must `gem install bundler` and `bundle install` to run rake tasks"
|
6
|
+
exit
|
7
|
+
end
|
8
|
+
|
9
|
+
# Loads bundler tasks
|
10
|
+
Bundler::GemHelper.install_tasks
|
11
|
+
|
12
|
+
# Loads the Hexx::Suit and its tasks
|
13
|
+
require "hexx-suit"
|
14
|
+
Hexx::Suit.install_tasks
|
15
|
+
|
16
|
+
# Sets the Hexx::RSpec :test task to default
|
17
|
+
task default: :test
|
@@ -0,0 +1,231 @@
|
|
1
|
+
= Ruby Style Guide
|
2
|
+
|
3
|
+
Adapted from Dan Kubb's Ruby Style Guide
|
4
|
+
https://github.com/dkubb/styleguide/blob/master/RUBY-STYLE
|
5
|
+
|
6
|
+
== Commiting:
|
7
|
+
|
8
|
+
* Write descriptive commit messages, following the pattern:
|
9
|
+
|
10
|
+
[TYPE] name
|
11
|
+
|
12
|
+
The message, describing the changes being made
|
13
|
+
|
14
|
+
* Use the types below to mark commits:
|
15
|
+
|
16
|
+
- FEATURE - for adding new features, or backward-compatible changes;
|
17
|
+
- UPDATE - for backward-compatible changes of existing code;
|
18
|
+
- CHANGE - for backward-incompatible changes;
|
19
|
+
- BUG FIX - for fixing bugs;
|
20
|
+
- REFACTORING - for other changes of the code not affecting the API;
|
21
|
+
- OTHER - for changes in documentaton, metrics etc, not touching the code;
|
22
|
+
- VERSION - for version changes.
|
23
|
+
|
24
|
+
* Try to separate commits of different types (such as FEATURE and CHANGE).
|
25
|
+
|
26
|
+
* Try to separate various features from each other.
|
27
|
+
|
28
|
+
* Include specification to the same commit as the code.
|
29
|
+
|
30
|
+
* Run all tests before making a commit.
|
31
|
+
Never commit the code that break unit tests.
|
32
|
+
|
33
|
+
* Use metric (run `rake check`) before making a commit.
|
34
|
+
|
35
|
+
* Do refactoring before making a commit. Best writing is rewriting.
|
36
|
+
|
37
|
+
* Follow semantic versioning.
|
38
|
+
|
39
|
+
http://semver.org/
|
40
|
+
|
41
|
+
* For versions name the commit after a version number, following the pattern:
|
42
|
+
|
43
|
+
VERSION 1.0.0-rc2
|
44
|
+
|
45
|
+
|
46
|
+
== Formatting:
|
47
|
+
|
48
|
+
* Use UTF-8. Declare encoding in the first line of every file.
|
49
|
+
|
50
|
+
# encoding: utf-8
|
51
|
+
|
52
|
+
* Use 2 space indent, no tabs.
|
53
|
+
|
54
|
+
* Use Unix-style line endings.
|
55
|
+
|
56
|
+
* Use spaces around operators, after commas, colons and semicolons,
|
57
|
+
around { and before }.
|
58
|
+
|
59
|
+
* No spaces after (, [ and before ], ).
|
60
|
+
|
61
|
+
* Align `when` and `else` with `case`.
|
62
|
+
|
63
|
+
* Use an empty line before the return value of a method (unless it
|
64
|
+
only has one line), and an empty line between defs.
|
65
|
+
|
66
|
+
* Use empty lines to break up a long method into logical paragraphs.
|
67
|
+
|
68
|
+
* Keep lines fewer than 80 characters.
|
69
|
+
|
70
|
+
* Strip trailing whitespace.
|
71
|
+
|
72
|
+
|
73
|
+
== Syntax:
|
74
|
+
|
75
|
+
* Write for 2.0.
|
76
|
+
|
77
|
+
* Use double quotes
|
78
|
+
|
79
|
+
http://viget.com/extend/just-use-double-quoted-ruby-strings
|
80
|
+
|
81
|
+
* Use def with parentheses when there are arguments.
|
82
|
+
|
83
|
+
* Never use for, unless you exactly know why.
|
84
|
+
|
85
|
+
* Never use then, except in case statements.
|
86
|
+
|
87
|
+
* Use when x then ... for one-line cases.
|
88
|
+
|
89
|
+
* Use &&/|| for boolean expressions, and/or for control flow. (Rule
|
90
|
+
of thumb: If you have to use outer parentheses, you are using the
|
91
|
+
wrong operators.)
|
92
|
+
|
93
|
+
* Avoid double negation (!!), unless Null Objects are expected.
|
94
|
+
|
95
|
+
http://devblog.avdi.org/2011/05/30/null-objects-and-falsiness
|
96
|
+
|
97
|
+
* Avoid multiline ?:, use if.
|
98
|
+
|
99
|
+
* Use {...} when defining blocks on one line. Use do...end for multiline
|
100
|
+
blocks.
|
101
|
+
|
102
|
+
* Avoid return where not required.
|
103
|
+
|
104
|
+
* Use ||= freely.
|
105
|
+
|
106
|
+
* Use OO regexps, and avoid =~ $0-9, $~, $` and $' when possible.
|
107
|
+
|
108
|
+
* Do not use Enumerable#inject when the "memo" object does not change between
|
109
|
+
iterations, use Enumerable#each_with_object instead (in ruby 1.9,
|
110
|
+
active_support and backports).
|
111
|
+
|
112
|
+
* Prefer ENV.fetch to ENV[] syntax.
|
113
|
+
Prefer block syntax for ENV.fetch to usage of the second argument.
|
114
|
+
|
115
|
+
|
116
|
+
== Naming:
|
117
|
+
|
118
|
+
* Use snake_case for methods.
|
119
|
+
|
120
|
+
* Use CamelCase for classes and modules. (Keep acronyms like HTTP,
|
121
|
+
RFC, XML uppercase.)
|
122
|
+
|
123
|
+
* Use SCREAMING_SNAKE_CASE for other constants.
|
124
|
+
|
125
|
+
* Do not use single letter variable names. Avoid uncommunicative names.
|
126
|
+
|
127
|
+
* Use consistent variable names. Try to keep the variable names close
|
128
|
+
to the object class name.
|
129
|
+
|
130
|
+
* Use names prefixed with _ for unused variables.
|
131
|
+
|
132
|
+
* When defining a predicate method that compares against another object of
|
133
|
+
a similar type, name the argument "other".
|
134
|
+
|
135
|
+
* Prefer map over collect, detect over find, select over find_all.
|
136
|
+
|
137
|
+
* Use def self.method to define singleton methods.
|
138
|
+
|
139
|
+
* Avoid alias when alias_method will do.
|
140
|
+
|
141
|
+
|
142
|
+
== Comments:
|
143
|
+
|
144
|
+
* Use YARD and its conventions for API documentation. Don't put an
|
145
|
+
empty line between the comment block and the def.
|
146
|
+
|
147
|
+
* Comments longer than a word are capitalized and use punctuation.
|
148
|
+
Use one space after periods.
|
149
|
+
|
150
|
+
* Avoid superfluous comments.
|
151
|
+
|
152
|
+
|
153
|
+
== Code structuring:
|
154
|
+
|
155
|
+
* Break code into packages, decoupled from the environment.
|
156
|
+
|
157
|
+
* Wrap packages into gems.
|
158
|
+
|
159
|
+
* Inject dependencies explicitly.
|
160
|
+
Leave all outer references on the border of any package. Inside
|
161
|
+
the package use internal references only.
|
162
|
+
|
163
|
+
* Follow SOLID principles.
|
164
|
+
|
165
|
+
http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)
|
166
|
+
|
167
|
+
* Only give a method one purpose for existing. If you pass in a boolean
|
168
|
+
to a method, what you're saying is that this method has two different
|
169
|
+
behaviours. Just split it into two single purpose methods. If you have
|
170
|
+
to use the words "AND" or "OR" to describe what the method does it
|
171
|
+
probably does too much.
|
172
|
+
|
173
|
+
* Avoid long methods.
|
174
|
+
Try to keep them at no more than 6 lines long, and preferably 4 or less.
|
175
|
+
|
176
|
+
If sections of a method are logically separate by blank lines, then
|
177
|
+
that's probably a sign that those sections should be split into separate
|
178
|
+
methods.
|
179
|
+
|
180
|
+
* Avoid hashes-as-optional-parameters. Does the method do too much?
|
181
|
+
|
182
|
+
* Avoid long parameter lists.
|
183
|
+
|
184
|
+
* Add "global" methods to Kernel (if you have to) and make them private.
|
185
|
+
|
186
|
+
* Use OptionParser for parsing complex command line options and
|
187
|
+
ruby -s for trivial command line options.
|
188
|
+
|
189
|
+
* Avoid needless metaprogramming.
|
190
|
+
|
191
|
+
* Always freeze objects assigned to constants.
|
192
|
+
|
193
|
+
|
194
|
+
== General:
|
195
|
+
|
196
|
+
* Code in a functional way, avoid mutation when it makes sense.
|
197
|
+
|
198
|
+
* Try to have methods either return the state of the object and have
|
199
|
+
no side effects, or return self and have side effects. This is
|
200
|
+
otherwise known as Command-query separation (CQS):
|
201
|
+
|
202
|
+
http://en.wikipedia.org/wiki/Command-query_separation
|
203
|
+
|
204
|
+
* Do not mutate arguments unless that is the purpose of the method.
|
205
|
+
|
206
|
+
* Try following TRUE heuristics by Sandi Metz
|
207
|
+
|
208
|
+
http://designisrefactoring.com/2015/02/08/introducing-sandi-metz-true/
|
209
|
+
|
210
|
+
* Do not mess around in core classes when writing libraries.
|
211
|
+
Namespace your code inside the modules, or wrap core classes to
|
212
|
+
decorators of your own.
|
213
|
+
|
214
|
+
* Do not program defensively.
|
215
|
+
|
216
|
+
http://www.erlang.se/doc/programming_rules.shtml#HDR11
|
217
|
+
|
218
|
+
* Keep the code simple.
|
219
|
+
|
220
|
+
* Don't overdesign.
|
221
|
+
|
222
|
+
* Don't underdesign.
|
223
|
+
|
224
|
+
* Avoid bugs.
|
225
|
+
|
226
|
+
* Read other style guides and apply the parts that don't dissent with
|
227
|
+
this list.
|
228
|
+
|
229
|
+
* Be consistent.
|
230
|
+
|
231
|
+
* Use common sense.
|
@@ -0,0 +1,14 @@
|
|
1
|
+
---
|
2
|
+
folders: # The list of folders to be used by any metric.
|
3
|
+
- lib
|
4
|
+
metrics: # The list of allowed metrics. The other metrics are disabled.
|
5
|
+
- cane
|
6
|
+
- churn
|
7
|
+
- flay
|
8
|
+
- flog
|
9
|
+
- reek
|
10
|
+
- roodi
|
11
|
+
- saikuro
|
12
|
+
format: html
|
13
|
+
output: tmp/metric_fu
|
14
|
+
verbose: false
|
@@ -0,0 +1 @@
|
|
1
|
+
---
|
@@ -0,0 +1,24 @@
|
|
1
|
+
---
|
2
|
+
AssignmentInConditionalCheck:
|
3
|
+
CaseMissingElseCheck:
|
4
|
+
ClassLineCountCheck:
|
5
|
+
line_count: 150
|
6
|
+
ClassNameCheck:
|
7
|
+
pattern: !ruby/regexp /^[A-Z][a-zA-Z0-9]*$/
|
8
|
+
ClassVariableCheck:
|
9
|
+
CyclomaticComplexityBlockCheck:
|
10
|
+
complexity: 2
|
11
|
+
CyclomaticComplexityMethodCheck:
|
12
|
+
complexity: 2
|
13
|
+
EmptyRescueBodyCheck:
|
14
|
+
ForLoopCheck:
|
15
|
+
MethodLineCountCheck:
|
16
|
+
line_count: 5
|
17
|
+
MethodNameCheck:
|
18
|
+
pattern: !ruby/regexp /^[\||\^|\&|\!]$|^[_a-z<>=\[|+-\/\*`]+[_a-z0-9_<>=~@\[\]]*[=!\?]?$/
|
19
|
+
ModuleLineCountCheck:
|
20
|
+
line_count: 150
|
21
|
+
ModuleNameCheck:
|
22
|
+
pattern: !ruby/regexp /^[A-Z][a-zA-Z0-9]*$/
|
23
|
+
ParameterNumberCheck:
|
24
|
+
parameter_count: 4
|