seq 0.2.0 → 0.3.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: 04b043d28893e71585895201f3a5d085e9cf84d0
4
+ data.tar.gz: ce20e14a0fdb7bd43662a4be84291448eaeb0fec
5
+ SHA512:
6
+ metadata.gz: 9475c6c9e3a0e41908b29333cd8b1f49addb0db38f8979f653f5762737f50b943a08dedf8266ecbeeb6e8f1db041e1fbdc432cd76fd3a2c0d3fbbea41d941703
7
+ data.tar.gz: 1273cf4658e717e3c0ea102b28274a66c8860035e4ee03c85c69bddf3f55777e06c6199f2e8a4617c8c49c603f6ae6c83749c9c7f6b900760878501648b6e605
data/README.md CHANGED
@@ -77,6 +77,8 @@ s.reject! {|i| i < 3 } #=> [3, 4]
77
77
  A subclass of Seq which returns random elements.
78
78
 
79
79
  ```ruby
80
+ require 'seq/random'
81
+
80
82
  r = Seq::Random.new([1,2,3], 5)
81
83
  r.entries #=> [2, 1, 1, 3, 2]
82
84
  r.entries #=> [1, 3, 2, 3, 3]
@@ -87,6 +89,8 @@ r.entries #=> [1, 3, 2, 3, 3]
87
89
  Lazily evaluates a block using starting list given.
88
90
 
89
91
  ```ruby
92
+ require 'seq/lazy'
93
+
90
94
  fibs = Seq::Lazy.new([1,1]) {|list| list[-1] + list[-2] }
91
95
  fibs.take(10) #=> [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
92
96
  fibs.take(20) #=> [89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040]
@@ -100,6 +104,8 @@ fibs.reset
100
104
  Returns elements from a page one at a time.
101
105
 
102
106
  ```ruby
107
+ require 'seq/paged'
108
+
103
109
  s = Seq::Paged.new {|page| [page, page+1, page+2] }
104
110
  s.take(10) #=> [0, 1, 2, 1, 2, 3, 2, 3, 4, 3]
105
111
  # extra spacing added to show pages
data/lib/seq.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'seq/version'
2
+
1
3
  unless defined?(Float::INFINITY)
2
4
  # Define Float::INFINITY if not previously defined
3
5
  Float::INFINITY = 1.0/0
@@ -131,8 +133,3 @@ class Seq
131
133
  end
132
134
  end
133
135
  end
134
-
135
- require 'seq/lazy'
136
- require 'seq/paged'
137
- require 'seq/random'
138
- require 'seq/version'
@@ -1,3 +1,5 @@
1
+ require 'seq'
2
+
1
3
  class Seq
2
4
 
3
5
  # Paged seqs evaluate a block that returns a page (Array) of items that are
@@ -51,6 +53,15 @@ class Seq
51
53
  end
52
54
  end
53
55
 
56
+ # Iterates over each item as returned by #next until #ended?.
57
+ def each
58
+ loop do
59
+ val = self.next
60
+ return if self.ended?
61
+ yield val
62
+ end
63
+ end
64
+
54
65
  # @return Whether the Paged seq has returned all of its items.
55
66
  def ended?
56
67
  @index >= @items.size && @done
@@ -75,7 +86,5 @@ class Seq
75
86
  @index += 1
76
87
  item
77
88
  end
78
-
79
-
80
89
  end
81
90
  end
@@ -0,0 +1,190 @@
1
+ require 'seq'
2
+
3
+ class Seq
4
+
5
+ # Takes a list of items and returns them in pages. This can be thought of as
6
+ # doing the opposite of {Seq::Paged}.
7
+ #
8
+ # @example
9
+ #
10
+ # pages = Seq::Paged.new(5, 1..100)
11
+ #
12
+ # pages.next #=> [1,2,3,4,5]
13
+ # pages.next #=> [6,7,8,9,10]
14
+ # pages.prev #=> [1,2,3,4,5]
15
+ # pages.page #=> 0
16
+ # pages.first? #=> true
17
+ # pages.last #=> [96,97,98,99,100]
18
+ #
19
+ class Pager < Seq
20
+
21
+ # Creates a new instance of Pager.
22
+ #
23
+ # @param page_size [Integer] Size of page to use
24
+ # @param start_page [Integer] Page to start at
25
+ # @param elements [Enumerable] Items to initialize with
26
+ def initialize(page_size, start_page=nil, elements)
27
+ @pages = elements.each_slice(page_size).to_a
28
+ @curr = start_page || 0
29
+ end
30
+
31
+ # Sets the current page. If the number passed is out of range, it will be
32
+ # set to within the bound of available pages.
33
+ #
34
+ # @param num [#to_i]
35
+ def page=(num)
36
+ unless num.respond_to?(:to_i)
37
+ raise ArgumentError.new("cannot call #to_i on argument")
38
+ end
39
+
40
+ num = num.to_i
41
+ num = 0 if num < 0
42
+ num = pages - 1 if num >= pages
43
+
44
+ @curr = num
45
+ end
46
+
47
+ # @return [Integer] The index of the current page.
48
+ def page
49
+ @curr
50
+ end
51
+
52
+ # @return [Integer] The total number of pages.
53
+ def pages
54
+ @pages.size
55
+ end
56
+
57
+ # @return Whether currently at the first page.
58
+ def first?
59
+ page == 0
60
+ end
61
+
62
+ # Moves to the first page.
63
+ #
64
+ # @return [Seq::Pager]
65
+ def first!
66
+ @curr = 0
67
+ self
68
+ end
69
+
70
+ # Moves to the first page, and returns it.
71
+ #
72
+ # @return [Array]
73
+ def first
74
+ @curr = 0
75
+ curr
76
+ end
77
+
78
+ # @return Whether currently at the last page.
79
+ def last?
80
+ page == pages - 1
81
+ end
82
+
83
+ # Moves to the last page.
84
+ #
85
+ # @return [Seq::Pager]
86
+ def last!
87
+ @curr = 0
88
+ self
89
+ end
90
+
91
+ # Moves to the last page, and returns it.
92
+ #
93
+ # @return [Array]
94
+ def last
95
+ @curr = pages - 1
96
+ curr
97
+ end
98
+
99
+ # @return [Array] Returns the current page without modifying the
100
+ # current position.
101
+ def curr
102
+ @pages[@curr]
103
+ end
104
+
105
+ # Moves the next page, and returns the {Pager} object itself. If at the last
106
+ # page it simply returns +self+.
107
+ #
108
+ # @return [Seq::Pager]
109
+ def next!
110
+ @curr += 1 unless last?
111
+ self
112
+ end
113
+
114
+ # Moves to, and returns the next page. If at the last page it returns +nil+.
115
+ #
116
+ # @return [Array, nil]
117
+ def next
118
+ return if last?
119
+
120
+ @curr += 1
121
+ curr
122
+ end
123
+
124
+ # Moves to the previous page, and returns the {Pager} object itself. If at
125
+ # the first page it simply returns +self+.
126
+ #
127
+ # @return [Seq::Pager]
128
+ def prev!
129
+ @curr -= 1 unless first?
130
+ self
131
+ end
132
+
133
+ # Moves to, and returns, the previous page. If at the first page it returns
134
+ # +nil+.
135
+ #
136
+ # @return [Array, nil]
137
+ def prev
138
+ return if first?
139
+
140
+ @curr -= 1
141
+ curr
142
+ end
143
+
144
+ # Provides a list of page numbers, useful for populating a page selector. It
145
+ # returns an array containing three arrays, some of which may be empty.
146
+ #
147
+ # @param left [Integer] Maximum number of elements for the left-most array
148
+ # @param middle [Integer] Maximum number of elements for either side of the
149
+ # current page
150
+ # @param right [Integer] Maximum number of elemengts for the right-most array
151
+ #
152
+ # @example
153
+ #
154
+ # s = Seq::Pager.new(10, 1..1000)
155
+ #
156
+ # s.range(2, 2, 2)
157
+ # #=> [[0, 1, 2, 3, 4, 5, 6, 7], [], [98, 99]]
158
+ #
159
+ # s.page = 10
160
+ # s.range(2, 2, 2)
161
+ # #=> [[0, 1, 2], [48, 49, 50, 51, 52], [98, 99]]
162
+ #
163
+ def range(left, middle, right)
164
+ width = 2 * middle + 1
165
+
166
+ if pages < left + width + right
167
+ return [(0..pages-1).to_a, [], []]
168
+ end
169
+
170
+ if page < left + middle + 1
171
+ left = (0..left+width-1).to_a
172
+ middle = []
173
+ right = (pages-right..pages-1).to_a
174
+
175
+ elsif page > (pages - 1) - right - middle - 1
176
+ left = (0..left-1).to_a
177
+ middle = []
178
+ right = (pages-right-width..pages-1).to_a
179
+
180
+ else
181
+ left = (0..left-1).to_a
182
+ middle = (page-middle..page+middle).to_a
183
+ right = (pages-right..pages-1).to_a
184
+ end
185
+
186
+ [left, middle, right]
187
+ end
188
+
189
+ end
190
+ end
@@ -7,10 +7,10 @@ class Seq
7
7
  # @example
8
8
  #
9
9
  # s = Seq::Random.new([1,2,3,4,5])
10
- # s.take(6) #=> [1, 4, 5, 3, 1, 2]
10
+ # s.take(6) #=> [1, 4, 5, 3, 1, 2]
11
11
  #
12
12
  class Random < Seq
13
-
13
+
14
14
  # Creates a new Random seq instance.
15
15
  #
16
16
  # @param list [Array] List of values to cycle over
@@ -19,8 +19,8 @@ class Seq
19
19
  def initialize(list=[], items=Float::INFINITY, default=nil)
20
20
  super(list, items, 0, default)
21
21
  end
22
-
23
- # @return Until ended it returns a randomly selected item from the list, when
22
+
23
+ # @return Until ended it returns a randomly selected item from the list, when
24
24
  # ended it returns the default value.
25
25
  def next
26
26
  if ended?
@@ -29,6 +29,6 @@ class Seq
29
29
  @list[rand(@list.size).to_i].tap { inc }
30
30
  end
31
31
  end
32
-
32
+
33
33
  end
34
34
  end
@@ -1,3 +1,3 @@
1
1
  class Seq
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -2,7 +2,9 @@ $: << File.dirname(__FILE__)
2
2
  $: << File.dirname(__FILE__) + '/../lib'
3
3
 
4
4
  require 'seq'
5
+ require 'seq/lazy'
6
+ require 'seq/paged'
7
+ require 'seq/pager'
8
+ require 'seq/random'
5
9
 
6
- require 'rubygems'
7
- require 'minitest/unit'
8
10
  require 'minitest/autorun'
@@ -1,7 +1,7 @@
1
1
  $: << File.dirname(__FILE__) + '/..'
2
2
  require 'helper'
3
3
 
4
- class LazySeqTest < MiniTest::Unit::TestCase
4
+ class LazySeqTest < MiniTest::Test
5
5
 
6
6
  def setup
7
7
  @seq = Seq::Lazy.new([1, 1]) {|l| l[-1] + l[-2] }
@@ -1,7 +1,7 @@
1
1
  $: << File.dirname(__FILE__) + '/..'
2
2
  require 'helper'
3
3
 
4
- class PagedSeqTest < MiniTest::Unit::TestCase
4
+ class PagedSeqTest < MiniTest::Test
5
5
 
6
6
  PAGE_COUNT = 10
7
7
  PAGE_SIZE = 10
@@ -13,6 +13,16 @@ class PagedSeqTest < MiniTest::Unit::TestCase
13
13
  end
14
14
  end
15
15
 
16
+ def test_defines_each
17
+ s = Seq::Paged.new do |page|
18
+ page == 3 ? [] : [1]
19
+ end
20
+
21
+ s.each do |n|
22
+ assert_equal n, 1
23
+ end
24
+ end
25
+
16
26
  def test_defines_next
17
27
  s = Seq::Paged.new(&@block)
18
28
 
@@ -33,7 +43,7 @@ class PagedSeqTest < MiniTest::Unit::TestCase
33
43
  assert_equal "default", s.next
34
44
  end
35
45
 
36
- def test_offset_works
46
+ def test_offset
37
47
  s = Seq::Paged.new(5, &@block)
38
48
 
39
49
  5.upto(99) do |n|
@@ -43,4 +53,17 @@ class PagedSeqTest < MiniTest::Unit::TestCase
43
53
  assert_equal nil, s.next
44
54
  end
45
55
 
56
+ def test_entries
57
+ s = Seq::Paged.new do |page|
58
+ case page
59
+ when 0 then [1,2,3]
60
+ when 1 then [4]
61
+ when 2 then [5,6]
62
+ when 3 then []
63
+ when 4 then [100]
64
+ end
65
+ end
66
+
67
+ assert_equal s.entries, [1,2,3,4,5,6]
68
+ end
46
69
  end
@@ -0,0 +1,109 @@
1
+ $: << File.dirname(__FILE__) + '/..'
2
+ require 'helper'
3
+
4
+ class PagerSeqTest < MiniTest::Test
5
+
6
+ PAGE_COUNT = 10
7
+ PAGE_SIZE = 10
8
+
9
+
10
+ def test_page_setting
11
+ s = Seq::Pager.new(5, 1..100)
12
+ s.page = 5
13
+ assert_equal s.page, 5
14
+ end
15
+
16
+ def test_page_setting_error
17
+ s = Seq::Pager.new(5, 1..100)
18
+
19
+ assert_raises ArgumentError do
20
+ s.page = Object.new
21
+ end
22
+ end
23
+
24
+ def test_page
25
+ s = Seq::Pager.new(5, 1..100)
26
+ assert_equal s.page, 0
27
+ end
28
+
29
+ def test_first?
30
+ s = Seq::Pager.new(5, 1..100)
31
+ assert s.first?
32
+
33
+ s.next
34
+ refute s.first?
35
+ end
36
+
37
+ def test_last?
38
+ s = Seq::Pager.new(5, 1..10)
39
+ refute s.last?
40
+
41
+ s.next
42
+ assert s.last?
43
+ end
44
+
45
+ def test_pages
46
+ s = Seq::Pager.new(5, 1..100)
47
+ assert_equal s.pages, 20
48
+ end
49
+
50
+ def test_curr
51
+ s = Seq::Pager.new(5, 1..100)
52
+ assert_equal [1,2,3,4,5], s.curr
53
+ end
54
+
55
+ def test_next
56
+ s = Seq::Pager.new(5, 1..100)
57
+ assert_equal [6,7,8,9,10], s.next
58
+ end
59
+
60
+ def test_next!
61
+ s = Seq::Pager.new(5, 1..100)
62
+ s = s.next!
63
+ assert_instance_of Seq::Pager, s
64
+ assert_equal s.page, 1
65
+ end
66
+
67
+ def test_prev
68
+ s = Seq::Pager.new(5, 1..100)
69
+ assert_equal [6,7,8,9,10], s.next!.next!.prev
70
+ end
71
+
72
+ def test_prev!
73
+ s = Seq::Pager.new(5, 1..100)
74
+ s = s.next!.next!.prev!
75
+ assert_instance_of Seq::Pager, s
76
+ assert_equal s.page, 1
77
+ end
78
+
79
+ def test_range
80
+ s = Seq::Pager.new(2, 1..100)
81
+ assert_equal s.range(2, 2, 2), [[0, 1, 2, 3, 4, 5, 6], [], [48, 49]]
82
+
83
+ s.page = 25
84
+ assert_equal s.range(2, 2, 2), [[0, 1], [23, 24, 25, 26, 27], [48, 49]]
85
+
86
+ s.last
87
+ assert_equal s.range(2, 2, 2), [[0, 1], [], [43, 44, 45, 46, 47, 48, 49]]
88
+ end
89
+
90
+ def test_range_cases
91
+ s = Seq::Pager.new(50, 1..100)
92
+ assert_equal s.range(2, 2, 2), [[0, 1], [], []]
93
+
94
+ s = Seq::Pager.new(2, 1..100)
95
+
96
+ s.page = 4
97
+ assert_equal s.range(2, 2, 2), [[0, 1, 2, 3, 4, 5, 6], [], [48, 49]]
98
+
99
+ s.page = 5
100
+ assert_equal s.range(2, 2, 2), [[0, 1], [3, 4, 5, 6, 7], [48, 49]]
101
+
102
+ s.page = 44
103
+ assert_equal s.range(2, 2, 2), [[0, 1], [42, 43, 44, 45, 46], [48, 49]]
104
+
105
+ s.page = 45
106
+ assert_equal s.range(2, 2, 2), [[0, 1], [], [43, 44, 45, 46, 47, 48, 49]]
107
+ end
108
+
109
+ end
@@ -1,12 +1,12 @@
1
1
  $: << File.dirname(__FILE__) + '/..'
2
2
  require 'helper'
3
3
 
4
- class RandomSeqTest < MiniTest::Unit::TestCase
4
+ class RandomSeqTest < MiniTest::Test
5
5
 
6
6
  def setup
7
7
  @seq = Seq::Random.new([1, 2, 3], 6)
8
8
  end
9
-
9
+
10
10
  def test_defines_next
11
11
  assert_includes [1,2,3], @seq.next
12
12
  assert_includes [1,2,3], @seq.next
@@ -16,9 +16,9 @@ class RandomSeqTest < MiniTest::Unit::TestCase
16
16
  assert_includes [1,2,3], @seq.next
17
17
  assert_equal nil, @seq.next
18
18
  end
19
-
19
+
20
20
  def test_cycles_correct_times
21
21
  assert_equal 6, @seq.entries.size
22
22
  end
23
23
 
24
- end
24
+ end
@@ -1,7 +1,7 @@
1
1
  $: << File.dirname(__FILE__)
2
2
  require 'helper'
3
3
 
4
- class SeqTest < MiniTest::Unit::TestCase
4
+ class SeqTest < MiniTest::Test
5
5
 
6
6
  def setup
7
7
  @empty = Seq.new
@@ -29,14 +29,15 @@ class SeqTest < MiniTest::Unit::TestCase
29
29
 
30
30
  @seq.each_with_index do |a,i|
31
31
  assert_equal a, case i
32
- when 0 then 2
33
- when 1 then 3
34
- when 2 then 4
35
- when 3 then 1
36
- when 4 then 2
37
- when 5 then 3
38
- when 6 then 4
39
- end
32
+ when 0 then 2
33
+ when 1 then 3
34
+ when 2 then 4
35
+ when 3 then 1
36
+ when 4 then 2
37
+ when 5 then 3
38
+ when 6 then 4
39
+ else assert_fail
40
+ end
40
41
  end
41
42
  end
42
43
 
@@ -61,5 +62,4 @@ class SeqTest < MiniTest::Unit::TestCase
61
62
  @seq.reset
62
63
  assert_equal 2, @seq.next
63
64
  end
64
-
65
65
  end
metadata CHANGED
@@ -1,33 +1,30 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seq
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
5
- prerelease:
4
+ version: 0.3.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Joshua Hawxwell
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-05-01 00:00:00.000000000 Z
11
+ date: 2014-03-07 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: minitest
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
19
  version: 2.3.1
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ~>
24
+ - - "~>"
28
25
  - !ruby/object:Gem::Version
29
26
  version: 2.3.1
30
- description: ! " A Seq is created with an array, and optionally a number of elements\n
27
+ description: " A Seq is created with an array, and optionally a number of elements\n
31
28
  \ to return, an offset to start at and a default item to return when\n ended.
32
29
  Call #next to return the next item.\n \n A Seq::Random will return randomly
33
30
  selected elements from the array.\n A Seq::Lazy will lazily evaluate a block
@@ -40,44 +37,46 @@ files:
40
37
  - README.md
41
38
  - Rakefile
42
39
  - LICENCE
40
+ - lib/seq.rb
43
41
  - lib/seq/lazy.rb
44
- - lib/seq/paged.rb
45
- - lib/seq/random.rb
42
+ - lib/seq/pager.rb
46
43
  - lib/seq/version.rb
47
- - lib/seq.rb
44
+ - lib/seq/random.rb
45
+ - lib/seq/paged.rb
46
+ - test/seq_test.rb
48
47
  - test/helper.rb
48
+ - test/seq/pager_test.rb
49
+ - test/seq/random_test.rb
49
50
  - test/seq/lazy_test.rb
50
51
  - test/seq/paged_test.rb
51
- - test/seq/random_test.rb
52
- - test/seq_test.rb
53
52
  homepage: http://github.com/hawx/seq
54
53
  licenses: []
54
+ metadata: {}
55
55
  post_install_message:
56
56
  rdoc_options: []
57
57
  require_paths:
58
58
  - lib
59
59
  required_ruby_version: !ruby/object:Gem::Requirement
60
- none: false
61
60
  requirements:
62
- - - ! '>='
61
+ - - ">="
63
62
  - !ruby/object:Gem::Version
64
63
  version: '0'
65
64
  required_rubygems_version: !ruby/object:Gem::Requirement
66
- none: false
67
65
  requirements:
68
- - - ! '>='
66
+ - - ">="
69
67
  - !ruby/object:Gem::Version
70
68
  version: '0'
71
69
  requirements: []
72
70
  rubyforge_project:
73
- rubygems_version: 1.8.23
71
+ rubygems_version: 2.0.14
74
72
  signing_key:
75
- specification_version: 3
73
+ specification_version: 4
76
74
  summary: Seqs cycle over elements of an array.
77
75
  test_files:
76
+ - test/seq_test.rb
78
77
  - test/helper.rb
78
+ - test/seq/pager_test.rb
79
+ - test/seq/random_test.rb
79
80
  - test/seq/lazy_test.rb
80
81
  - test/seq/paged_test.rb
81
- - test/seq/random_test.rb
82
- - test/seq_test.rb
83
82
  has_rdoc: