rubysl-generator 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d6014863fdbc341360f1d7fb1c3cb3ad80360c83
4
+ data.tar.gz: 01580cf4e9f8514e2f782fbb0c23129db1bdbef7
5
+ SHA512:
6
+ metadata.gz: aa8845c6b81d059d7c4bd7e649fed58d75218d8d070ccfaf81b5e469b9e3f7ff4d6aab699ff9f51e463982d09511b8eb4f9dd9e2cf05eca6f88ef653a899b69e
7
+ data.tar.gz: 9a3eb3afc2a2d6a9f9fe7f62ccd2ab135d97d19c0ed03235b391034b8a85dad5d18bf54997fc61f8862f0a28e20addff464572d419823649c8cac618664be8f6
data/.gitignore CHANGED
@@ -15,4 +15,3 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
- .rbx
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ before_install:
3
+ - gem update --system
4
+ - gem --version
5
+ - gem install rubysl-bundler
6
+ script: bundle exec mspec spec
7
+ rvm:
8
+ - rbx-nightly-18mode
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # RubySL::Generator
1
+ # Rubysl::Generator
2
2
 
3
3
  TODO: Write a gem description
4
4
 
@@ -24,6 +24,6 @@ TODO: Write usage instructions here
24
24
 
25
25
  1. Fork it
26
26
  2. Create your feature branch (`git checkout -b my-new-feature`)
27
- 3. Commit your changes (`git commit -am 'Added some feature'`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
28
  4. Push to the branch (`git push origin my-new-feature`)
29
29
  5. Create new Pull Request
data/Rakefile CHANGED
@@ -1,2 +1 @@
1
- #!/usr/bin/env rake
2
1
  require "bundler/gem_tasks"
@@ -0,0 +1 @@
1
+ require "rubysl/generator"
@@ -0,0 +1,2 @@
1
+ require "rubysl/generator/generator"
2
+ require "rubysl/generator/version"
@@ -0,0 +1,241 @@
1
+ #!/usr/bin/env ruby
2
+ #--
3
+ # $Idaemons: /home/cvs/rb/generator.rb,v 1.8 2001/10/03 08:54:32 knu Exp $
4
+ # $RoughId: generator.rb,v 1.10 2003/10/14 19:36:58 knu Exp $
5
+ # $Id: generator.rb 15954 2008-04-10 10:52:50Z knu $
6
+ #++
7
+ #
8
+ # = generator.rb: convert an internal iterator to an external one
9
+ #
10
+ # Copyright (c) 2001,2003 Akinori MUSHA <knu@iDaemons.org>
11
+ #
12
+ # All rights reserved. You can redistribute and/or modify it under
13
+ # the same terms as Ruby.
14
+ #
15
+ # == Overview
16
+ #
17
+ # This library provides the Generator class, which converts an
18
+ # internal iterator (i.e. an Enumerable object) to an external
19
+ # iterator. In that form, you can roll many iterators independently.
20
+ #
21
+ # The SyncEnumerator class, which is implemented using Generator,
22
+ # makes it easy to roll many Enumerable objects synchronously.
23
+ #
24
+ # See the respective classes for examples of usage.
25
+
26
+
27
+ #
28
+ # Generator converts an internal iterator (i.e. an Enumerable object)
29
+ # to an external iterator.
30
+ #
31
+ # Note that it is not very fast since it is implemented using
32
+ # continuations, which are currently slow.
33
+ #
34
+ # == Example
35
+ #
36
+ # require 'generator'
37
+ #
38
+ # # Generator from an Enumerable object
39
+ # g = Generator.new(['A', 'B', 'C', 'Z'])
40
+ #
41
+ # while g.next?
42
+ # puts g.next
43
+ # end
44
+ #
45
+ # # Generator from a block
46
+ # g = Generator.new { |g|
47
+ # for i in 'A'..'C'
48
+ # g.yield i
49
+ # end
50
+ #
51
+ # g.yield 'Z'
52
+ # }
53
+ #
54
+ # # The same result as above
55
+ # while g.next?
56
+ # puts g.next
57
+ # end
58
+ #
59
+ class Generator
60
+ include Enumerable
61
+
62
+ # Creates a new generator either from an Enumerable object or from a
63
+ # block.
64
+ #
65
+ # In the former, block is ignored even if given.
66
+ #
67
+ # In the latter, the given block is called with the generator
68
+ # itself, and expected to call the +yield+ method for each element.
69
+ def initialize(enum=nil, &block)
70
+ if enum
71
+ @block = proc do |g|
72
+ enum.each { |x| g.yield x }
73
+ end
74
+ else
75
+ @block = block
76
+ end
77
+
78
+ @index = 0
79
+ @queue = []
80
+
81
+ @done = false
82
+
83
+ @fiber = Rubinius::Fiber.new do
84
+ @block.call(self)
85
+ @done = true
86
+ @fiber = nil
87
+ end
88
+
89
+ self
90
+ end
91
+
92
+ # Yields an element to the generator.
93
+ def yield(value)
94
+ # Running inside @fiber
95
+ @queue << value
96
+ Rubinius::Fiber.yield
97
+
98
+ self
99
+ end
100
+
101
+ # Returns true if the generator has reached the end.
102
+ def end?
103
+ return true if @done
104
+ return false unless @queue.empty?
105
+
106
+ # Turn the loop over and see if we hit the end
107
+ @fiber.resume
108
+
109
+ return @done
110
+ end
111
+
112
+ # Returns true if the generator has not reached the end yet.
113
+ def next?
114
+ !end?
115
+ end
116
+
117
+ # Returns the current index (position) counting from zero.
118
+ def index
119
+ @index
120
+ end
121
+
122
+ # Returns the current index (position) counting from zero.
123
+ def pos
124
+ @index
125
+ end
126
+
127
+ # Returns the element at the current position and moves forward.
128
+ def next
129
+ raise EOFError, "end of iteration hit" if end?
130
+
131
+ @fiber.resume if @queue.empty?
132
+
133
+ # We've driven the fiber ahead and least once now, so we should
134
+ # have values if there are values to be had
135
+
136
+ unless @queue.empty?
137
+ @index += 1
138
+ return @queue.shift
139
+ end
140
+
141
+ raise EOFError, "end of iteration hit"
142
+ end
143
+
144
+ # Returns the element at the current position.
145
+ def current
146
+ if @queue.empty?
147
+ raise EOFError, "no more elements available"
148
+ end
149
+
150
+ @queue.first
151
+ end
152
+
153
+ # Rewinds the generator.
154
+ def rewind
155
+ initialize(nil, &@block) if @index.nonzero?
156
+
157
+ self
158
+ end
159
+
160
+ # Rewinds the generator and enumerates the elements.
161
+ def each
162
+ rewind
163
+
164
+ until end?
165
+ yield self.next
166
+ end
167
+
168
+ self
169
+ end
170
+ end
171
+
172
+ #
173
+ # SyncEnumerator creates an Enumerable object from multiple Enumerable
174
+ # objects and enumerates them synchronously.
175
+ #
176
+ # == Example
177
+ #
178
+ # require 'generator'
179
+ #
180
+ # s = SyncEnumerator.new([1,2,3], ['a', 'b', 'c'])
181
+ #
182
+ # # Yields [1, 'a'], [2, 'b'], and [3,'c']
183
+ # s.each { |row| puts row.join(', ') }
184
+ #
185
+ class SyncEnumerator
186
+ include Enumerable
187
+
188
+ # Creates a new SyncEnumerator which enumerates rows of given
189
+ # Enumerable objects.
190
+ def initialize(*enums)
191
+ @gens = enums.map { |e| Generator.new(e) }
192
+ end
193
+
194
+ # Returns the number of enumerated Enumerable objects, i.e. the size
195
+ # of each row.
196
+ def size
197
+ @gens.size
198
+ end
199
+
200
+ # Returns the number of enumerated Enumerable objects, i.e. the size
201
+ # of each row.
202
+ def length
203
+ @gens.length
204
+ end
205
+
206
+ # Returns true if the given nth Enumerable object has reached the
207
+ # end. If no argument is given, returns true if any of the
208
+ # Enumerable objects has reached the end.
209
+ def end?(i = nil)
210
+ if i.nil?
211
+ @gens.detect { |g| g.end? } ? true : false
212
+ else
213
+ @gens[i].end?
214
+ end
215
+ end
216
+
217
+ # Enumerates rows of the Enumerable objects.
218
+ def each
219
+ @gens.each { |g| g.rewind }
220
+
221
+ while true
222
+ count = 0
223
+
224
+ ret = @gens.map { |g|
225
+ if g.end?
226
+ count += 1
227
+ nil
228
+ else
229
+ g.next
230
+ end
231
+ }
232
+
233
+ break if count == @gens.size
234
+
235
+ yield ret
236
+ end
237
+
238
+ self
239
+ end
240
+ end
241
+
@@ -1,5 +1,5 @@
1
1
  module RubySL
2
2
  module Generator
3
- VERSION = "0.0.1"
3
+ VERSION = "1.0.0"
4
4
  end
5
5
  end
@@ -1,22 +1,23 @@
1
- # -*- encoding: utf-8 -*-
2
- require File.expand_path('../lib/rubysl-generator/version', __FILE__)
1
+ # coding: utf-8
2
+ require './lib/rubysl/generator/version'
3
3
 
4
- Gem::Specification.new do |gem|
5
- gem.authors = ["Brian Shirai"]
6
- gem.email = ["brixen@gmail.com"]
7
- gem.description = %q{Ruby Standard Library - generator}
8
- gem.summary = %q{Ruby Standard Library - generator}
9
- gem.homepage = ""
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "rubysl-generator"
6
+ spec.version = RubySL::Generator::VERSION
7
+ spec.authors = ["Brian Shirai"]
8
+ spec.email = ["brixen@gmail.com"]
9
+ spec.description = %q{Ruby standard library generator.}
10
+ spec.summary = %q{Ruby standard library generator.}
11
+ spec.homepage = "https://github.com/rubysl/rubysl-generator"
12
+ spec.license = "BSD"
10
13
 
11
- gem.files = `git ls-files`.split($\)
12
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
- gem.name = "rubysl-generator"
15
- gem.require_paths = ["lib"]
16
- gem.version = RubySL::Generator::VERSION
14
+ spec.files = `git ls-files`.split($/)
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ["lib"]
17
18
 
18
- gem.add_runtime_dependency "redcard", "~> 1.0"
19
-
20
- gem.add_development_dependency "rake", "~> 10.0"
21
- gem.add_development_dependency "mspec", "~> 1.5"
19
+ spec.add_development_dependency "bundler", "~> 1.3"
20
+ spec.add_development_dependency "rake", "~> 10.0"
21
+ spec.add_development_dependency "mspec", "~> 1.5"
22
+ spec.add_development_dependency "rubysl-prettyprint", "~> 1.0"
22
23
  end
@@ -0,0 +1,31 @@
1
+ ruby_version_is ""..."1.9" do
2
+ with_feature :generator do
3
+
4
+ require File.expand_path('../fixtures/common', __FILE__)
5
+
6
+ describe "Generator#each" do
7
+ it "enumerates the elements" do
8
+ g = GeneratorSpecs.four_elems
9
+ result = []
10
+
11
+ g.each { |element|
12
+ result << element
13
+ }
14
+
15
+ result.should == ['A', 'B', 'C', 'Z']
16
+ end
17
+
18
+ it "rewinds the generator and only then enumerates the elements" do
19
+ g = GeneratorSpecs.four_elems
20
+ g.next; g.next
21
+ result = []
22
+
23
+ g.each { |element|
24
+ result << element
25
+ }
26
+
27
+ result.should == ['A', 'B', 'C', 'Z']
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,21 @@
1
+ not_supported_on :ironruby do
2
+ require 'generator'
3
+ end
4
+
5
+ module GeneratorSpecs
6
+ def self.empty
7
+ Generator.new([])
8
+ end
9
+
10
+ def self.one_elem
11
+ Generator.new([1])
12
+ end
13
+
14
+ def self.two_elems
15
+ Generator.new([1, 2])
16
+ end
17
+
18
+ def self.four_elems
19
+ Generator.new(['A', 'B', 'C', 'Z'])
20
+ end
21
+ end
@@ -0,0 +1,24 @@
1
+ ruby_version_is ""..."1.9" do
2
+ with_feature :generator do
3
+ require 'generator'
4
+ describe "Generator.new" do
5
+ it "creates a new generator from an Enumerable object" do
6
+ g = Generator.new(['A', 'B', 'C', 'Z'])
7
+ g.should_not == nil
8
+ g.kind_of?(Generator).should == true
9
+ end
10
+
11
+ it "creates a new generator from a block" do
12
+ g = Generator.new { |g|
13
+ for i in 'A'..'C'
14
+ g.yield i
15
+ end
16
+ g.yield 'Z'
17
+ }
18
+
19
+ g.should_not == nil
20
+ g.kind_of?(Generator).should == true
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,77 @@
1
+ ruby_version_is ""..."1.9" do
2
+ with_feature :generator do
3
+ require File.expand_path('../fixtures/common', __FILE__)
4
+
5
+ describe "Generator#next?" do
6
+ it "returns false for empty generator" do
7
+ GeneratorSpecs.empty.next?.should == false
8
+ end
9
+
10
+ it "returns true for non-empty generator" do
11
+ g = Generator.new([1])
12
+ g.next?.should == true
13
+
14
+ GeneratorSpecs.two_elems.next?.should == true
15
+
16
+ g = Generator.new(['A', 'B', 'C', 'D', 'E', 'F'])
17
+ g.next?.should == true
18
+ end
19
+
20
+ it "returns true if the generator has not reached the end yet" do
21
+ g = GeneratorSpecs.two_elems
22
+ g.next
23
+ g.next?.should == true
24
+ end
25
+
26
+ it "returns false if the generator has reached the end" do
27
+ g = GeneratorSpecs.two_elems
28
+ g.next
29
+ g.next
30
+ g.next?.should == false
31
+ end
32
+
33
+ it "returns false if end? returns true" do
34
+ g = GeneratorSpecs.two_elems
35
+ def g.end?; true end
36
+ g.next?.should == false
37
+ end
38
+ end
39
+
40
+ describe "Generator#next" do
41
+ it "raises an EOFError on empty generator" do
42
+ lambda { GeneratorSpecs.empty.next }.should raise_error(EOFError)
43
+ end
44
+
45
+ it "raises an EOFError if no elements available" do
46
+ g = GeneratorSpecs.two_elems
47
+ g.next;
48
+ g.next
49
+ lambda { g.next }.should raise_error(EOFError)
50
+ end
51
+
52
+ it "raises an EOFError if end? returns true" do
53
+ g = GeneratorSpecs.two_elems
54
+ def g.end?; true end
55
+ lambda { g.next }.should raise_error(EOFError)
56
+ end
57
+
58
+ it "returns the element at the current position and moves forward" do
59
+ g = GeneratorSpecs.two_elems
60
+ g.index.should == 0
61
+ g.next.should == 1
62
+ g.index.should == 1
63
+ end
64
+
65
+ it "subsequent calls should return all elements in proper order" do
66
+ g = GeneratorSpecs.four_elems
67
+
68
+ result = []
69
+ while g.next?
70
+ result << g.next
71
+ end
72
+
73
+ result.should == ['A', 'B', 'C', 'Z']
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,32 @@
1
+ ruby_version_is ""..."1.9" do
2
+ with_feature :generator do
3
+ require File.expand_path('../fixtures/common', __FILE__)
4
+
5
+ describe "Generator#rewind" do
6
+ it "does nothing for empty generator" do
7
+ g = GeneratorSpecs.empty
8
+ g.index.should == 0
9
+ g.rewind
10
+ g.index.should == 0
11
+ end
12
+
13
+ it "rewinds the generator" do
14
+ g = GeneratorSpecs.two_elems
15
+ orig = g.next
16
+ g.index.should == 1
17
+ g.rewind
18
+ g.index.should == 0
19
+ g.next.should == orig
20
+ end
21
+
22
+ it "rewinds the previously finished generator" do
23
+ g = GeneratorSpecs.two_elems
24
+ g.next; g.next
25
+ g.rewind
26
+ g.end?.should == false
27
+ g.next?.should == true
28
+ g.next.should == 1
29
+ end
30
+ end
31
+ end
32
+ end
metadata CHANGED
@@ -1,118 +1,121 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: rubysl-generator
3
- version: !ruby/object:Gem::Version
4
- hash: 856480538658449761
5
- prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 1
10
- version: 0.0.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Brian Shirai
14
8
  autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
-
18
- date: 2013-04-15 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
21
- name: redcard
11
+ date: 2013-12-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
22
21
  prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- none: false
25
- requirements:
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
26
24
  - - ~>
27
- - !ruby/object:Gem::Version
28
- hash: 4428665182548103036
29
- segments:
30
- - 1
31
- - 0
32
- version: "1.0"
33
- type: :runtime
34
- version_requirements: *id001
35
- - !ruby/object:Gem::Dependency
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
36
28
  name: rake
37
- prerelease: false
38
- requirement: &id002 !ruby/object:Gem::Requirement
39
- none: false
40
- requirements:
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
41
31
  - - ~>
42
- - !ruby/object:Gem::Version
43
- hash: 1510892033553700768
44
- segments:
45
- - 10
46
- - 0
47
- version: "10.0"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
48
34
  type: :development
49
- version_requirements: *id002
50
- - !ruby/object:Gem::Dependency
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
51
42
  name: mspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
52
49
  prerelease: false
53
- requirement: &id003 !ruby/object:Gem::Requirement
54
- none: false
55
- requirements:
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubysl-prettyprint
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
56
59
  - - ~>
57
- - !ruby/object:Gem::Version
58
- hash: 1660815245844205030
59
- segments:
60
- - 1
61
- - 5
62
- version: "1.5"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
63
62
  type: :development
64
- version_requirements: *id003
65
- description: Ruby Standard Library - generator
66
- email:
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ description: Ruby standard library generator.
70
+ email:
67
71
  - brixen@gmail.com
68
72
  executables: []
69
-
70
73
  extensions: []
71
-
72
74
  extra_rdoc_files: []
73
-
74
- files:
75
+ files:
75
76
  - .gitignore
77
+ - .travis.yml
76
78
  - Gemfile
77
79
  - LICENSE
78
80
  - README.md
79
81
  - Rakefile
80
- - lib/rubysl-generator.rb
81
- - lib/rubysl-generator/version.rb
82
+ - lib/generator.rb
83
+ - lib/rubysl/generator.rb
84
+ - lib/rubysl/generator/generator.rb
85
+ - lib/rubysl/generator/version.rb
82
86
  - rubysl-generator.gemspec
83
- homepage: ""
84
- licenses: []
85
-
87
+ - spec/each_spec.rb
88
+ - spec/fixtures/common.rb
89
+ - spec/new_spec.rb
90
+ - spec/next_spec.rb
91
+ - spec/rewind_spec.rb
92
+ homepage: https://github.com/rubysl/rubysl-generator
93
+ licenses:
94
+ - BSD
95
+ metadata: {}
86
96
  post_install_message:
87
97
  rdoc_options: []
88
-
89
- require_paths:
98
+ require_paths:
90
99
  - lib
91
- required_ruby_version: !ruby/object:Gem::Requirement
92
- none: false
93
- requirements:
94
- - - ">="
95
- - !ruby/object:Gem::Version
96
- hash: 2002549777813010636
97
- segments:
98
- - 0
99
- version: "0"
100
- required_rubygems_version: !ruby/object:Gem::Requirement
101
- none: false
102
- requirements:
103
- - - ">="
104
- - !ruby/object:Gem::Version
105
- hash: 2002549777813010636
106
- segments:
107
- - 0
108
- version: "0"
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
109
110
  requirements: []
110
-
111
111
  rubyforge_project:
112
- rubygems_version: 1.8.25
112
+ rubygems_version: 2.0.7
113
113
  signing_key:
114
- specification_version: 3
115
- summary: Ruby Standard Library - generator
116
- test_files: []
117
-
118
- has_rdoc:
114
+ specification_version: 4
115
+ summary: Ruby standard library generator.
116
+ test_files:
117
+ - spec/each_spec.rb
118
+ - spec/fixtures/common.rb
119
+ - spec/new_spec.rb
120
+ - spec/next_spec.rb
121
+ - spec/rewind_spec.rb
@@ -1,7 +0,0 @@
1
- require "rubysl-generator/version"
2
-
3
- module RubySL
4
- module Generator
5
- # Your code goes here...
6
- end
7
- end