status_enumerator 0.0.2 → 0.0.3
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.
- data/VERSION +1 -1
- data/lib/status_enumerator.rb +51 -38
- data/spec/spec_helper.rb +21 -0
- data/spec/status_enumerator_spec.rb +6 -0
- data/spec/status_enumerator_status_spec.rb +241 -0
- data/status_enumerator.gemspec +3 -2
- metadata +5 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.3
|
data/lib/status_enumerator.rb
CHANGED
@@ -3,55 +3,68 @@ class StatusEnumerator
|
|
3
3
|
raise ArgumentError, '%s is not redpond to #each' % target.class.name if target.nil? or !target.respond_to?(:each)
|
4
4
|
@target = target
|
5
5
|
end
|
6
|
-
|
7
|
-
def each
|
8
|
-
|
9
|
-
c, stat = 0, Status.new
|
10
|
-
@target.each do |i|
|
11
|
-
stat.send(:put_next, i)
|
12
|
-
c += 1
|
13
|
-
if c == 3
|
14
|
-
n = stat.send(:put_prev)
|
15
|
-
stat.send(:set_flags, true, false)
|
16
|
-
yield stat
|
17
|
-
stat.send(:put_next, n)
|
18
|
-
yield stat
|
19
|
-
elsif c > 2
|
20
|
-
yield stat
|
21
|
-
end
|
22
|
-
end.tap do
|
23
|
-
if c > 0
|
24
|
-
case c
|
25
|
-
when 1
|
26
|
-
stat.send(:put_next)
|
27
|
-
stat.send(:set_flags, true, true)
|
28
|
-
yield stat
|
29
|
-
when 2
|
30
|
-
stat.send(:set_flags, true, false)
|
31
|
-
yield stat
|
32
|
-
stat.send(:put_next)
|
33
|
-
stat.send(:set_flags, false, true)
|
34
|
-
yield stat
|
35
|
-
else
|
36
|
-
stat.send(:put_next)
|
37
|
-
stat.send(:set_flags, false, true)
|
38
|
-
yield stat
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
6
|
+
|
7
|
+
def each(&block)
|
8
|
+
status_class.send(:new, nil, &block).send(:each_status, @target)
|
42
9
|
end
|
43
10
|
|
44
|
-
|
11
|
+
def status_class; Status end
|
12
|
+
|
13
|
+
class Status
|
14
|
+
class <<self
|
15
|
+
private :new
|
16
|
+
end
|
17
|
+
|
45
18
|
attr_reader :current, :prev, :next
|
46
19
|
def first?; !!@first_p end
|
47
20
|
def last?; !!@last_p end
|
48
21
|
|
49
22
|
private
|
50
23
|
|
51
|
-
def initialize
|
24
|
+
def initialize(owner, &block)
|
25
|
+
raise ArgumentError, '%s is not kind of StatusEnumerator::Status' % owner.inspect unless owner.nil? or owner.kind_of?(StatusEnumerator::Status)
|
26
|
+
raise ArgumentError, 'block not given' if owner.nil? and block.nil?
|
27
|
+
@owner, @block = owner, block || owner.instance_variable_get(:@block)
|
28
|
+
|
52
29
|
@prev = @current = @next = nil
|
53
30
|
@first_p = @last_p = true
|
54
31
|
end
|
32
|
+
|
33
|
+
def each_status(enum)
|
34
|
+
c = 0
|
35
|
+
enum.each do |i|
|
36
|
+
put_next i
|
37
|
+
c += 1
|
38
|
+
if c == 3
|
39
|
+
n = put_prev
|
40
|
+
set_flags true, false
|
41
|
+
@block.call(self)
|
42
|
+
put_next n
|
43
|
+
@block.call(self)
|
44
|
+
elsif c > 2
|
45
|
+
@block.call(self)
|
46
|
+
end
|
47
|
+
end.tap do
|
48
|
+
if c > 0
|
49
|
+
case c
|
50
|
+
when 1
|
51
|
+
put_next
|
52
|
+
set_flags true, true
|
53
|
+
@block.call(self)
|
54
|
+
when 2
|
55
|
+
set_flags true, false
|
56
|
+
@block.call(self)
|
57
|
+
put_next
|
58
|
+
set_flags false, true
|
59
|
+
@block.call(self)
|
60
|
+
else
|
61
|
+
put_next
|
62
|
+
set_flags false, true
|
63
|
+
@block.call(self)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
55
68
|
|
56
69
|
def put_next(obj = nil)
|
57
70
|
_prev, @prev, @current, @next = @prev, @current, @next, obj
|
data/spec/spec_helper.rb
CHANGED
@@ -10,3 +10,24 @@ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
|
10
10
|
RSpec.configure do |config|
|
11
11
|
|
12
12
|
end
|
13
|
+
|
14
|
+
class Foo
|
15
|
+
def initialize(value, *args)
|
16
|
+
@value, @children = value, args
|
17
|
+
end
|
18
|
+
attr_reader :value, :children
|
19
|
+
|
20
|
+
def self.conv(ary, &block)
|
21
|
+
ret = ary.map {|x| x.kind_of?(self) ? x.value : x }
|
22
|
+
ret = block.call(ret) if block
|
23
|
+
ary.each_with_index do |obj, i|
|
24
|
+
if obj.kind_of?(self)
|
25
|
+
o = conv(obj.children, &block)
|
26
|
+
o.unshift ret[i]
|
27
|
+
ret[i] = o
|
28
|
+
end
|
29
|
+
end
|
30
|
+
ret.flatten!
|
31
|
+
ret
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,241 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe StatusEnumerator::Status do
|
4
|
+
def status_new(*args, &block); StatusEnumerator::Status.send(:new, *args, &block) end
|
5
|
+
|
6
|
+
it 'is Class' do
|
7
|
+
StatusEnumerator::Status.should be_instance_of(Class)
|
8
|
+
end
|
9
|
+
|
10
|
+
describe '.new(owner, &block)' do
|
11
|
+
it 'is private' do
|
12
|
+
StatusEnumerator::Status.tap do |x|
|
13
|
+
x.should be_respond_to(:new, true)
|
14
|
+
x.should_not be_respond_to(:new)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'stores owner in @owner' do
|
19
|
+
owner = status_new(nil) {}
|
20
|
+
status_new(owner) {}.instance_variable_get(:@owner).should == owner
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'permits owner of nil' do
|
24
|
+
lambda { status_new(nil) {} }.should_not raise_error
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'when owner is not kind of Status, it raises an ArgumentError' do
|
28
|
+
lambda { status_new(:symbol) {} }.should raise_error(ArgumentError)
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'stores block in @block' do
|
32
|
+
func = lambda {|e| e == :ok }
|
33
|
+
status_new(nil, &func).instance_variable_get(:@block).should == func
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'in case of owner is not nil and block is nil, it store @block of owner in @block' do
|
37
|
+
func = lambda {|e| e == :ok }
|
38
|
+
status_new(status_new(nil, &func)).instance_variable_get(:@block).should == func
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'in the case of owner and block is nil, it raises an ArgumentError' do
|
42
|
+
lambda { status_new(nil) }.should raise_error(ArgumentError)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe '(#each_status(enum))' do
|
47
|
+
before do
|
48
|
+
@count = 0
|
49
|
+
@call = nil
|
50
|
+
@each = status_new(nil) do |e|
|
51
|
+
@call.call(e) if @call
|
52
|
+
@count += 1
|
53
|
+
end
|
54
|
+
end
|
55
|
+
def do_each(enum); @each.send(:each_status, enum) end
|
56
|
+
|
57
|
+
it 'calls a block set by @block' do
|
58
|
+
flag = false
|
59
|
+
@each.instance_variable_set(:@block, lambda { flag = true })
|
60
|
+
do_each([1,2,3])
|
61
|
+
flag.should == true
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'in the case of 0, a number of element of enum does not call a block' do
|
65
|
+
do_each([])
|
66
|
+
@count.should == 0
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'calls an element of enum once' do
|
70
|
+
obj = [:dfa, 1532, /reg/, 'sample', Enumerable, 55.5]
|
71
|
+
do_each(obj)
|
72
|
+
@count.should == obj.size
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe do
|
77
|
+
before :all do
|
78
|
+
@enum_single = [1]
|
79
|
+
@enum_double = [156.3, :ee]
|
80
|
+
@enum_triple = ['sam', /ple/, true]
|
81
|
+
@enum_many = [:a, 2, 3.0, 'd', /e/, true, nil, false, 'sample', 10]
|
82
|
+
end
|
83
|
+
before do
|
84
|
+
@count, @result, @call = 0, [], lambda { }
|
85
|
+
@each = status_new(nil) do |e|
|
86
|
+
@result.push @call.call(e)
|
87
|
+
@count += 1
|
88
|
+
end
|
89
|
+
end
|
90
|
+
def do_each(enum)
|
91
|
+
@each.send(:each_status, enum)
|
92
|
+
@count.should == enum.size
|
93
|
+
@result
|
94
|
+
end
|
95
|
+
|
96
|
+
describe '#first?' do
|
97
|
+
before do
|
98
|
+
@call = lambda {|x| x.first? }
|
99
|
+
end
|
100
|
+
def make_result(enum)
|
101
|
+
Array.new(enum.size, false).tap {|x| x[0] = true }
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'initalized true' do
|
105
|
+
@each.first?.should == true
|
106
|
+
end
|
107
|
+
|
108
|
+
it 'puts back true to only the first value(single ary)' do
|
109
|
+
do_each(@enum_single).should == [true]
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'puts back true to only the first value(double ary)' do
|
113
|
+
do_each(@enum_double).should == make_result(@enum_double)
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'puts back true to only the first value(triple ary)' do
|
117
|
+
do_each(@enum_triple).should == make_result(@enum_triple)
|
118
|
+
end
|
119
|
+
|
120
|
+
it 'puts back true to only the first value(many ary)' do
|
121
|
+
do_each(@enum_many).should == make_result(@enum_many)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
describe '#last?' do
|
126
|
+
before do
|
127
|
+
@call = lambda {|x| x.last? }
|
128
|
+
end
|
129
|
+
def make_result(enum)
|
130
|
+
Array.new(enum.size, false).tap {|x| x[-1] = true }
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'initalized true' do
|
134
|
+
@each.last?.should == true
|
135
|
+
end
|
136
|
+
|
137
|
+
it 'puts back true to only the last value(single ary)' do
|
138
|
+
do_each(@enum_single).should == [true]
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'puts back true to only the last value(double ary)' do
|
142
|
+
do_each(@enum_double).should == make_result(@enum_double)
|
143
|
+
end
|
144
|
+
|
145
|
+
it 'puts back true to only the last value(triple ary)' do
|
146
|
+
do_each(@enum_triple).should == make_result(@enum_triple)
|
147
|
+
end
|
148
|
+
|
149
|
+
it 'puts back true to only the last value(many ary)' do
|
150
|
+
do_each(@enum_many).should == make_result(@enum_many)
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
describe '#current' do
|
155
|
+
before do
|
156
|
+
@call = lambda {|x| x.current }
|
157
|
+
end
|
158
|
+
def make_result(enum)
|
159
|
+
enum
|
160
|
+
end
|
161
|
+
|
162
|
+
it 'initalized nil' do
|
163
|
+
@each.current.should be_nil
|
164
|
+
end
|
165
|
+
|
166
|
+
it 'puts back true to only the current value(single ary)' do
|
167
|
+
do_each(@enum_single).should == make_result(@enum_single)
|
168
|
+
end
|
169
|
+
|
170
|
+
it 'puts back true to only the current value(double ary)' do
|
171
|
+
do_each(@enum_double).should == make_result(@enum_double)
|
172
|
+
end
|
173
|
+
|
174
|
+
it 'puts back true to only the current value(triple ary)' do
|
175
|
+
do_each(@enum_triple).should == make_result(@enum_triple)
|
176
|
+
end
|
177
|
+
|
178
|
+
it 'puts back true to only the current value(many ary)' do
|
179
|
+
do_each(@enum_many).should == make_result(@enum_many)
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
describe '#prev' do
|
184
|
+
before do
|
185
|
+
@call = lambda {|x| x.prev }
|
186
|
+
end
|
187
|
+
def make_result(enum)
|
188
|
+
enum.dup.tap {|x| x.pop; x.unshift nil }
|
189
|
+
end
|
190
|
+
|
191
|
+
it 'initalized nil' do
|
192
|
+
@each.prev.should be_nil
|
193
|
+
end
|
194
|
+
|
195
|
+
it 'puts back true to only the prev value(single ary)' do
|
196
|
+
do_each(@enum_single).should == make_result(@enum_single)
|
197
|
+
end
|
198
|
+
|
199
|
+
it 'puts back true to only the prev value(double ary)' do
|
200
|
+
do_each(@enum_double).should == make_result(@enum_double)
|
201
|
+
end
|
202
|
+
|
203
|
+
it 'puts back true to only the prev value(triple ary)' do
|
204
|
+
do_each(@enum_triple).should == make_result(@enum_triple)
|
205
|
+
end
|
206
|
+
|
207
|
+
it 'puts back true to only the prev value(many ary)' do
|
208
|
+
do_each(@enum_many).should == make_result(@enum_many)
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
describe '#next' do
|
213
|
+
before do
|
214
|
+
@call = lambda {|x| x.next }
|
215
|
+
end
|
216
|
+
def make_result(enum)
|
217
|
+
enum.dup.tap {|x| x.shift; x.push nil }
|
218
|
+
end
|
219
|
+
|
220
|
+
it 'initalized nil' do
|
221
|
+
@each.next.should be_nil
|
222
|
+
end
|
223
|
+
|
224
|
+
it 'puts back true to only the next value(single ary)' do
|
225
|
+
do_each(@enum_single).should == make_result(@enum_single)
|
226
|
+
end
|
227
|
+
|
228
|
+
it 'puts back true to only the next value(double ary)' do
|
229
|
+
do_each(@enum_double).should == make_result(@enum_double)
|
230
|
+
end
|
231
|
+
|
232
|
+
it 'puts back true to only the next value(triple ary)' do
|
233
|
+
do_each(@enum_triple).should == make_result(@enum_triple)
|
234
|
+
end
|
235
|
+
|
236
|
+
it 'puts back true to only the next value(many ary)' do
|
237
|
+
do_each(@enum_many).should == make_result(@enum_many)
|
238
|
+
end
|
239
|
+
end
|
240
|
+
end
|
241
|
+
end
|
data/status_enumerator.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{status_enumerator}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = [%q{Keiichiro Nishi}]
|
12
|
-
s.date = %q{2011-
|
12
|
+
s.date = %q{2011-06-19}
|
13
13
|
s.description = %q{This class provides an enumeration function to have the object which I added tree information to in an argument.
|
14
14
|
The instance receives an enumerable object and provides #each and #each_method. The #each method calls a block in an argument in own. The #each_method method calls the method of an object appointed own in an argument.
|
15
15
|
I have the information of the object equal to the ancestors in own and front and back and hierarchy structure, and a block and the argument handed to a method maintain the state flag in the enumeration again.
|
@@ -33,6 +33,7 @@ This class provides a function to enumerate it, but it is not the object which i
|
|
33
33
|
"lib/status_enumerator.rb",
|
34
34
|
"spec/spec_helper.rb",
|
35
35
|
"spec/status_enumerator_spec.rb",
|
36
|
+
"spec/status_enumerator_status_spec.rb",
|
36
37
|
"status_enumerator.gemspec"
|
37
38
|
]
|
38
39
|
s.homepage = %q{http://github.com/Ktouth/status_enumerator}
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: status_enumerator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Keiichiro Nishi
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-06-19 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
prerelease: false
|
@@ -122,6 +122,7 @@ files:
|
|
122
122
|
- lib/status_enumerator.rb
|
123
123
|
- spec/spec_helper.rb
|
124
124
|
- spec/status_enumerator_spec.rb
|
125
|
+
- spec/status_enumerator_status_spec.rb
|
125
126
|
- status_enumerator.gemspec
|
126
127
|
homepage: http://github.com/Ktouth/status_enumerator
|
127
128
|
licenses:
|