enumex 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +14 -0
- data/enumex.gemspec +2 -2
- data/lib/enumex.rb +4 -18
- data/lib/enumex/action_container.rb +20 -0
- data/lib/enumex/{actions.rb → action_containers.rb} +2 -2
- data/lib/enumex/base.rb +23 -14
- data/lib/enumex/extenders/every_once_extender.rb +1 -1
- data/lib/enumex/extenders/every_time_extender.rb +5 -6
- data/lib/enumex/version.rb +1 -1
- metadata +6 -6
- data/lib/enumex/action.rb +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 90e5f8ea91ff80f4e2db270a0b150ffde3958409
|
4
|
+
data.tar.gz: c392358418b8e5b27db1c50d9b79bd68f860fda3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b5fe35bdbc63eec57698a26253bc84c29f68909f216c8e233aa812d38aec9ce48d67e95d220565069e15192850cec1f941a78d7a5409835112aed970ccc5207b
|
7
|
+
data.tar.gz: eda9ff34b9e15dffa26d652b6ebdfc900c49a36562f16aa7eb1ecd87b7ecb96f9cb60277f2b14e1209a94c36ba2c5a23e75c87917d7814a724f947716fdc764e
|
data/README.md
CHANGED
@@ -40,4 +40,18 @@ enumex((1..10).each) {|n|
|
|
40
40
|
}.post_action.every_once(3).times {
|
41
41
|
puts "sleep"
|
42
42
|
}
|
43
|
+
```
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
using Enumex
|
47
|
+
|
48
|
+
enumex.post_action.every_once(3).times {
|
49
|
+
print "Fizz"
|
50
|
+
}.every_once(5).times {
|
51
|
+
print "Buzz"
|
52
|
+
}.every_time {
|
53
|
+
print "\n"
|
54
|
+
}.attach_to((1..100).each) do |n|
|
55
|
+
print "#{n}: "
|
56
|
+
end
|
43
57
|
```
|
data/enumex.gemspec
CHANGED
@@ -5,8 +5,8 @@ Gem::Specification.new do |gem|
|
|
5
5
|
gem.version = Enumex::VERSION
|
6
6
|
gem.authors = ['m4oda']
|
7
7
|
gem.email = 'e5ww2sze@gmail.com'
|
8
|
-
gem.description = 'Enumex is a utility
|
9
|
-
gem.summary = 'Enumex is a utility
|
8
|
+
gem.description = 'Enumex is a utility to attach preprocessing and postprocessing to Enumerator.'
|
9
|
+
gem.summary = 'Enumex is a utility to attach preprocessing and postprocessing to Enumerator.'
|
10
10
|
gem.homepage = 'https://github.com/m4oda/enumex'
|
11
11
|
gem.license = 'MIT'
|
12
12
|
gem.require_paths = ['lib']
|
data/lib/enumex.rb
CHANGED
@@ -2,23 +2,9 @@ require 'forwardable'
|
|
2
2
|
require 'enumex/version'
|
3
3
|
|
4
4
|
module Enumex
|
5
|
-
# Enumex.attach_to([1, 2, 3, 4, 5].each) {|n|
|
6
|
-
# puts n
|
7
|
-
# }.every_once(3).times {
|
8
|
-
# puts "\n"
|
9
|
-
# }
|
10
|
-
#
|
11
|
-
# enm = Enumex.every_once(3).times do
|
12
|
-
# "\n"
|
13
|
-
# end
|
14
|
-
#
|
15
|
-
# enum.attach_to([1, 2, 3, 4, 5].each) do |n|
|
16
|
-
# puts n
|
17
|
-
# end
|
18
|
-
|
19
5
|
autoload :Base, 'enumex/base'
|
20
|
-
autoload :
|
21
|
-
autoload :
|
6
|
+
autoload :ActionContainer, 'enumex/action_container'
|
7
|
+
autoload :ActionContainers, 'enumex/action_containers'
|
22
8
|
|
23
9
|
module Extenders
|
24
10
|
autoload :EveryOnceExtender, 'enumex/extenders/every_once_extender'
|
@@ -34,11 +20,11 @@ module Enumex
|
|
34
20
|
Extenders.constants.map {|nm|
|
35
21
|
Extenders.const_get(nm)
|
36
22
|
}.each do |klass|
|
37
|
-
|
23
|
+
ActionContainer.instance_eval { include klass::Port }
|
38
24
|
end
|
39
25
|
|
40
26
|
refine Object do
|
41
|
-
define_method :enumex, ->(enumerator
|
27
|
+
define_method :enumex, ->(enumerator=nil, &block) do
|
42
28
|
Enumex.new(enumerator, &block)
|
43
29
|
end
|
44
30
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Enumex
|
2
|
+
class ActionContainer
|
3
|
+
extend Forwardable
|
4
|
+
|
5
|
+
def_delegators :@root,
|
6
|
+
:attach_to, :enumerator, :block, :run,
|
7
|
+
*ActionContainers.types.map {|nm| "#{nm}_action" }
|
8
|
+
|
9
|
+
attr_reader :extenders
|
10
|
+
|
11
|
+
def initialize(root)
|
12
|
+
@root = root
|
13
|
+
@extenders = []
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
attr_reader :root
|
19
|
+
end
|
20
|
+
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
module Enumex
|
2
|
-
class
|
2
|
+
class ActionContainers
|
3
3
|
class << self
|
4
4
|
def types
|
5
5
|
[:post, :pre]
|
@@ -14,7 +14,7 @@ module Enumex
|
|
14
14
|
define_method nm, -> do
|
15
15
|
name = "@#{nm}"
|
16
16
|
v = instance_variable_get(name)
|
17
|
-
v ? v : instance_variable_set(name,
|
17
|
+
v ? v : instance_variable_set(name, ActionContainer.new(base))
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
data/lib/enumex/base.rb
CHANGED
@@ -1,36 +1,45 @@
|
|
1
1
|
module Enumex
|
2
2
|
class Base
|
3
|
-
attr_reader :enumerator
|
3
|
+
attr_reader :enumerator
|
4
4
|
|
5
5
|
def attach_to(enumerator, &block)
|
6
|
+
raise TypeError unless enumerator.is_a?(Enumerator)
|
7
|
+
|
6
8
|
@enumerator = enumerator
|
7
9
|
@block = block
|
8
10
|
|
9
11
|
actions.empty? ? self : run
|
10
12
|
end
|
11
13
|
|
12
|
-
|
14
|
+
ActionContainers.types.each do |nm|
|
13
15
|
define_method "#{nm}_action", -> { actions.public_send(nm) }
|
14
16
|
end
|
15
17
|
|
16
|
-
|
17
|
-
actions.reset
|
18
|
+
private
|
18
19
|
|
19
|
-
|
20
|
-
actions.pre.extenders.any? {|ext| ext.execute(*args) }
|
20
|
+
attr_reader :block
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
end
|
25
|
-
end
|
22
|
+
def actions
|
23
|
+
@actions ||= ActionContainers.new(self)
|
26
24
|
end
|
27
25
|
|
28
|
-
|
26
|
+
def run
|
27
|
+
return unless enumerator
|
29
28
|
|
30
|
-
|
29
|
+
enm = new_enumerator
|
30
|
+
block ? enm.each(&block) : enm
|
31
|
+
end
|
31
32
|
|
32
|
-
def
|
33
|
-
|
33
|
+
def new_enumerator
|
34
|
+
Enumerator.new do |y|
|
35
|
+
actions.reset
|
36
|
+
enumerator.each do |*args|
|
37
|
+
actions.pre.extenders.each {|e| e.execute(*args) }
|
38
|
+
result = y.yield(*args)
|
39
|
+
actions.post.extenders.each {|e| e.execute(*args) }
|
40
|
+
result
|
41
|
+
end
|
42
|
+
end
|
34
43
|
end
|
35
44
|
end
|
36
45
|
end
|
@@ -3,7 +3,7 @@ module Enumex
|
|
3
3
|
class EveryTimeExtender
|
4
4
|
Port = Module.new do
|
5
5
|
def every_time(&block)
|
6
|
-
EveryTimeExtender.new(self, &block).
|
6
|
+
EveryTimeExtender.new(self, &block).instance_eval { evaluate }
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
@@ -11,7 +11,6 @@ module Enumex
|
|
11
11
|
@base = base
|
12
12
|
@block = block
|
13
13
|
base.extenders << self if block_given?
|
14
|
-
base.enumerator ? base.run : base
|
15
14
|
end
|
16
15
|
|
17
16
|
def reset
|
@@ -21,13 +20,13 @@ module Enumex
|
|
21
20
|
block.call(*args) if block
|
22
21
|
end
|
23
22
|
|
24
|
-
def value
|
25
|
-
base.enumerator ? base.run : base
|
26
|
-
end
|
27
|
-
|
28
23
|
private
|
29
24
|
|
30
25
|
attr_reader :base, :block
|
26
|
+
|
27
|
+
def evaluate
|
28
|
+
base.enumerator ? base.attach_to(base.enumerator, &base.block) : base
|
29
|
+
end
|
31
30
|
end
|
32
31
|
end
|
33
32
|
end
|
data/lib/enumex/version.rb
CHANGED
metadata
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enumex
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- m4oda
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-11-
|
11
|
+
date: 2016-11-27 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description: Enumex is a utility
|
13
|
+
description: Enumex is a utility to attach preprocessing and postprocessing to Enumerator.
|
14
14
|
email: e5ww2sze@gmail.com
|
15
15
|
executables: []
|
16
16
|
extensions: []
|
@@ -19,8 +19,8 @@ files:
|
|
19
19
|
- README.md
|
20
20
|
- enumex.gemspec
|
21
21
|
- lib/enumex.rb
|
22
|
-
- lib/enumex/
|
23
|
-
- lib/enumex/
|
22
|
+
- lib/enumex/action_container.rb
|
23
|
+
- lib/enumex/action_containers.rb
|
24
24
|
- lib/enumex/base.rb
|
25
25
|
- lib/enumex/extenders/every_once_extender.rb
|
26
26
|
- lib/enumex/extenders/every_time_extender.rb
|
@@ -48,5 +48,5 @@ rubyforge_project:
|
|
48
48
|
rubygems_version: 2.4.5
|
49
49
|
signing_key:
|
50
50
|
specification_version: 4
|
51
|
-
summary: Enumex is a utility
|
51
|
+
summary: Enumex is a utility to attach preprocessing and postprocessing to Enumerator.
|
52
52
|
test_files: []
|
data/lib/enumex/action.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
module Enumex
|
2
|
-
class Action
|
3
|
-
extend Forwardable
|
4
|
-
|
5
|
-
def_delegators :@root,
|
6
|
-
:attach_to, :enumerator, :run,
|
7
|
-
*Actions.types.map {|nm| "#{nm}_action" }
|
8
|
-
|
9
|
-
attr_reader :root, :extenders
|
10
|
-
|
11
|
-
def initialize(root)
|
12
|
-
@root = root
|
13
|
-
@extenders = []
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|