eigindir 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/.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 +7 -0
- data/Guardfile +14 -0
- data/LICENSE +21 -0
- data/README.md +342 -0
- data/Rakefile +22 -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 +72 -0
- data/config/metrics/saikuro.yml +3 -0
- data/config/metrics/simplecov.yml +6 -0
- data/config/metrics/yardstick.yml +37 -0
- data/eigindir.gemspec +22 -0
- data/lib/eigindir.rb +63 -0
- data/lib/eigindir/api.rb +79 -0
- data/lib/eigindir/coercer.rb +70 -0
- data/lib/eigindir/patches.rb +39 -0
- data/lib/eigindir/version.rb +9 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/tests/eigindir/api_spec.rb +395 -0
- data/spec/tests/eigindir/patches_spec.rb +34 -0
- data/spec/tests/eigindir_spec.rb +86 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a04b87646bc1711b3906a320357ff5c3a0ac6329
|
4
|
+
data.tar.gz: e1e65a6867902d0a8b6ad1a6a1c80bd53a1e9db9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0a815b9c785c79e92b18add69df7bccd7f323391daf508b0e5ffdb24678256e1fb16b3ca426b2e289713660f41874bee71f88f788137f0a4da04ea7d2c0f549a
|
7
|
+
data.tar.gz: b519e53fcab8f5375bd9f508400af912b63ba5f534cd838ef67c49c258cb73e06ebea0a2ee4e59ba4e6e1aaa82db4bb718b0f144303f7356cc6e18d68700f0a5
|
data/.coveralls.yml
ADDED
data/.gitignore
ADDED
data/.metrics
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
# Settings for metric_fu and its packages are collected in the `config/metrics`
|
2
|
+
# and loaded by the Hexx::Suit::Metrics::MetricFu.
|
3
|
+
|
4
|
+
begin
|
5
|
+
require "hexx-suit"
|
6
|
+
Hexx::Suit::Metrics::MetricFu.load
|
7
|
+
rescue LoadError
|
8
|
+
puts "The 'hexx-suit' gem is not installed"
|
9
|
+
end
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/.yardopts
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
guard :rspec, cmd: "bundle exec rspec" do
|
4
|
+
|
5
|
+
watch(%r{^spec/tests/.+_spec\.rb$})
|
6
|
+
|
7
|
+
watch(%r{^lib/(.+)\.rb$}) do |m|
|
8
|
+
"spec/tests/#{ m[1] }_spec.rb"
|
9
|
+
end
|
10
|
+
|
11
|
+
watch(%r{^lib/\w+\.rb$}) { "spec" }
|
12
|
+
watch("spec/spec_helper.rb") { "spec" }
|
13
|
+
|
14
|
+
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,342 @@
|
|
1
|
+
Eigindir
|
2
|
+
========
|
3
|
+
|
4
|
+
[![Gem Version](https://img.shields.io/gem/v/eigindir.svg?style=flat)][gem]
|
5
|
+
[![Build Status](https://img.shields.io/travis/nepalez/eigindir/master.svg?style=flat)][travis]
|
6
|
+
[![Dependency Status](https://img.shields.io/gemnasium/nepalez/eigindir.svg?style=flat)][gemnasium]
|
7
|
+
[![Code Climate](https://img.shields.io/codeclimate/github/nepalez/eigindir.svg?style=flat)][codeclimate]
|
8
|
+
[![Coverage](https://img.shields.io/coveralls/nepalez/eigindir.svg?style=flat)][coveralls]
|
9
|
+
[![Inline docs](http://inch-ci.org/github/nepalez/eigindir.svg)][inch]
|
10
|
+
|
11
|
+
[codeclimate]: https://codeclimate.com/github/nepalez/eigindir
|
12
|
+
[coveralls]: https://coveralls.io/r/nepalez/eigindir
|
13
|
+
[gem]: https://rubygems.org/gems/eigindir
|
14
|
+
[gemnasium]: https://gemnasium.com/nepalez/eigindir
|
15
|
+
[travis]: https://travis-ci.org/nepalez/eigindir
|
16
|
+
[inch]: https://inch-ci.org/github/nepalez/eigindir
|
17
|
+
|
18
|
+
Coercible PORO attributes.
|
19
|
+
|
20
|
+
Synopsis
|
21
|
+
--------
|
22
|
+
|
23
|
+
This is a tiny [virtus]-inspired library
|
24
|
+
for PORO attributes' definition and coersion.
|
25
|
+
|
26
|
+
Like [virtus] it declares attributes, and coerces their values.
|
27
|
+
Unlike [virtus] it uses different coersion mechanism, and does nothing else.
|
28
|
+
|
29
|
+
[virtus]: https://github.com/solnic/virtus
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
class Foo
|
33
|
+
include Eigindir
|
34
|
+
|
35
|
+
attribute(
|
36
|
+
:bar,
|
37
|
+
writer: ->(v) { v.to_i + 1 },
|
38
|
+
reader: ->(v) { v.to_s }
|
39
|
+
)
|
40
|
+
end
|
41
|
+
|
42
|
+
foo = Foo.new
|
43
|
+
|
44
|
+
# the setter coerced given value:
|
45
|
+
foo.bar = "10"
|
46
|
+
foo.instance_eval "@bar" # => 11
|
47
|
+
# the getter coerced stored value:
|
48
|
+
foo.instance_eval # => "11"
|
49
|
+
|
50
|
+
# attributes getter and setter are defined as well:
|
51
|
+
foo.attributes = { "bar" => 5 }
|
52
|
+
foo.attributes # => { bar: "6" }
|
53
|
+
```
|
54
|
+
|
55
|
+
The module doesn't define the initializer.
|
56
|
+
You're free to define in a following way:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
class Foo
|
60
|
+
include Eigindir
|
61
|
+
|
62
|
+
def initializer(**options)
|
63
|
+
self.attributes = options
|
64
|
+
end
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
Details
|
69
|
+
-------
|
70
|
+
|
71
|
+
### Base Use
|
72
|
+
|
73
|
+
`attribute`, `attribute_reader` and `attribute_writer` are like
|
74
|
+
`attr_accessor`, `attr_reader` and `attr_writer`:
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
class Foo
|
78
|
+
include Eigindir
|
79
|
+
|
80
|
+
attribute :bar
|
81
|
+
attribute_reader :baz
|
82
|
+
attribute_writer :qux
|
83
|
+
end
|
84
|
+
|
85
|
+
foo = Foo.new
|
86
|
+
|
87
|
+
foo.bar = 1
|
88
|
+
foo.bar # => 1
|
89
|
+
|
90
|
+
foo.baz = 1 # <NoMethodError>
|
91
|
+
foo.baz # => nil
|
92
|
+
|
93
|
+
foo.qux = 1
|
94
|
+
foo.instance_eval "@qux" # => 1
|
95
|
+
foo.qux # <NoMethodError>
|
96
|
+
```
|
97
|
+
|
98
|
+
### Attributes Declaration
|
99
|
+
|
100
|
+
The `attributes` and `attributes=` ignore all keys not declared as `attribute`.
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
class Foo
|
104
|
+
include Eigindir
|
105
|
+
|
106
|
+
attribute :bar
|
107
|
+
attr_accessor :baz
|
108
|
+
end
|
109
|
+
|
110
|
+
foo = Foo.new
|
111
|
+
foo.attributes = { bar: 1, baz: 2 }
|
112
|
+
foo.bar # => 1
|
113
|
+
foo.baz # => nil
|
114
|
+
|
115
|
+
foo.baz = 2
|
116
|
+
foo.baz # => 2
|
117
|
+
foo.attributes # => { bar: 1 }
|
118
|
+
```
|
119
|
+
|
120
|
+
The methods ignore attributes that has no getters or setters correspondingly:
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
class Foo
|
124
|
+
include Eigindir
|
125
|
+
|
126
|
+
attribute_reader :bar
|
127
|
+
attribute_writer :baz
|
128
|
+
end
|
129
|
+
|
130
|
+
foo = Foo.new
|
131
|
+
foo.attributes = { bar: 1, baz: 1 }
|
132
|
+
|
133
|
+
# baz has no getter,
|
134
|
+
# bar value is not assigned because it has no setter:
|
135
|
+
foo.attributes = { bar: nil }
|
136
|
+
```
|
137
|
+
|
138
|
+
The `attributes=` accepts hash with any stringified keys and symbolizes them:
|
139
|
+
|
140
|
+
```ruby
|
141
|
+
class Foo
|
142
|
+
include Eigindir
|
143
|
+
|
144
|
+
attribute :bar
|
145
|
+
attribute :baz
|
146
|
+
end
|
147
|
+
|
148
|
+
foo = Foo.new
|
149
|
+
foo.attributes = { bar: "1", "bar" => 2 }
|
150
|
+
# The setter symbolizes keys to :bar and uses last value:
|
151
|
+
foo.attributes # => { bar: 2 }
|
152
|
+
```
|
153
|
+
|
154
|
+
### Base Coersion
|
155
|
+
|
156
|
+
All class methods: `attribute`, `attribute_reader` and `attribute_writer`
|
157
|
+
accepts the `:coerce` key:
|
158
|
+
|
159
|
+
```ruby
|
160
|
+
class Foo
|
161
|
+
include Eigindir
|
162
|
+
|
163
|
+
attribute :bar, coerce: ->(val) { val.to_i + 1 }
|
164
|
+
end
|
165
|
+
```
|
166
|
+
|
167
|
+
The setter coerces given value:
|
168
|
+
|
169
|
+
```ruby
|
170
|
+
foo = Foo.new
|
171
|
+
|
172
|
+
foo.bar = "1"
|
173
|
+
foo.instance_eval "@bar" # => 2
|
174
|
+
```
|
175
|
+
|
176
|
+
The getter coerces stored variable:
|
177
|
+
|
178
|
+
```ruby
|
179
|
+
foo.instance_eval "@bar" # => 2
|
180
|
+
foo.bar # => 3
|
181
|
+
```
|
182
|
+
|
183
|
+
A coercer can be set either as proc or lambda, or by instance method name.
|
184
|
+
The following declarations are equivalent:
|
185
|
+
|
186
|
+
```ruby
|
187
|
+
class Foo
|
188
|
+
include Eigindir
|
189
|
+
|
190
|
+
attribute :bar, coerce: ->(value) { value.to_s }
|
191
|
+
attribute :bar, coerce: lambda { |value| value.to_s }
|
192
|
+
attribute :bar, coerce: proc { |value| value.to_s }
|
193
|
+
attribute :bar, coerce: :coercer
|
194
|
+
attribute :bar, coerce: "coercer"
|
195
|
+
|
196
|
+
private
|
197
|
+
|
198
|
+
def coercer(value)
|
199
|
+
value.to_s
|
200
|
+
end
|
201
|
+
end
|
202
|
+
```
|
203
|
+
|
204
|
+
### Separate Coersion
|
205
|
+
|
206
|
+
The `attribute` class method also takes `:reader` and `:writer`.
|
207
|
+
Every option sets its own coersion for the corresponding direction:
|
208
|
+
|
209
|
+
```ruby
|
210
|
+
class Foo
|
211
|
+
include Eigindir
|
212
|
+
|
213
|
+
attribute(
|
214
|
+
:bar,
|
215
|
+
writer: ->(val) { val.to_i + 1 }
|
216
|
+
reader: ->(val) { val.to_s }
|
217
|
+
)
|
218
|
+
end
|
219
|
+
|
220
|
+
foo = Foo.new
|
221
|
+
|
222
|
+
# The setter uses the :writer coercer:
|
223
|
+
foo.bar = "10"
|
224
|
+
foo.instance_eval "@bar" # => 11
|
225
|
+
|
226
|
+
# The getter uses the :reader coercer:
|
227
|
+
foo.bar # => "11"
|
228
|
+
```
|
229
|
+
|
230
|
+
### Coersion of nil
|
231
|
+
|
232
|
+
Because +nil+ stands for pure nothing, it is not coerced by default:
|
233
|
+
|
234
|
+
```ruby
|
235
|
+
class Foo
|
236
|
+
include Eigindir
|
237
|
+
|
238
|
+
attribute :bar, coerce: &:to_s
|
239
|
+
end
|
240
|
+
|
241
|
+
foo = Foo.new
|
242
|
+
foo.bar = nil
|
243
|
+
foo.instance_eval "@bar" # => nil
|
244
|
+
foo.bar # => nil
|
245
|
+
```
|
246
|
+
|
247
|
+
Use `strict: true` option to coerce `nil` as well:
|
248
|
+
|
249
|
+
```ruby
|
250
|
+
class Foo
|
251
|
+
include Eigindir
|
252
|
+
|
253
|
+
attribute :baz, coerce: &:to_s, strict: true
|
254
|
+
end
|
255
|
+
|
256
|
+
foo = Foo.new
|
257
|
+
foo.bar = nil
|
258
|
+
foo.instance_eval "@bar" # => ""
|
259
|
+
foo.bar # => ""
|
260
|
+
```
|
261
|
+
|
262
|
+
This can be used to set default values explicitly either by getter or setter:
|
263
|
+
|
264
|
+
```ruby
|
265
|
+
class Foo
|
266
|
+
include Eigindir
|
267
|
+
|
268
|
+
attribute :bar, reader: proc { |val| val || 1 }, strict: true
|
269
|
+
attribute :baz, writer: proc { |val| val || 1 }, strict: true
|
270
|
+
end
|
271
|
+
|
272
|
+
foo = Foo.new
|
273
|
+
|
274
|
+
# The getter's default is pretty virtual:
|
275
|
+
foo.bar = nil
|
276
|
+
foo.instance_eval "@bar" # => nil
|
277
|
+
foo.bar # => 1
|
278
|
+
|
279
|
+
# While the setter's is persistent:
|
280
|
+
foo.baz = nil
|
281
|
+
foo.instance_eval "@baz" # => 1
|
282
|
+
foo.baz # => 1
|
283
|
+
```
|
284
|
+
|
285
|
+
Credits
|
286
|
+
-------
|
287
|
+
|
288
|
+
Many thanks to Icelandic for
|
289
|
+
[the name of the module](https://translate.google.ru/?hl=en#is/en/eigindir).
|
290
|
+
|
291
|
+
Installation
|
292
|
+
------------
|
293
|
+
|
294
|
+
Add this line to your application's Gemfile:
|
295
|
+
|
296
|
+
```ruby
|
297
|
+
# Gemfile
|
298
|
+
gem "eigindir"
|
299
|
+
```
|
300
|
+
|
301
|
+
Then execute:
|
302
|
+
|
303
|
+
```
|
304
|
+
bundle
|
305
|
+
```
|
306
|
+
|
307
|
+
Or add it manually:
|
308
|
+
|
309
|
+
```
|
310
|
+
gem install eigindir
|
311
|
+
```
|
312
|
+
|
313
|
+
Compatibility
|
314
|
+
-------------
|
315
|
+
|
316
|
+
Tested under [MRI rubies 2.1+](.travis.yml) only.
|
317
|
+
|
318
|
+
It uses [Ruby 2.1+ refinements] that aren't yet supported by Rubinius and JRuby.
|
319
|
+
|
320
|
+
Uses [RSpec] 3.0+ for testing and [hexx-suit] for dev/test tools collection.
|
321
|
+
|
322
|
+
[Ruby 2.1+ refinements]: http://ruby-doc.org/core-2.1.1/doc/syntax/refinements_rdoc.html
|
323
|
+
[RSpec]: http://rspec.org
|
324
|
+
[hexx-suit]: https://github.com/nepalez/hexx-suit
|
325
|
+
|
326
|
+
Contributing
|
327
|
+
------------
|
328
|
+
|
329
|
+
* Fork the project.
|
330
|
+
* Read the [STYLEGUIDE](config/metrics/STYLEGUIDE).
|
331
|
+
* Make your feature addition or bug fix.
|
332
|
+
* Add tests for it. This is important so I don't break it in a
|
333
|
+
future version unintentionally.
|
334
|
+
* Commit, do not mess with Rakefile or version
|
335
|
+
(if you want to have your own version, that is fine but bump version
|
336
|
+
in a commit by itself I can ignore when I pull)
|
337
|
+
* Send me a pull request. Bonus points for topic branches.
|
338
|
+
|
339
|
+
License
|
340
|
+
-------
|
341
|
+
|
342
|
+
See the [MIT LICENSE](LICENSE).
|