LazyEnumerable 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.
@@ -0,0 +1,42 @@
1
+ module Lazy
2
+ class AbstractEnumerable
3
+ PLACEBO=lambda {|each| each}
4
+
5
+ ##
6
+ ## Remove any unnecessary methods so that method_missing is invoked
7
+ ##
8
+ def self.wack_all_my_methods
9
+ to_wack=instance_methods.reject do |each|
10
+ ['===','method_missing'].include?(each) || each =~ /^__/
11
+ end
12
+ to_wack.each do |each|
13
+ alias_method(("_" << each), each)
14
+ undef_method(each)
15
+ end
16
+ end
17
+
18
+ def self.iterator_creator(*method_names)
19
+ method_names.each { |every| iterator_creator_for(every) }
20
+ end
21
+
22
+ def self.iterator_creator_for(method_name)
23
+ non_lazy_name = /lazy_(.+)/.match(method_name.to_s)[1].to_sym
24
+ define_method(method_name) { Iterator.new(_method(non_lazy_name)) }
25
+ end
26
+
27
+ wack_all_my_methods
28
+ include Enumerable
29
+ iterator_creator :lazy_select, :lazy_collect, :lazy_reject, :lazy_detect, :lazy_each
30
+
31
+ ##
32
+ ## Give me the size. This can be dangerous if you ever wrap an infinite enumerable
33
+ ##
34
+ def size
35
+ inject(0) {|sum,each| sum + 1 }
36
+ end
37
+
38
+ def to_lazy
39
+ self
40
+ end
41
+ end
42
+ end
data/lib/lazy/iterator.rb CHANGED
@@ -10,7 +10,7 @@
10
10
  ###
11
11
  module Lazy
12
12
 
13
- class Iterator < LazyEnumerable
13
+ class Iterator < AbstractEnumerable
14
14
 
15
15
  def initialize(proc)
16
16
  @internal=proc
@@ -11,27 +11,7 @@
11
11
 
12
12
  module Lazy
13
13
 
14
- class LazyEnumerable
15
- ##
16
- ## Remove any unnecessary methods so that method_missing is invoked
17
- ##
18
- def self.wack_all_my_methods
19
- to_wack=instance_methods.reject do |each|
20
- ['===','method_missing'].include?(each) || each =~ /^__/
21
- end
22
- to_wack.each do |each|
23
- alias_method(("_" << each), each)
24
- undef_method(each)
25
- end
26
- end
27
-
28
- ##
29
- ## Wack all of my methods, then apply Enumerable and define my constants
30
- ## and class variable for cached method calls
31
- ##
32
- wack_all_my_methods
33
- include Enumerable
34
- PLACEBO=lambda {|each| each}
14
+ class LazyEnumerable < AbstractEnumerable
35
15
  @@cached={}
36
16
 
37
17
  ##
@@ -50,14 +30,7 @@ module Lazy
50
30
  @@cached[method_name]
51
31
  end
52
32
 
53
- def self.iterator_creator(*method_names)
54
- method_names.each { |every| iterator_creator_for(every) }
55
- end
56
33
 
57
- def self.iterator_creator_for(method_name)
58
- non_lazy_name = /lazy_(.+)/.match(method_name.to_s)[1].to_sym
59
- define_method(method_name) { Iterator.new(_method(non_lazy_name)) }
60
- end
61
34
 
62
35
  def initialize(original, block=PLACEBO)
63
36
  @original=original
@@ -97,27 +70,10 @@ module Lazy
97
70
  !my_class.nil?
98
71
  end
99
72
 
100
- ##
101
- ## Give me the size. This can be dangerous if you ever wrap an infinite enumerable
102
- ##
103
- def size
104
- inject(0) {|sum,each| sum + 1 }
105
- end
106
-
107
- def to_lazy
108
- self
109
- end
110
-
111
73
  def to_real
112
74
  _collect(PLACEBO)
113
75
  end
114
76
 
115
- ##
116
- ## Create the lazy iterators when no block is passed in the methods
117
- ## below
118
- ##
119
- iterator_creator :lazy_select, :lazy_collect, :lazy_reject, :lazy_detect, :lazy_each
120
-
121
77
  end
122
78
 
123
79
  end
@@ -1,4 +1,5 @@
1
1
  require 'lazy/extensions'
2
+ require 'lazy/abstract_enumerable'
2
3
  require 'lazy/lazy_enumerable'
3
4
  require 'lazy/collect_enumerable'
4
5
  require 'lazy/reject_enumerable'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: LazyEnumerable
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
  - Blaine Buxton
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-06-20 00:00:00 -05:00
12
+ date: 2008-06-21 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -26,6 +26,7 @@ files:
26
26
  - tests/lazy_enumerable_test.rb
27
27
  - tests/lazy_enumerable_test_suite.rb
28
28
  - lib/lazy
29
+ - lib/lazy/abstract_enumerable.rb
29
30
  - lib/lazy/collect_enumerable.rb
30
31
  - lib/lazy/extensions.rb
31
32
  - lib/lazy/iterator.rb