circular_list 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,11 +11,19 @@ module CircularList
11
11
  end
12
12
 
13
13
  def fetch_previous(index=0)
14
- @arr.unshift(@arr.pop)[index]
14
+ index.nil? ? nil : @arr.unshift(@arr.pop)[index]
15
15
  end
16
16
 
17
17
  def fetch_next(index=0)
18
- @arr.push(@arr.shift)[index]
18
+ index.nil? ? nil : @arr.push(@arr.shift)[index]
19
+ end
20
+
21
+ def fetch_after(e)
22
+ fetch_next(@arr.index(e))
23
+ end
24
+
25
+ def fetch_before(e)
26
+ fetch_previous(@arr.index(e))
19
27
  end
20
28
  end
21
29
  end
@@ -1,3 +1,3 @@
1
1
  module CircularList
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -67,7 +67,7 @@ module CircularList
67
67
  c.fetch_next(12).should eq nil
68
68
 
69
69
  c = List.new(['a','b','c','d'])
70
- c.fetch_previous(12).should eq nil
70
+ c.fetch_next(12).should eq nil
71
71
  end
72
72
 
73
73
  it "fetch_previous should return nil" do
@@ -78,7 +78,45 @@ module CircularList
78
78
  c.fetch_previous(12).should eq nil
79
79
  end
80
80
  end
81
+
82
+ context "when a valid element is specified" do
83
+ it "fetch_after should return next element after the given element" do
84
+ c = List.new(['a','b','c','d'])
85
+ c.fetch_after('c').should eq 'd'
86
+
87
+ c = List.new(['a','b','c','d'])
88
+ c.fetch_after('d').should eq 'a'
89
+ end
90
+
91
+ it "fetch_before should return previous element before the given element" do
92
+ c = List.new(['a','b','c','d'])
93
+ c.fetch_before('c').should eq 'b'
94
+
95
+ c = List.new(['a','b','c','d'])
96
+ c.fetch_before('a').should eq 'd'
97
+ end
98
+ end
99
+
100
+ context "when an ivalid element is specified" do
101
+ it "fetch_after should return nil" do
102
+ c = List.new(['a','b','c','d'])
103
+ c.fetch_after('z').should eq nil
104
+
105
+ c = List.new(['a','b','c','d'])
106
+ c.fetch_after('z').should eq nil
107
+ end
108
+
109
+ it "fetch_before should return nil" do
110
+ c = List.new(['a','b','c','d'])
111
+ c.fetch_before('z').should eq nil
112
+
113
+ c = List.new(['a','b','c','d'])
114
+ c.fetch_before('z').should eq nil
115
+ end
116
+ end
117
+
81
118
  end
82
119
 
120
+
83
121
  end
84
122
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: circular_list
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - unnitallman
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-09-27 00:00:00 +05:30
18
+ date: 2011-10-13 00:00:00 +05:30
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -68,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
68
  requirements: []
69
69
 
70
70
  rubyforge_project: circular_list
71
- rubygems_version: 1.4.2
71
+ rubygems_version: 1.6.2
72
72
  signing_key:
73
73
  specification_version: 3
74
74
  summary: A circular list data structure for Ruby