enum_args 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -6
- data/lib/enum_args/proxied_enumerable.rb +23 -26
- data/lib/enum_args/proxy.rb +1 -0
- data/lib/enum_args/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c4ec59ecef09b36ae718c8b4e15386620803a609
|
4
|
+
data.tar.gz: b986fb672cac0ef9bb05408baa499885a3124779
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 45c8476b26ea171fec07746bd53c7f56e45f8ef3e5126d11456c11448d4215fad3785c5ab18de5a2b55c695df4f36e06118ca0811471115081e6f6cfeee49b1e
|
7
|
+
data.tar.gz: a5462e8eecf041017b4a44a83df9a4502ef22cf43a22c9bf1080fb2897181fcc9badf4c9e9297d8a64d13879b1ed22ed0fb32c870dedaedcdc6f1adff249a819
|
data/README.md
CHANGED
@@ -9,6 +9,17 @@ could, ie. `ai_team.select(max_cpu: 80) { |troop| troop.can_hit?(enemy) }.each
|
|
9
9
|
|
10
10
|
See Usage for some examples.
|
11
11
|
|
12
|
+
### Namespace pollution
|
13
|
+
|
14
|
+
A common issue with this sort of gems is namespace pollution. EnumArgs is wise
|
15
|
+
enough to pollute your namespace with the bare minimum needed.
|
16
|
+
|
17
|
+
All of this is done by adding, apart from the Enumerable instance methods, _one_
|
18
|
+
single accessor (instance) method to your class and _one_ single instance variable
|
19
|
+
with configurable name (default is `enum_args`). A few _class_ methods are added
|
20
|
+
to hold default values for your enumerator, all of them prefixed with
|
21
|
+
`enum_args_`.
|
22
|
+
|
12
23
|
## Installation
|
13
24
|
|
14
25
|
Add this line to your application's Gemfile:
|
@@ -42,7 +53,7 @@ class MyCollection
|
|
42
53
|
end
|
43
54
|
|
44
55
|
def each(pause = 1, verbose = false)
|
45
|
-
return enum_for(:each, pause) unless block_given?
|
56
|
+
return enum_for(:each, pause, verbose) unless block_given?
|
46
57
|
ary.each do |element|
|
47
58
|
yield element
|
48
59
|
puts "sleeping for #{pause} secs" if verbose
|
@@ -74,14 +85,14 @@ not exactly ergonomic. But what happens if I wanted to suddenly increase the
|
|
74
85
|
pause time used to 5 secs?
|
75
86
|
|
76
87
|
```ruby
|
77
|
-
enumerator.each(10) do |
|
88
|
+
enumerator.each(10) do |element|
|
78
89
|
puts "This does not quite work"
|
79
90
|
end
|
80
91
|
```
|
81
92
|
|
82
93
|
Well, we could do better, see:
|
83
94
|
```ruby
|
84
|
-
collection.each(5) do |
|
95
|
+
collection.each(5) do |element|
|
85
96
|
puts "This does work indeed!"
|
86
97
|
end
|
87
98
|
```
|
@@ -91,8 +102,8 @@ whole process again... Except you'll soon discover this:
|
|
91
102
|
|
92
103
|
```ruby
|
93
104
|
# won't work :(
|
94
|
-
collection.select(5) do |
|
95
|
-
|
105
|
+
collection.select(5) do |element|
|
106
|
+
element.odd?
|
96
107
|
end
|
97
108
|
```
|
98
109
|
|
@@ -167,7 +178,7 @@ methods. The instance method you will get is `enum_args`, which is the actual
|
|
167
178
|
enumerator that will use your iterator method.
|
168
179
|
|
169
180
|
If that bothers you, you can change it by specifiyng a different symbol to
|
170
|
-
`enum_args_for` using the `
|
181
|
+
`enum_args_for` using the `with_enum_args_as: :your_symbol` keyword argument.
|
171
182
|
|
172
183
|
## Development
|
173
184
|
|
@@ -5,28 +5,28 @@ module EnumArgs
|
|
5
5
|
|
6
6
|
module ProxiedEnumerable
|
7
7
|
module ClassMethods
|
8
|
-
def
|
9
|
-
@
|
8
|
+
def enum_args_method
|
9
|
+
@enum_args_method ||= :iterator
|
10
10
|
end
|
11
11
|
|
12
|
-
def
|
13
|
-
@
|
12
|
+
def enum_args_accessor_method
|
13
|
+
@enum_args_accessor_method ||= :enum_args
|
14
14
|
end
|
15
15
|
|
16
|
-
def
|
17
|
-
@
|
16
|
+
def enum_args_default_args
|
17
|
+
@enum_args_default_args ||= []
|
18
18
|
end
|
19
19
|
|
20
|
-
def
|
21
|
-
@
|
20
|
+
def enum_args_default_using
|
21
|
+
@enum_args_default_using ||= {}
|
22
22
|
end
|
23
23
|
|
24
|
-
def enum_args_for(method, *args, using: {},
|
25
|
-
@
|
26
|
-
@
|
24
|
+
def enum_args_for(method, *args, using: {}, with_enum_args_as: :enum_args)
|
25
|
+
@enum_args_method = method
|
26
|
+
@enum_args_default_args = args
|
27
27
|
raise TypeError, "expected Hash, found #{using.class}" unless using.is_a? Hash
|
28
|
-
@
|
29
|
-
@
|
28
|
+
@enum_args_default_using = using
|
29
|
+
@enum_args_accessor_method = with_enum_args_as
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
@@ -43,24 +43,21 @@ module EnumArgs
|
|
43
43
|
end
|
44
44
|
|
45
45
|
on_enumerable_methods self do |*args, &blk|
|
46
|
-
send(self.class.
|
46
|
+
send(self.class.enum_args_accessor_method).send __method__, *args, &blk
|
47
47
|
end
|
48
48
|
|
49
49
|
def initialize(*args, &blk)
|
50
|
-
|
51
|
-
|
52
|
-
|
50
|
+
klass = self.class
|
51
|
+
name = klass.enum_args_accessor_method
|
52
|
+
instance_variable_set(
|
53
|
+
"@#{name}",
|
54
|
+
EnumArgs::Proxy.new(self, klass.enum_args_method, *klass.enum_args_default_args,
|
55
|
+
using: klass.enum_args_default_using)
|
56
|
+
)
|
57
|
+
define_singleton_method(name) do
|
58
|
+
instance_variable_get("@#{name}")
|
53
59
|
end
|
54
60
|
super
|
55
61
|
end
|
56
|
-
|
57
|
-
def enum_args=(*args)
|
58
|
-
@enum_args.args = args
|
59
|
-
end
|
60
|
-
|
61
|
-
def enum_using=(using)
|
62
|
-
raise TypeError, "expected Hash, found #{using.class}" unless using.is_a? Hash
|
63
|
-
@enum_args.using = using
|
64
|
-
end
|
65
62
|
end
|
66
63
|
end
|
data/lib/enum_args/proxy.rb
CHANGED
data/lib/enum_args/version.rb
CHANGED