enumex 0.0.1 → 0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6651c8a9336684d0e1cd34331e9d69ce75b7770d
4
- data.tar.gz: ba8c16d31f15303c0ec6be0336201aae786c561d
3
+ metadata.gz: 90e5f8ea91ff80f4e2db270a0b150ffde3958409
4
+ data.tar.gz: c392358418b8e5b27db1c50d9b79bd68f860fda3
5
5
  SHA512:
6
- metadata.gz: b174dc6ce15e5fabc5eae0097888c4381dff8781f44de955ddca5d0275cb292b78256b88458be2db8f001122b95fc1f460f8bb2acd8178bdac67489cb9b42981
7
- data.tar.gz: 509b2bbb3653178598391135a2cee51683e237ae545dcb169d2b5df71a561364688833ac51c694516a1a5785f453b204464a747e4ab5a94af8eff4b548410525
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
  ```
@@ -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 for Enumerator.'
9
- gem.summary = 'Enumex is a utility for Enumerator.'
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']
@@ -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 :Action, 'enumex/action'
21
- autoload :Actions, 'enumex/actions'
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
- Action.instance_eval { include klass::Port }
23
+ ActionContainer.instance_eval { include klass::Port }
38
24
  end
39
25
 
40
26
  refine Object do
41
- define_method :enumex, ->(enumerator = nil, &block) do
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 Actions
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, Action.new(base))
17
+ v ? v : instance_variable_set(name, ActionContainer.new(base))
18
18
  end
19
19
  end
20
20
 
@@ -1,36 +1,45 @@
1
1
  module Enumex
2
2
  class Base
3
- attr_reader :enumerator, :extenders
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
- Actions.types.each do |nm|
14
+ ActionContainers.types.each do |nm|
13
15
  define_method "#{nm}_action", -> { actions.public_send(nm) }
14
16
  end
15
17
 
16
- def run
17
- actions.reset
18
+ private
18
19
 
19
- enumerator.each do |*args|
20
- actions.pre.extenders.any? {|ext| ext.execute(*args) }
20
+ attr_reader :block
21
21
 
22
- block.call(*args).tap do |result|
23
- actions.post.extenders.each {|ext| ext.execute(*args) }
24
- end
25
- end
22
+ def actions
23
+ @actions ||= ActionContainers.new(self)
26
24
  end
27
25
 
28
- private
26
+ def run
27
+ return unless enumerator
29
28
 
30
- attr_reader :block
29
+ enm = new_enumerator
30
+ block ? enm.each(&block) : enm
31
+ end
31
32
 
32
- def actions
33
- @actions ||= Actions.new(self)
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
@@ -21,7 +21,7 @@ module Enumex
21
21
  executor.block = block
22
22
  base.extenders << self if block_given?
23
23
 
24
- base.enumerator ? base.run : base
24
+ base.enumerator ? base.attach_to(base.enumerator, &base.block) : base
25
25
  end
26
26
 
27
27
  private
@@ -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).value
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
@@ -1,3 +1,3 @@
1
1
  module Enumex
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
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.1
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-26 00:00:00.000000000 Z
11
+ date: 2016-11-27 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Enumex is a utility for Enumerator.
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/action.rb
23
- - lib/enumex/actions.rb
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 for Enumerator.
51
+ summary: Enumex is a utility to attach preprocessing and postprocessing to Enumerator.
52
52
  test_files: []
@@ -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