attr_coerced 0.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/.gitignore +9 -0
- data/.metrics +9 -0
- data/.rspec +2 -0
- data/.rubocop.yml +2 -0
- data/.travis.yml +9 -0
- data/.yardopts +3 -0
- data/Gemfile +5 -0
- data/Guardfile +15 -0
- data/LICENSE +21 -0
- data/README.md +142 -0
- data/Rakefile +22 -0
- data/attr_coerced.gemspec +23 -0
- data/config/metrics/STYLEGUIDE +230 -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 +15 -0
- data/config/metrics/reek.yml +1 -0
- data/config/metrics/roodi.yml +24 -0
- data/config/metrics/rubocop.yml +77 -0
- data/config/metrics/saikuro.yml +3 -0
- data/config/metrics/simplecov.yml +6 -0
- data/config/metrics/yardstick.yml +37 -0
- data/lib/attr_coerced/version.rb +9 -0
- data/lib/attr_coerced.rb +59 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/tests/attr_coerced_spec.rb +109 -0
- metadata +90 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c709d08d8b9359589eeb00ffcac0c7dac9b20b07
|
4
|
+
data.tar.gz: 709a619645efd382ad5f64da0653549c7e42b3e7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b96f6ca645ca651e93e7e1b0699da0b4d2cc4e4e53f6cae129c4619e3dadd220b33962b752ac8f08ebb6db39a468418f2ec129a54c170e979392969691ad4b19
|
7
|
+
data.tar.gz: 2bfa88ee0e7f23554c9f4c6a8b765a1858cec1641302d076738f50f73147cba34711cec14982f783dbecc5ac4a368e77b0c96a9bc598bdf17361d1ef6c5c095a
|
data/.coveralls.yml
ADDED
data/.gitignore
ADDED
data/.metrics
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
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(/^lib(.+)\.rb$/) do |m|
|
6
|
+
"spec/tests#{ m[1] }_spec.rb"
|
7
|
+
end
|
8
|
+
|
9
|
+
watch("spec/tests/*_spec.rb")
|
10
|
+
|
11
|
+
watch("lib/*.rb") { "spec" }
|
12
|
+
watch("spec/spec_helper.rb") { "spec" }
|
13
|
+
watch("spec/support/**/*.rb") { "spec" }
|
14
|
+
|
15
|
+
end # guard :rspec
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2015 Andrew Kozin (nepalez), andrew.kozin@gmail.com
|
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,142 @@
|
|
1
|
+
AttrCoerced
|
2
|
+
===========
|
3
|
+
|
4
|
+
[][gem]
|
5
|
+
[][travis]
|
6
|
+
[][gemnasium]
|
7
|
+
[][codeclimate]
|
8
|
+
[][coveralls]
|
9
|
+
[][inch]
|
10
|
+
|
11
|
+
[codeclimate]: https://codeclimate.com/github/nepalez/attr_coerced
|
12
|
+
[coveralls]: https://coveralls.io/r/nepalez/attr_coerced
|
13
|
+
[gem]: https://rubygems.org/gems/attr_coerced
|
14
|
+
[gemnasium]: https://gemnasium.com/nepalez/attr_coerced
|
15
|
+
[travis]: https://travis-ci.org/nepalez/attr_coerced
|
16
|
+
[inch]: https://inch-ci.org/github/nepalez/attr_coerced
|
17
|
+
|
18
|
+
Defines `attr_coerced` class helper to create coercible PORO attributes.
|
19
|
+
|
20
|
+
Usage
|
21
|
+
-----
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
class MyModel
|
25
|
+
extend AttrCoerced
|
26
|
+
|
27
|
+
# @!attribute name ("foo")
|
28
|
+
# @return [Name]
|
29
|
+
attr_coerced :name, Name, default: "foo"
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
This provides coerced attribute `name` (both getter and setter), defined in a following manner:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
class MyModel
|
37
|
+
|
38
|
+
# coerced getter
|
39
|
+
def name
|
40
|
+
Name.new(@name ||= "foo")
|
41
|
+
end
|
42
|
+
|
43
|
+
# coerced setter
|
44
|
+
def name=(value)
|
45
|
+
@name = Name.new(value)
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
You can use `attr_coerced_reader` and `attr_coerced_writer` to define only getter or setter. (Notice, the `attr_coerced_writer` doesn't use the `:default` option).
|
52
|
+
|
53
|
+
The type of the attribute can be defined somehow like:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
class Name < String
|
57
|
+
|
58
|
+
def self.new(value)
|
59
|
+
super unless value.blank? # returns nil except for ""
|
60
|
+
end
|
61
|
+
|
62
|
+
def initialize(value)
|
63
|
+
super value.to_s
|
64
|
+
end
|
65
|
+
|
66
|
+
# ... additional methods extending the string
|
67
|
+
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
71
|
+
Ensure the constructor to accept instances of its own - this is necessary for the getter.
|
72
|
+
|
73
|
+
Focus
|
74
|
+
-----
|
75
|
+
|
76
|
+
The module does nothing except for the getter and setter definitions.
|
77
|
+
|
78
|
+
You're free to define initializers for coerced attributes by youself. For example, you can use the module along with [Virtus], that exploits a different coersion mechanism, but provides many other useful features.
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
class MyModule
|
82
|
+
extend AttrCoerced
|
83
|
+
include Virtus.model
|
84
|
+
|
85
|
+
attribute :name # This adds :name to attributes, initializer etc.
|
86
|
+
attr_coerced :name, Name # This reloads getter and setter only
|
87
|
+
|
88
|
+
end
|
89
|
+
```
|
90
|
+
|
91
|
+
[Virtus]: https://github.com/solnic/virtus/
|
92
|
+
|
93
|
+
Installation
|
94
|
+
------------
|
95
|
+
|
96
|
+
Add this line to your application's Gemfile:
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
# Gemfile
|
100
|
+
gem "attr_coerced"
|
101
|
+
```
|
102
|
+
|
103
|
+
Then execute:
|
104
|
+
|
105
|
+
```
|
106
|
+
bundle
|
107
|
+
```
|
108
|
+
|
109
|
+
Or add it manually:
|
110
|
+
|
111
|
+
```
|
112
|
+
gem install attr_coerced
|
113
|
+
```
|
114
|
+
|
115
|
+
## Compatibility
|
116
|
+
|
117
|
+
Tested under rubies compatible to API 2.0+:
|
118
|
+
|
119
|
+
* MRI 2.0+
|
120
|
+
* Rubinius-2 (mode 2.0+)
|
121
|
+
* JRuby-9000 (mode 2.0+)
|
122
|
+
|
123
|
+
Uses [RSpec] 3.0+ for testing and [hexx-suit] for dev/test tools collection.
|
124
|
+
|
125
|
+
[RSpec]: http://rspec.info/
|
126
|
+
[hexx-suit]: https://github.com/nepalez/hexx-suit
|
127
|
+
|
128
|
+
## Contributing
|
129
|
+
|
130
|
+
* Fork the project.
|
131
|
+
* Read the [STYLEGUIDE](config/metrics/STYLEGUIDE).
|
132
|
+
* Make your feature addition or bug fix.
|
133
|
+
* Add tests for it. This is important so I don't break it in a
|
134
|
+
future version unintentionally.
|
135
|
+
* Commit, do not mess with Rakefile or version
|
136
|
+
(if you want to have your own version, that is fine but bump version
|
137
|
+
in a commit by itself I can ignore when I pull)
|
138
|
+
* Send me a pull request. Bonus points for topic branches.
|
139
|
+
|
140
|
+
## License
|
141
|
+
|
142
|
+
See the [MIT LICENSE](LICENSE).
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
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::RSpec and its tasks
|
13
|
+
begin
|
14
|
+
require "hexx-suit"
|
15
|
+
Hexx::Suit.install_tasks
|
16
|
+
rescue LoadError
|
17
|
+
require "hexx-rspec"
|
18
|
+
Hexx::RSpec.install_tasks
|
19
|
+
end
|
20
|
+
|
21
|
+
# Sets the Hexx::RSpec :test task to default
|
22
|
+
task default: "test:coverage:run"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
require "attr_coerced/version"
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
|
6
|
+
gem.name = "attr_coerced"
|
7
|
+
gem.version = AttrCoerced::VERSION.dup
|
8
|
+
gem.author = "Andrew Kozin"
|
9
|
+
gem.email = "andrew.kozin@gmail.com"
|
10
|
+
gem.homepage = "https://github.com/nepalez/attr_coerced"
|
11
|
+
gem.summary = "Coerced attributes for PORO models."
|
12
|
+
gem.description = gem.summary
|
13
|
+
gem.license = "MIT"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
16
|
+
gem.test_files = Dir["spec/**/*.rb"]
|
17
|
+
gem.extra_rdoc_files = Dir["README.md", "LICENSE"]
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.required_ruby_version = "~> 2.0"
|
21
|
+
gem.add_development_dependency "hexx-rspec", "~> 0.4"
|
22
|
+
|
23
|
+
end # Gem::Specification
|
@@ -0,0 +1,230 @@
|
|
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
|
+
- CHANGE - for backward-incompatible changes;
|
18
|
+
- BUG FIX - for fixing bugs;
|
19
|
+
- REFACTORING - for other changes of the code not affecting the API;
|
20
|
+
- OTHER - for changes in documentaton, metrics etc, not touching the code;
|
21
|
+
- VERSION - for version changes.
|
22
|
+
|
23
|
+
* Always separate commits of different types (such as FEATURE and CHANGE).
|
24
|
+
|
25
|
+
* Try to separate various features from each other.
|
26
|
+
|
27
|
+
* Include specification to the same commit as the code.
|
28
|
+
|
29
|
+
* Run all tests before making a commit.
|
30
|
+
Never commit the code that break unit tests.
|
31
|
+
|
32
|
+
* Use metric (run `rake check`) before making a commit.
|
33
|
+
|
34
|
+
* Do refactoring before making a commit. Best writing is rewriting.
|
35
|
+
|
36
|
+
* Follow semantic versioning.
|
37
|
+
|
38
|
+
http://semver.org/
|
39
|
+
|
40
|
+
* For versions name the commit after a version number, following the pattern:
|
41
|
+
|
42
|
+
VERSION 1.0.0-rc2
|
43
|
+
|
44
|
+
|
45
|
+
== Formatting:
|
46
|
+
|
47
|
+
* Use UTF-8. Declare encoding in the first line of every file.
|
48
|
+
|
49
|
+
# encoding: utf-8
|
50
|
+
|
51
|
+
* Use 2 space indent, no tabs.
|
52
|
+
|
53
|
+
* Use Unix-style line endings.
|
54
|
+
|
55
|
+
* Use spaces around operators, after commas, colons and semicolons,
|
56
|
+
around { and before }.
|
57
|
+
|
58
|
+
* No spaces after (, [ and before ], ).
|
59
|
+
|
60
|
+
* Align `when` and `else` with `case`.
|
61
|
+
|
62
|
+
* Use an empty line before the return value of a method (unless it
|
63
|
+
only has one line), and an empty line between defs.
|
64
|
+
|
65
|
+
* Use empty lines to break up a long method into logical paragraphs.
|
66
|
+
|
67
|
+
* Keep lines fewer than 80 characters.
|
68
|
+
|
69
|
+
* Strip trailing whitespace.
|
70
|
+
|
71
|
+
|
72
|
+
== Syntax:
|
73
|
+
|
74
|
+
* Write for 2.0.
|
75
|
+
|
76
|
+
* Use double quotes
|
77
|
+
|
78
|
+
http://viget.com/extend/just-use-double-quoted-ruby-strings
|
79
|
+
|
80
|
+
* Use def with parentheses when there are arguments.
|
81
|
+
|
82
|
+
* Never use for, unless you exactly know why.
|
83
|
+
|
84
|
+
* Never use then, except in case statements.
|
85
|
+
|
86
|
+
* Use when x then ... for one-line cases.
|
87
|
+
|
88
|
+
* Use &&/|| for boolean expressions, and/or for control flow. (Rule
|
89
|
+
of thumb: If you have to use outer parentheses, you are using the
|
90
|
+
wrong operators.)
|
91
|
+
|
92
|
+
* Avoid double negation (!!), unless Null Objects are expected.
|
93
|
+
|
94
|
+
http://devblog.avdi.org/2011/05/30/null-objects-and-falsiness
|
95
|
+
|
96
|
+
* Avoid multiline ?:, use if.
|
97
|
+
|
98
|
+
* Use {...} when defining blocks on one line. Use do...end for multiline
|
99
|
+
blocks.
|
100
|
+
|
101
|
+
* Avoid return where not required.
|
102
|
+
|
103
|
+
* Use ||= freely.
|
104
|
+
|
105
|
+
* Use OO regexps, and avoid =~ $0-9, $~, $` and $' when possible.
|
106
|
+
|
107
|
+
* Do not use Enumerable#inject when the "memo" object does not change between
|
108
|
+
iterations, use Enumerable#each_with_object instead (in ruby 1.9,
|
109
|
+
active_support and backports).
|
110
|
+
|
111
|
+
* Prefer ENV.fetch to ENV[] syntax.
|
112
|
+
Prefer block syntax for ENV.fetch to usage of the second argument.
|
113
|
+
|
114
|
+
|
115
|
+
== Naming:
|
116
|
+
|
117
|
+
* Use snake_case for methods.
|
118
|
+
|
119
|
+
* Use CamelCase for classes and modules. (Keep acronyms like HTTP,
|
120
|
+
RFC, XML uppercase.)
|
121
|
+
|
122
|
+
* Use SCREAMING_SNAKE_CASE for other constants.
|
123
|
+
|
124
|
+
* Do not use single letter variable names. Avoid uncommunicative names.
|
125
|
+
|
126
|
+
* Use consistent variable names. Try to keep the variable names close
|
127
|
+
to the object class name.
|
128
|
+
|
129
|
+
* Use names prefixed with _ for unused variables.
|
130
|
+
|
131
|
+
* When defining a predicate method that compares against another object of
|
132
|
+
a similar type, name the argument "other".
|
133
|
+
|
134
|
+
* Prefer map over collect, detect over find, select over find_all.
|
135
|
+
|
136
|
+
* Use def self.method to define singleton methods.
|
137
|
+
|
138
|
+
* Avoid alias when alias_method will do.
|
139
|
+
|
140
|
+
|
141
|
+
== Comments:
|
142
|
+
|
143
|
+
* Use YARD and its conventions for API documentation. Don't put an
|
144
|
+
empty line between the comment block and the def.
|
145
|
+
|
146
|
+
* Comments longer than a word are capitalized and use punctuation.
|
147
|
+
Use one space after periods.
|
148
|
+
|
149
|
+
* Avoid superfluous comments.
|
150
|
+
|
151
|
+
|
152
|
+
== Code structuring:
|
153
|
+
|
154
|
+
* Break code into packages, decoupled from the environment.
|
155
|
+
|
156
|
+
* Wrap packages into gems.
|
157
|
+
|
158
|
+
* Inject dependencies explicitly.
|
159
|
+
Leave all outer references on the border of any package. Inside
|
160
|
+
the package use internal references only.
|
161
|
+
|
162
|
+
* Follow SOLID principles.
|
163
|
+
|
164
|
+
http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)
|
165
|
+
|
166
|
+
* Only give a method one purpose for existing. If you pass in a boolean
|
167
|
+
to a method, what you're saying is that this method has two different
|
168
|
+
behaviours. Just split it into two single purpose methods. If you have
|
169
|
+
to use the words "AND" or "OR" to describe what the method does it
|
170
|
+
probably does too much.
|
171
|
+
|
172
|
+
* Avoid long methods.
|
173
|
+
Try to keep them at no more than 6 lines long, and preferably 4 or less.
|
174
|
+
|
175
|
+
If sections of a method are logically separate by blank lines, then
|
176
|
+
that's probably a sign that those sections should be split into separate
|
177
|
+
methods.
|
178
|
+
|
179
|
+
* Avoid hashes-as-optional-parameters. Does the method do too much?
|
180
|
+
|
181
|
+
* Avoid long parameter lists.
|
182
|
+
|
183
|
+
* Add "global" methods to Kernel (if you have to) and make them private.
|
184
|
+
|
185
|
+
* Use OptionParser for parsing complex command line options and
|
186
|
+
ruby -s for trivial command line options.
|
187
|
+
|
188
|
+
* Avoid needless metaprogramming.
|
189
|
+
|
190
|
+
* Always freeze objects assigned to constants.
|
191
|
+
|
192
|
+
|
193
|
+
== General:
|
194
|
+
|
195
|
+
* Code in a functional way, avoid mutation when it makes sense.
|
196
|
+
|
197
|
+
* Try to have methods either return the state of the object and have
|
198
|
+
no side effects, or return self and have side effects. This is
|
199
|
+
otherwise known as Command-query separation (CQS):
|
200
|
+
|
201
|
+
http://en.wikipedia.org/wiki/Command-query_separation
|
202
|
+
|
203
|
+
* Do not mutate arguments unless that is the purpose of the method.
|
204
|
+
|
205
|
+
* Try following TRUE heuristics by Sandi Metz
|
206
|
+
|
207
|
+
http://designisrefactoring.com/2015/02/08/introducing-sandi-metz-true/
|
208
|
+
|
209
|
+
* Do not mess around in core classes when writing libraries.
|
210
|
+
Namespace your code inside the modules, or wrap core classes to
|
211
|
+
decorators of your own.
|
212
|
+
|
213
|
+
* Do not program defensively.
|
214
|
+
|
215
|
+
http://www.erlang.se/doc/programming_rules.shtml#HDR11
|
216
|
+
|
217
|
+
* Keep the code simple.
|
218
|
+
|
219
|
+
* Don't overdesign.
|
220
|
+
|
221
|
+
* Don't underdesign.
|
222
|
+
|
223
|
+
* Avoid bugs.
|
224
|
+
|
225
|
+
* Read other style guides and apply the parts that don't dissent with
|
226
|
+
this list.
|
227
|
+
|
228
|
+
* Be consistent.
|
229
|
+
|
230
|
+
* Use common sense.
|
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
folders: # The list of folders to be used by any metric.
|
3
|
+
- lib
|
4
|
+
- app
|
5
|
+
metrics: # The list of allowed metrics. The other metrics are disabled.
|
6
|
+
- cane
|
7
|
+
- churn
|
8
|
+
- flay
|
9
|
+
- flog
|
10
|
+
- reek
|
11
|
+
- roodi
|
12
|
+
- saikuro
|
13
|
+
format: html
|
14
|
+
output: tmp/metric_fu
|
15
|
+
verbose: false
|
@@ -0,0 +1 @@
|
|
1
|
+
---
|
@@ -0,0 +1,24 @@
|
|
1
|
+
---
|
2
|
+
AssignmentInConditionalCheck:
|
3
|
+
CaseMissingElseCheck:
|
4
|
+
ClassLineCountCheck:
|
5
|
+
line_count: 100
|
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: 20
|
17
|
+
MethodNameCheck:
|
18
|
+
pattern: !ruby/regexp /^[\||\^|\&|\!]$|^[_a-z<>=\[|+-\/\*`]+[_a-z0-9_<>=~@\[\]]*[=!\?]?$/
|
19
|
+
ModuleLineCountCheck:
|
20
|
+
line_count: 100
|
21
|
+
ModuleNameCheck:
|
22
|
+
pattern: !ruby/regexp /^[A-Z][a-zA-Z0-9]*$/
|
23
|
+
ParameterNumberCheck:
|
24
|
+
parameter_count: 4
|
@@ -0,0 +1,77 @@
|
|
1
|
+
---
|
2
|
+
# settings added by the 'hexx-suit' module
|
3
|
+
# output: "tmp/rubocop"
|
4
|
+
# format: "html"
|
5
|
+
|
6
|
+
AllCops:
|
7
|
+
Exclude:
|
8
|
+
- '**/db/schema.rb'
|
9
|
+
|
10
|
+
Lint/HandleExceptions:
|
11
|
+
Exclude:
|
12
|
+
- '**/*_spec.rb'
|
13
|
+
|
14
|
+
Lint/RescueException:
|
15
|
+
Exclude:
|
16
|
+
- '**/*_spec.rb'
|
17
|
+
|
18
|
+
Lint/UnusedBlockArgument:
|
19
|
+
Exclude:
|
20
|
+
- '**/attr_coerced.rb'
|
21
|
+
|
22
|
+
Style/AccessorMethodName:
|
23
|
+
Exclude:
|
24
|
+
- '**/*_spec.rb'
|
25
|
+
|
26
|
+
Style/AsciiComments:
|
27
|
+
Enabled: false
|
28
|
+
|
29
|
+
Style/ClassAndModuleChildren:
|
30
|
+
Exclude:
|
31
|
+
- '**/*_spec.rb'
|
32
|
+
|
33
|
+
Style/Documentation:
|
34
|
+
Enabled: false
|
35
|
+
|
36
|
+
Style/EmptyLinesAroundBlockBody:
|
37
|
+
Enabled: false
|
38
|
+
|
39
|
+
Style/EmptyLinesAroundClassBody:
|
40
|
+
Enabled: false
|
41
|
+
|
42
|
+
Style/EmptyLinesAroundMethodBody:
|
43
|
+
Enabled: false
|
44
|
+
|
45
|
+
Style/EmptyLinesAroundModuleBody:
|
46
|
+
Enabled: false
|
47
|
+
|
48
|
+
Style/EmptyLineBetweenDefs:
|
49
|
+
Enabled: false
|
50
|
+
|
51
|
+
Style/FileName:
|
52
|
+
Enabled: false
|
53
|
+
|
54
|
+
Style/RaiseArgs:
|
55
|
+
EnforcedStyle: compact
|
56
|
+
|
57
|
+
Style/SingleLineMethods:
|
58
|
+
Exclude:
|
59
|
+
- '**/*_spec.rb'
|
60
|
+
|
61
|
+
Style/SingleSpaceBeforeFirstArg:
|
62
|
+
Enabled: false
|
63
|
+
|
64
|
+
Style/SpecialGlobalVars:
|
65
|
+
Exclude:
|
66
|
+
- '**/Gemfile'
|
67
|
+
- '**/*.gemspec'
|
68
|
+
|
69
|
+
Style/StringLiterals:
|
70
|
+
EnforcedStyle: double_quotes
|
71
|
+
|
72
|
+
Style/StringLiteralsInInterpolation:
|
73
|
+
EnforcedStyle: double_quotes
|
74
|
+
|
75
|
+
Style/TrivialAccessors:
|
76
|
+
Exclude:
|
77
|
+
- '**/*_spec.rb'
|
@@ -0,0 +1,37 @@
|
|
1
|
+
---
|
2
|
+
# Settings added by the 'hexx-suit' gem
|
3
|
+
output: "tmp/yardstick/output.log"
|
4
|
+
path: "lib/**/*.rb"
|
5
|
+
rules:
|
6
|
+
ApiTag::Presence:
|
7
|
+
enabled: true
|
8
|
+
exclude: []
|
9
|
+
ApiTag::Inclusion:
|
10
|
+
enabled: true
|
11
|
+
exclude: []
|
12
|
+
ApiTag::ProtectedMethod:
|
13
|
+
enabled: true
|
14
|
+
exclude: []
|
15
|
+
ApiTag::PrivateMethod:
|
16
|
+
enabled: false
|
17
|
+
exclude: []
|
18
|
+
ExampleTag:
|
19
|
+
enabled: true
|
20
|
+
exclude: []
|
21
|
+
ReturnTag:
|
22
|
+
enabled: true
|
23
|
+
exclude: []
|
24
|
+
Summary::Presence:
|
25
|
+
enabled: true
|
26
|
+
exclude: []
|
27
|
+
Summary::Length:
|
28
|
+
enabled: true
|
29
|
+
exclude: []
|
30
|
+
Summary::Delimiter:
|
31
|
+
enabled: true
|
32
|
+
exclude: []
|
33
|
+
Summary::SingleLine:
|
34
|
+
enabled: true
|
35
|
+
exclude: []
|
36
|
+
threshold: 100
|
37
|
+
verbose: false
|
data/lib/attr_coerced.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require_relative "attr_coerced/version"
|
4
|
+
|
5
|
+
# Namespace for the code of the 'attr_coerced' gem
|
6
|
+
module AttrCoerced
|
7
|
+
|
8
|
+
# Defines both value getter and setter for a coercible attribute
|
9
|
+
#
|
10
|
+
# @example
|
11
|
+
# attr_coerced :name, Name, default: "foo"
|
12
|
+
#
|
13
|
+
# @param [#to_sym] name
|
14
|
+
# The name of the attribute
|
15
|
+
# @param [Class] type
|
16
|
+
# The type of the attribute
|
17
|
+
# @option [Object] :default (nil)
|
18
|
+
# The default value for the attribute. The value will be coerced as well
|
19
|
+
#
|
20
|
+
# @return [Symbol] name of the attribute
|
21
|
+
def attr_coerced(name, type, default: nil)
|
22
|
+
[
|
23
|
+
attr_coerced_reader(name, type, default: default),
|
24
|
+
attr_coerced_writer(name, type)
|
25
|
+
]
|
26
|
+
end
|
27
|
+
|
28
|
+
# Defines value getter for a coercible attribute
|
29
|
+
#
|
30
|
+
# @example
|
31
|
+
# attr_coerced_reader :name, Name, default: "foo"
|
32
|
+
#
|
33
|
+
# @param (see #attr_coerced)
|
34
|
+
# @option (see #attr_coerced)
|
35
|
+
#
|
36
|
+
# @return (see #attr_coerced)
|
37
|
+
def attr_coerced_reader(name, type, default: nil)
|
38
|
+
define_method(name) { type.new(instance_eval("@#{ name }") || default) }
|
39
|
+
|
40
|
+
name.to_sym
|
41
|
+
end
|
42
|
+
|
43
|
+
# Defines value setter for a coercible attribute
|
44
|
+
#
|
45
|
+
# @example
|
46
|
+
# attr_coerced_writer :name, Name
|
47
|
+
#
|
48
|
+
# @param (see #attr_coerced)
|
49
|
+
#
|
50
|
+
# @return (see #attr_coerced)
|
51
|
+
def attr_coerced_writer(name, type)
|
52
|
+
define_method "#{ name }=" do |value|
|
53
|
+
instance_eval "@#{ name } = #{ type }.new(value)"
|
54
|
+
end
|
55
|
+
|
56
|
+
"#{ name }=".to_sym
|
57
|
+
end
|
58
|
+
|
59
|
+
end # module AttrCoerced
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
describe AttrCoerced do
|
4
|
+
|
5
|
+
before do
|
6
|
+
class Foo < String
|
7
|
+
def self.new(value)
|
8
|
+
super(value.to_s) unless value.empty?
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
after { Object.send :remove_const, :Foo }
|
13
|
+
|
14
|
+
let(:test_class) { Class.new.send :extend, described_class }
|
15
|
+
subject { test_class.new }
|
16
|
+
|
17
|
+
describe "#attr_coerced_reader" do
|
18
|
+
|
19
|
+
let!(:result) { test_class.attr_coerced_reader :foo, Foo, default: "bar" }
|
20
|
+
|
21
|
+
it "defines the getter" do
|
22
|
+
expect(subject).to respond_to :foo
|
23
|
+
end
|
24
|
+
|
25
|
+
it "makes the getter to return the default value" do
|
26
|
+
value = subject.foo
|
27
|
+
|
28
|
+
expect(value).to be_kind_of Foo
|
29
|
+
expect(value).to eq "bar"
|
30
|
+
end
|
31
|
+
|
32
|
+
it "makes the getter to coerce the value" do
|
33
|
+
subject.instance_eval "@foo = \"baz\""
|
34
|
+
value = subject.foo
|
35
|
+
|
36
|
+
expect(value).to be_kind_of Foo
|
37
|
+
expect(value).to eq "baz"
|
38
|
+
end
|
39
|
+
|
40
|
+
it "returns the name of the attribute" do
|
41
|
+
expect(result).to eq :foo
|
42
|
+
end
|
43
|
+
|
44
|
+
end # describe #attr_coerced_reader
|
45
|
+
|
46
|
+
describe "#attr_coerced_writer" do
|
47
|
+
|
48
|
+
let!(:result) { test_class.attr_coerced_writer :foo, Foo }
|
49
|
+
|
50
|
+
it "defines the setter" do
|
51
|
+
expect(subject).to respond_to :foo=
|
52
|
+
end
|
53
|
+
|
54
|
+
it "makes the setter to coerce the value" do
|
55
|
+
subject.foo = "baz"
|
56
|
+
value = subject.instance_eval "@foo"
|
57
|
+
|
58
|
+
expect(value).to be_kind_of Foo
|
59
|
+
expect(value).to eq "baz"
|
60
|
+
end
|
61
|
+
|
62
|
+
it "returns the name of the attribute" do
|
63
|
+
expect(result).to eq :foo=
|
64
|
+
end
|
65
|
+
|
66
|
+
end # describe #attr_coerced_writer
|
67
|
+
|
68
|
+
describe "#attr_coerced" do
|
69
|
+
|
70
|
+
let!(:result) { test_class.attr_coerced :foo, Foo, default: "bar" }
|
71
|
+
|
72
|
+
it "defines the getter" do
|
73
|
+
expect(subject).to respond_to :foo
|
74
|
+
end
|
75
|
+
|
76
|
+
it "makes the getter to return the default value" do
|
77
|
+
value = subject.foo
|
78
|
+
|
79
|
+
expect(value).to be_kind_of Foo
|
80
|
+
expect(value).to eq "bar"
|
81
|
+
end
|
82
|
+
|
83
|
+
it "makes the getter to coerce the value" do
|
84
|
+
subject.instance_eval "@foo = \"baz\""
|
85
|
+
value = subject.foo
|
86
|
+
|
87
|
+
expect(value).to be_kind_of Foo
|
88
|
+
expect(value).to eq "baz"
|
89
|
+
end
|
90
|
+
|
91
|
+
it "defines the setter" do
|
92
|
+
expect(subject).to respond_to :foo=
|
93
|
+
end
|
94
|
+
|
95
|
+
it "makes the setter to coerce the value" do
|
96
|
+
subject.foo = "baz"
|
97
|
+
value = subject.instance_eval "@foo"
|
98
|
+
|
99
|
+
expect(value).to be_kind_of Foo
|
100
|
+
expect(value).to eq "baz"
|
101
|
+
end
|
102
|
+
|
103
|
+
it "returns the names of the methods" do
|
104
|
+
expect(result).to eq [:foo, :foo=]
|
105
|
+
end
|
106
|
+
|
107
|
+
end # describe #attr_coerced
|
108
|
+
|
109
|
+
end # describe AttrCoerced
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: attr_coerced
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Kozin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: hexx-rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.4'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.4'
|
27
|
+
description: Coerced attributes for PORO models.
|
28
|
+
email: andrew.kozin@gmail.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files:
|
32
|
+
- README.md
|
33
|
+
- LICENSE
|
34
|
+
files:
|
35
|
+
- .coveralls.yml
|
36
|
+
- .gitignore
|
37
|
+
- .metrics
|
38
|
+
- .rspec
|
39
|
+
- .rubocop.yml
|
40
|
+
- .travis.yml
|
41
|
+
- .yardopts
|
42
|
+
- Gemfile
|
43
|
+
- Guardfile
|
44
|
+
- LICENSE
|
45
|
+
- README.md
|
46
|
+
- Rakefile
|
47
|
+
- attr_coerced.gemspec
|
48
|
+
- config/metrics/STYLEGUIDE
|
49
|
+
- config/metrics/cane.yml
|
50
|
+
- config/metrics/churn.yml
|
51
|
+
- config/metrics/flay.yml
|
52
|
+
- config/metrics/metric_fu.yml
|
53
|
+
- config/metrics/reek.yml
|
54
|
+
- config/metrics/roodi.yml
|
55
|
+
- config/metrics/rubocop.yml
|
56
|
+
- config/metrics/saikuro.yml
|
57
|
+
- config/metrics/simplecov.yml
|
58
|
+
- config/metrics/yardstick.yml
|
59
|
+
- lib/attr_coerced.rb
|
60
|
+
- lib/attr_coerced/version.rb
|
61
|
+
- spec/spec_helper.rb
|
62
|
+
- spec/tests/attr_coerced_spec.rb
|
63
|
+
homepage: https://github.com/nepalez/attr_coerced
|
64
|
+
licenses:
|
65
|
+
- MIT
|
66
|
+
metadata: {}
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '2.0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 2.4.6
|
84
|
+
signing_key:
|
85
|
+
specification_version: 4
|
86
|
+
summary: Coerced attributes for PORO models.
|
87
|
+
test_files:
|
88
|
+
- spec/spec_helper.rb
|
89
|
+
- spec/tests/attr_coerced_spec.rb
|
90
|
+
has_rdoc:
|