modifiers 1.1.0 → 2.0.2
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 +13 -5
- data/.rubocop.yml +6 -0
- data/.rubocop_todo.yml +10 -0
- data/.travis.yml +6 -5
- data/README.md +28 -24
- data/Rakefile +4 -2
- data/lib/modifiers/command.rb +1 -1
- data/lib/modifiers/query.rb +3 -3
- data/lib/modifiers/version.rb +1 -1
- data/modifiers.gemspec +3 -2
- data/spec/modifiers/command_spec.rb +2 -2
- data/spec/modifiers/define_modifier_spec.rb +1 -1
- data/spec/modifiers/deprecated_spec.rb +1 -1
- data/spec/modifiers/query_spec.rb +1 -0
- data/spec/shared_examples_for_modifiers.rb +1 -3
- metadata +27 -11
checksums.yaml
CHANGED
@@ -1,7 +1,15 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
Yzc0Y2YyNjU3YjUyOTRjYWE1ZjcwMmQxZjFhN2FiNTgxNThiMjMwNg==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NGMyZTkyMzViNjM4NjUwYTE0ZWJmNmIxODg5ZjBhNmYzNzQyZmJiMw==
|
5
7
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ZDIxNDVmYTFmNzY0ODQ3YWRkMjljOWZkNjk4Mjg0YTZmOTE4ZDZlY2RiZjUw
|
10
|
+
NDBkZjFiNmRiYzk3ODM4MTA3MTQzNmU1M2VlMmY3NTVjNTYyZGUwNzU1MTU0
|
11
|
+
NDEzOTM2MmQ1MDQyMDE0ZjczNTg4MWNiNGNiNmZmNWQ4Y2JkN2U=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NzcwMjZmNTA5MzJhYTdhMWM2MDM2NGM2OTQ5YzliZWNlZmVmM2Y1MGFmY2Ux
|
14
|
+
MDNkMDYyNjAyM2UxZGZlNGZlY2NkMmQ1YWY0ZDVlZjg1OTYxMzIxMTg1ZWRk
|
15
|
+
MTM3NzYxMjM3YzUzY2I4NTRiZTc5NDQ1NDZkYzhjNTZlNmVhMjc=
|
data/.rubocop.yml
ADDED
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# This configuration was generated by `rubocop --auto-gen-config`
|
2
|
+
# on 2014-09-20 01:40:17 +0000 using RuboCop version 0.26.1.
|
3
|
+
# The point is for the user to remove these configuration records
|
4
|
+
# one by one as the offenses are removed from the code base.
|
5
|
+
# Note that changes in the inspected code, or installation of new
|
6
|
+
# versions of RuboCop, may require this file to be generated again.
|
7
|
+
|
8
|
+
# Offense count: 11
|
9
|
+
Style/Documentation:
|
10
|
+
Enabled: false
|
data/.travis.yml
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
language: ruby
|
2
2
|
rvm:
|
3
|
-
|
4
|
-
|
5
|
-
- 2.1.1
|
6
|
-
- 2.1.2
|
3
|
+
- 2.0.0
|
4
|
+
- 2.1.5
|
7
5
|
deploy:
|
8
6
|
provider: rubygems
|
9
7
|
api_key:
|
10
|
-
secure:
|
8
|
+
secure: ZPeXm1QrIHsDx+ltW0aQ5mKK2vdeRHiHltBHO+KnnWMygHmeoe4ophwQetUhee/oH1LL9W1KdpLc+tSvt64PN0WooMdJtsbOA7M3DLOZGSrqNtTHM3b0xcLrQFIg8wLKDlp/0jx4xUFHybCIapsXT2N+Q8EdgtiS1lwRKEcN44I=
|
9
|
+
gem: modifiers
|
11
10
|
on:
|
12
11
|
tags: true
|
12
|
+
repo: nicknovitski/modifiers
|
13
|
+
all_branches: true
|
data/README.md
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# Modifiers
|
2
|
-
[](https://travis-ci.org/nicknovitski/modifiers)
|
2
|
+
[](https://travis-ci.org/nicknovitski/modifiers) [](https://codeclimate.com/github/nicknovitski/modifiers)
|
3
3
|
|
4
4
|
## What is/are Modifiers?
|
5
5
|
|
6
|
-
`
|
6
|
+
`modifiers` is a collection of **method modifiers**, and a way to make more.
|
7
7
|
|
8
|
-
|
8
|
+
Method modifiers, obviously, modify methods. Specifically, in Ruby
|
9
9
|
terms, they are class methods which:
|
10
10
|
|
11
11
|
1. Take a symbol argument which names an instance method of the same class, _and_
|
@@ -27,10 +27,10 @@ end
|
|
27
27
|
```
|
28
28
|
|
29
29
|
This method is quite small, but it still complects the concerns of counting
|
30
|
-
ducks
|
31
|
-
concern might be
|
30
|
+
ducks with that of saving and reusing the result of a calculation, and that latter
|
31
|
+
concern might be duplicated any number of times across your codebase.
|
32
32
|
|
33
|
-
With modifiers
|
33
|
+
With `modifiers`, you can encapsulate the implementation of the memoization, and
|
34
34
|
keep the intent:
|
35
35
|
```ruby
|
36
36
|
def count_ducks
|
@@ -67,7 +67,7 @@ And then execute:
|
|
67
67
|
|
68
68
|
## Usage
|
69
69
|
|
70
|
-
### built-in modifiers
|
70
|
+
### built-in method modifiers
|
71
71
|
|
72
72
|
#### memoized
|
73
73
|
|
@@ -135,12 +135,12 @@ no side-effects.
|
|
135
135
|
It may or may not be a good idea, but at least now it's easy to unambiguously
|
136
136
|
indicate and enforce!
|
137
137
|
|
138
|
-
First, a method modified by `command` will always return
|
139
|
-
sounds.
|
138
|
+
First, a method modified by `command` will always return `self`. It's as
|
139
|
+
trivial as it sounds.
|
140
140
|
|
141
|
-
Conversely (
|
142
|
-
|
143
|
-
more impressive.
|
141
|
+
Conversely (TODO: find out if this is actually what converse means), a method
|
142
|
+
modified by `query` will never change the state of anything non-global and
|
143
|
+
in-process. This is also trivial, but it might seem more impressive.
|
144
144
|
|
145
145
|
```ruby
|
146
146
|
require 'modifiers/command_query'
|
@@ -169,14 +169,14 @@ john = DuckFarmer.new(Array.new(3) { DuckHutch.new(rand(20)) })
|
|
169
169
|
|
170
170
|
john.fullest_hutch # => #<struct DuckHutch num_eggs=11>
|
171
171
|
|
172
|
-
john.hutches.any?
|
172
|
+
john.hutches.any?(&:ducks_disturbed?) # => false
|
173
173
|
```
|
174
174
|
|
175
175
|
If this was an infomercial, now is when I would say something like "It's just
|
176
176
|
that easy, Michael!", and you (your name is Michael in this scenario) would say
|
177
177
|
"Now _that's_ incredible!" and the audience would applaud.
|
178
178
|
|
179
|
-
### defining new modifiers
|
179
|
+
### defining new method modifiers
|
180
180
|
|
181
181
|
New modifiers can be defined in your own modules using the `define_modifier` method.
|
182
182
|
|
@@ -203,18 +203,20 @@ end
|
|
203
203
|
```
|
204
204
|
|
205
205
|
Much as with `define_method`, the first argument to `define_modifier` gives us
|
206
|
-
the name of the new modifier, and the block gives us the implementation
|
207
|
-
given
|
208
|
-
(in this case, `DuckFarm#farm`), does whatever it likes, then invokes
|
209
|
-
original method using `super`.
|
206
|
+
the name of the new method modifier, and the block gives us the implementation
|
207
|
+
of a given **modification**: a method which intercepts calls to the original
|
208
|
+
method (in this case, `DuckFarm#farm`), does whatever it likes, then invokes
|
209
|
+
that original method using `super`.
|
210
210
|
|
211
|
-
|
212
|
-
calling `super`.
|
211
|
+
Sadly, just as with `define_method`, you have to use explicit arguments when
|
212
|
+
calling `super`. I genuinely and sincerely apologize for this leaky abstraction,
|
213
|
+
and wish I knew a way to optimize for the common case of just calling through with
|
214
|
+
unchanged arguments without adding significant implementation complexity.
|
213
215
|
|
214
|
-
But maybe you don't want to call the original method at all!
|
216
|
+
But hey, maybe I'm lucky and you don't want to call the original method at all!
|
215
217
|
```ruby
|
216
218
|
module DuckFarmModifiers
|
217
|
-
define_modifier(:
|
219
|
+
define_modifier(:disabled) { }
|
218
220
|
end
|
219
221
|
```
|
220
222
|
|
@@ -225,8 +227,10 @@ module DuckFarmModifiers
|
|
225
227
|
super(*args.map(&:to_i), &block)
|
226
228
|
end
|
227
229
|
end
|
230
|
+
```
|
228
231
|
|
229
|
-
You can do things before, after, or even "around" the invocation
|
232
|
+
You can do things before, after, or even "around" the invocation; It's
|
233
|
+
Just Ruby®!
|
230
234
|
```ruby
|
231
235
|
module DuckFarmModifiers
|
232
236
|
define_modifier(:perf_logged) do |*args, &block|
|
@@ -237,7 +241,7 @@ module DuckFarmModifiers
|
|
237
241
|
end
|
238
242
|
```
|
239
243
|
|
240
|
-
### Extending modifiers
|
244
|
+
### Extending method modifiers
|
241
245
|
|
242
246
|
The body of a modifier will be evaluated in the context of the receiver
|
243
247
|
instance of the modified method, so you can refer to any other instance
|
data/Rakefile
CHANGED
data/lib/modifiers/command.rb
CHANGED
data/lib/modifiers/query.rb
CHANGED
@@ -9,10 +9,10 @@ module Modifiers
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def duplicate_ivars!
|
12
|
-
ivars_hash.each { |k,v| instance_variable_set(k, Marshal.load(Marshal.dump(v))) }
|
12
|
+
ivars_hash.each { |k, v| instance_variable_set(k, Marshal.load(Marshal.dump(v))) }
|
13
13
|
end
|
14
14
|
|
15
|
-
def
|
15
|
+
def reset_ivars(hash)
|
16
16
|
hash.each { |k, v| instance_variable_set(k, v) }
|
17
17
|
|
18
18
|
in_state_but_not_hash = instance_variables.select { |i| hash[i].nil? }
|
@@ -24,7 +24,7 @@ module Modifiers
|
|
24
24
|
orig_ivars = ivars_hash
|
25
25
|
duplicate_ivars!
|
26
26
|
return_value = super(*args, &block)
|
27
|
-
|
27
|
+
reset_ivars(orig_ivars)
|
28
28
|
return_value
|
29
29
|
end
|
30
30
|
end
|
data/lib/modifiers/version.rb
CHANGED
data/modifiers.gemspec
CHANGED
@@ -19,7 +19,8 @@ Gem::Specification.new do |spec|
|
|
19
19
|
|
20
20
|
spec.required_ruby_version = '>= 2.0.0'
|
21
21
|
|
22
|
-
spec.add_development_dependency 'bundler', '~> 1.
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.7'
|
23
23
|
spec.add_development_dependency 'rake'
|
24
|
-
spec.add_development_dependency
|
24
|
+
spec.add_development_dependency 'rspec'
|
25
|
+
spec.add_development_dependency 'rubocop'
|
25
26
|
end
|
@@ -16,8 +16,8 @@ RSpec.describe Modifiers do
|
|
16
16
|
describe '#command' do
|
17
17
|
it_behaves_like 'a modifier', :command, changes_return_value: true
|
18
18
|
|
19
|
-
it 'causes the method to always return
|
20
|
-
expect(doer.take_action).to
|
19
|
+
it 'causes the method to always return the receiver' do
|
20
|
+
expect(doer.take_action).to be doer
|
21
21
|
end
|
22
22
|
end
|
23
23
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
RSpec.shared_examples 'a modifier' do |modifier, changes_return_value: false|
|
2
2
|
let(:test_class) do
|
3
|
-
|
3
|
+
Class.new do
|
4
4
|
extend Modifiers
|
5
5
|
|
6
6
|
def public_method(arg = :foo)
|
@@ -19,9 +19,7 @@ RSpec.shared_examples 'a modifier' do |modifier, changes_return_value: false|
|
|
19
19
|
#
|
20
20
|
end
|
21
21
|
end
|
22
|
-
Object.const_set(random_class_name, klass)
|
23
22
|
end
|
24
|
-
let(:random_class_name) { (0...10).map { ('A'..'Z').to_a[rand(26)] }.join }
|
25
23
|
subject(:instance) { test_class.new }
|
26
24
|
|
27
25
|
it 'returns the symbol it is passed' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: modifiers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Novitski
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -16,40 +16,54 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '1.
|
19
|
+
version: '1.7'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '1.
|
26
|
+
version: '1.7'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - '>='
|
31
|
+
- - ! '>='
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - '>='
|
38
|
+
- - ! '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - '>='
|
45
|
+
- - ! '>='
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - '>='
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
53
67
|
- !ruby/object:Gem::Version
|
54
68
|
version: '0'
|
55
69
|
description: A simple and composable way to add functionality to methods.
|
@@ -61,6 +75,8 @@ extra_rdoc_files: []
|
|
61
75
|
files:
|
62
76
|
- .gitignore
|
63
77
|
- .rspec
|
78
|
+
- .rubocop.yml
|
79
|
+
- .rubocop_todo.yml
|
64
80
|
- .travis.yml
|
65
81
|
- Gemfile
|
66
82
|
- LICENSE.txt
|
@@ -93,17 +109,17 @@ require_paths:
|
|
93
109
|
- lib
|
94
110
|
required_ruby_version: !ruby/object:Gem::Requirement
|
95
111
|
requirements:
|
96
|
-
- - '>='
|
112
|
+
- - ! '>='
|
97
113
|
- !ruby/object:Gem::Version
|
98
114
|
version: 2.0.0
|
99
115
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
116
|
requirements:
|
101
|
-
- - '>='
|
117
|
+
- - ! '>='
|
102
118
|
- !ruby/object:Gem::Version
|
103
119
|
version: '0'
|
104
120
|
requirements: []
|
105
121
|
rubyforge_project:
|
106
|
-
rubygems_version: 2.
|
122
|
+
rubygems_version: 2.4.2
|
107
123
|
signing_key:
|
108
124
|
specification_version: 4
|
109
125
|
summary: Cute and Easy method modifiers (also called decorators)
|