enum_args 1.0.1 → 1.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d30df69cd412bd2fd7ab806e32739eb3283058fd
4
- data.tar.gz: 54bf76bb749fdff1b1aa5bc5af29974ae4ad9228
3
+ metadata.gz: c4ec59ecef09b36ae718c8b4e15386620803a609
4
+ data.tar.gz: b986fb672cac0ef9bb05408baa499885a3124779
5
5
  SHA512:
6
- metadata.gz: d8f79beac8e7242382e529b1e6853c6f23516c075dd8629cde523e8d270d58a4351f9e401713383e7838482a499cc29cadafe46ad2c75c08b36a04c2d9b53297
7
- data.tar.gz: 390433118a98e132fb0bf2ab34295060af0236d37aa21d9fcdfa0f2856d7c3b56a91be45916544c374e5e58729d7b3f012c0b8e95a5e95394d374b4c77af01aa
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 |elements|
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 |elements|
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 |e|
95
- e.odd?
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 `with_enum_as: :your_symbol` keyword argument.
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 enum_method
9
- @enum_method ||= :iterator
8
+ def enum_args_method
9
+ @enum_args_method ||= :iterator
10
10
  end
11
11
 
12
- def enum_accessor_method
13
- @enum_accessor_method ||= :enum_args
12
+ def enum_args_accessor_method
13
+ @enum_args_accessor_method ||= :enum_args
14
14
  end
15
15
 
16
- def enum_default_args
17
- @enum_default_args ||= []
16
+ def enum_args_default_args
17
+ @enum_args_default_args ||= []
18
18
  end
19
19
 
20
- def enum_default_using
21
- @enum_default_using ||= {}
20
+ def enum_args_default_using
21
+ @enum_args_default_using ||= {}
22
22
  end
23
23
 
24
- def enum_args_for(method, *args, using: {}, with_enum_as: :enum_args)
25
- @enum_method = method
26
- @enum_default_args = args
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
- @enum_default_using = using
29
- @enum_accessor_method = with_enum_as
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.enum_accessor_method).send __method__, *args, &blk
46
+ send(self.class.enum_args_accessor_method).send __method__, *args, &blk
47
47
  end
48
48
 
49
49
  def initialize(*args, &blk)
50
- @enum_args = EnumArgs::Proxy.new self, self.class.enum_method, *self.class.enum_default_args, using: self.class.enum_default_using
51
- define_singleton_method(self.class.enum_accessor_method) do
52
- instance_variable_get('@enum_args')
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
@@ -53,6 +53,7 @@ module EnumArgs
53
53
  end
54
54
 
55
55
  def reset_default_enum
56
+ raise TypeError, "expected Hash, found #{using.class}" unless using.is_a? Hash
56
57
  self.enum = build_enum
57
58
  end
58
59
 
@@ -1,3 +1,3 @@
1
1
  module EnumArgs
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enum_args
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alejandro Martinez Ruiz