rindle 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec CHANGED
@@ -1,2 +1,2 @@
1
- --format nested
2
1
  --color
2
+ --format progress
@@ -101,8 +101,8 @@ class Rindle
101
101
  # Adds an index or a document to the collection.
102
102
  def add index
103
103
  index = index.index if index.is_a?(Document)
104
- unless indices.include?(index)
105
- indices << obj.index
104
+ unless @indices.include?(index)
105
+ @indices << index
106
106
  @documents = nil
107
107
  end
108
108
  end
@@ -110,8 +110,8 @@ class Rindle
110
110
  # Removes an entry from this collection.
111
111
  def remove index
112
112
  index = index.index if index.is_a?(Document)
113
- if indices.include?(index)
114
- indices.delete index
113
+ if @indices.include?(index)
114
+ @indices.delete index
115
115
  @documents = nil
116
116
  end
117
117
  end
@@ -130,11 +130,11 @@ class Rindle
130
130
 
131
131
  # Sets the array of `Document` objects.
132
132
  def documents= documents
133
- indices = documents.map(&:index)
133
+ self.indices = documents.map(&:index)
134
134
  @documents = documents
135
135
  end
136
136
 
137
- # Returns true if the collection includes the given indec,
137
+ # Returns true if the collection includes the given index,
138
138
  # `Document` or `Array`.
139
139
  def include? obj
140
140
  if obj.is_a?(Array)
@@ -69,27 +69,22 @@ class Rindle
69
69
  def find_by_index index
70
70
  Rindle.index[index]
71
71
  end
72
+
73
+ # Generates the index for the current path
74
+ def generate_index path
75
+ if path =~ /([\w\s]+)-asin_([A-Z0-9]+)-type_([A-Z]+)-v_[0-9]+.azw/
76
+ "##{$2}^#{$3}"
77
+ else
78
+ "*#{Digest::SHA1.hexdigest(File.join('/mnt/us', path))}"
79
+ end
80
+ end
72
81
  end
73
82
 
74
83
  attr_reader :index, :path
75
84
 
76
85
  def initialize path
77
- self.path = path
78
- end
79
-
80
- # Sets the path variable and updates the index
81
- def path= path
82
86
  @path = path
83
- @index = generate_index
84
- end
85
-
86
- # Generates the index for the current path
87
- def generate_index
88
- if path =~ /([\w\s]+)-asin_([A-Z0-9]+)-type_([A-Z]+)-v_[0-9]+.azw/
89
- "##{$2}^#{$3}"
90
- else
91
- "*#{Digest::SHA1.hexdigest(File.join('/mnt/us', path))}"
92
- end
87
+ @index = Rindle::Document.generate_index(path)
93
88
  end
94
89
 
95
90
  # Two documents are the same if the indices are equal.
@@ -116,14 +111,24 @@ class Rindle
116
111
  # and the Index-hash is updated.
117
112
  def rename! new_name
118
113
  Rindle.index.delete(@index)
119
- self.path = @path.gsub filename, new_name
120
- # TODO: update references in Rindle.collections
114
+
115
+ old_index = @index
116
+ @path.gsub!(filename, new_name)
117
+ @index = Rindle::Document.generate_index(@path)
118
+
119
+ Rindle.collections.values.each do |col|
120
+ if col.include?(old_index)
121
+ col.remove old_index
122
+ col.add @index
123
+ end
124
+ end
125
+
121
126
  Rindle.index[@index] = self
122
127
  end
123
128
 
124
129
  # Returns an array of all the collections, this document is in.
125
130
  def collections
126
- Rindle.collections.select do |col|
131
+ Rindle.collections.values.select do |col|
127
132
  col.include? self.index
128
133
  end
129
134
  end
@@ -1,3 +1,3 @@
1
1
  class Rindle
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,3 +1,4 @@
1
+ # -*- coding: utf-8 -*-
1
2
  require "spec_helper"
2
3
 
3
4
  describe Rindle::Collection do
@@ -5,8 +6,7 @@ describe Rindle::Collection do
5
6
  Rindle.load(kindle_root)
6
7
  end
7
8
 
8
-
9
- context '#exists?' do
9
+ describe '.exists?' do
10
10
  it 'returns true if the collection exists' do
11
11
  Rindle::Collection.exists?(:named => 'collection1').should == true
12
12
  end
@@ -16,24 +16,54 @@ describe Rindle::Collection do
16
16
  end
17
17
  end
18
18
 
19
- context '#all' do
19
+ describe '.all' do
20
20
  it 'invokes Collection.find with parameter :all' do
21
21
  Rindle::Collection.find(:all).map(&:name).should == Rindle::Collection.all.map(&:name)
22
22
  end
23
23
  end
24
24
 
25
- context '#first' do
25
+ describe '.first' do
26
26
  it 'invokes Collection.find with parameter :first' do
27
27
  Rindle::Collection.find(:first).should == Rindle::Collection.first
28
28
  end
29
29
  end
30
30
 
31
- context '#find' do
31
+ describe '.find_by_name' do
32
+ it 'should return the collection by name' do
33
+ Rindle::Collection.find_by_name('collection1').
34
+ name.should == 'collection1'
35
+ end
36
+
37
+ it 'should return nil if not found' do
38
+ Rindle::Collection.find_by_name('collection3').
39
+ should == nil
40
+ end
41
+ end
42
+
43
+ describe '.create' do
44
+ before :all do
45
+ @col = Rindle::Collection.create 'some_collection'
46
+ end
47
+
48
+ after :all do
49
+ Rindle.collections.delete 'some_collection'
50
+ end
51
+
52
+ it 'should create a new collection object' do
53
+ @col.should_not == nil
54
+ end
55
+
56
+ it 'should add the collection to collections hash' do
57
+ Rindle.collections['some_collection'] == @col
58
+ end
59
+ end
60
+
61
+ describe '.find' do
32
62
  it 'returns an array of Kindle::Collection objects' do
33
63
  Rindle::Collection.find
34
64
  end
35
65
 
36
- context 'finds all filtered' do
66
+ describe 'finds all filtered' do
37
67
  it 'by name with string' do
38
68
  name = "collection"
39
69
  collections = Rindle::Collection.find(:all, :named => name)
@@ -62,7 +92,7 @@ describe Rindle::Collection do
62
92
  end
63
93
  end
64
94
 
65
- context 'finds first filtered' do
95
+ describe 'finds first filtered' do
66
96
  it 'by name' do
67
97
  collection = Rindle::Collection.find(:first, :named => 'collection1')
68
98
  collection.name.should == 'collection1'
@@ -80,21 +110,195 @@ describe Rindle::Collection do
80
110
  end
81
111
  end
82
112
 
83
- context '#documents' do
84
- it 'returns an array of Rindle::Document objects' do
85
- collection = Rindle::Collection.find(:first, :named => 'collection1')
86
- collection.documents.each do |document|
87
- document.should be_a(Rindle::Document)
113
+ describe 'Instance' do
114
+ before :all do
115
+ @col = Rindle::Collection.create 'test collection'
116
+ @col.add "*18be6fcd5d5df39c1a96cd22596bbe7fe01db9b7"
117
+ @col.add '#B001UQ5HVA^EBSP'
118
+ end
119
+
120
+ after :all do
121
+ Rindle.collections.delete 'test collection'
122
+ end
123
+
124
+ describe '#documents' do
125
+ it 'returns an array of Rindle::Document objects' do
126
+ @col.documents.each do |document|
127
+ document.should be_a(Rindle::Document)
128
+ end
129
+ end
130
+ end
131
+
132
+ describe '#documents=' do
133
+ it 'should set the documents indices to be the new indices array' do
134
+ docs = [ "*18be6fcd5d5df39c1a96cd22596bbe7fe01db9b7", '#B001UQ5HVA^EBSP' ].map do |index|
135
+ Rindle::Document.find_by_index index
136
+ end
137
+ @col.documents = docs
138
+ @col.indices.should == [ "*18be6fcd5d5df39c1a96cd22596bbe7fe01db9b7", '#B001UQ5HVA^EBSP' ]
139
+ end
140
+ end
141
+
142
+ describe '#include?' do
143
+ context 'given an Array' do
144
+ it 'should return true if all indices are included' do
145
+ @col.include?([ "*18be6fcd5d5df39c1a96cd22596bbe7fe01db9b7", '#B001UQ5HVA^EBSP' ]).should == true
146
+ end
147
+ it 'should return false if one is not included' do
148
+ @col.include?([ "*18be6fcd5d5df39c1a96cd22596bbe7fe01db9b7", '#B001UQ5HVB^EBSP' ]).should == false
149
+ end
150
+ it 'should return false if none is included' do
151
+ @col.include?([ "*18be6fcd5d5df39c1a96cd22596bbe7fe01db9b6", '#B001UQ5HVB^EBSP' ]).should == false
152
+ end
153
+ end
154
+
155
+ context 'given a document' do
156
+ it 'should return true if the documents index is included' do
157
+ doc = Rindle::Document.find_by_name 'A test aswell.mobi'
158
+ @col.include?(doc).should == true
159
+ end
160
+ it 'should return false if the documents index isn´t included' do
161
+ doc = Rindle::Document.find_by_name 'Definitely a Test.pdf'
162
+ @col.include?(doc).should == false
163
+ end
164
+ end
165
+
166
+ context 'given an index' do
167
+ it 'should return true if index is included' do
168
+ @col.include?("*18be6fcd5d5df39c1a96cd22596bbe7fe01db9b7").should == true
169
+ end
170
+
171
+ it 'should return false if index isn´t included' do
172
+ @col.include?("*18be6fcd5d5df39c1a96cd22596bbe7fe01db9b6").should == false
173
+ end
174
+ end
175
+ end
176
+
177
+ describe '#indices=' do
178
+ before :all do
179
+ @col.indices = [ "*18be6fcd5d5df39c1a96cd22596bbe7fe01db9b7" ]
180
+ end
181
+
182
+ after :all do
183
+ @col.indices = [ "*18be6fcd5d5df39c1a96cd22596bbe7fe01db9b7", "#B001UQ5HVB^EBSP" ]
184
+ end
185
+
186
+ it 'should set the indices array' do
187
+ @col.indices.should == [ "*18be6fcd5d5df39c1a96cd22596bbe7fe01db9b7" ]
188
+ end
189
+ it 'should clear the memoized documents array' do
190
+ @col.documents.should == [ Rindle::Document.find_by_index("*18be6fcd5d5df39c1a96cd22596bbe7fe01db9b7") ]
191
+ end
192
+ end
193
+
194
+ describe '#rename!' do
195
+ before :all do
196
+ @col.rename! 'renamed test collection'
197
+ end
198
+
199
+ after :all do
200
+ @col.rename! 'test collection'
201
+ end
202
+
203
+ it 'should remove the old collections entry' do
204
+ Rindle.collections.should_not have_key 'test collection'
205
+ end
206
+
207
+ it 'should add the new collections entry' do
208
+ Rindle.collections.should have_key 'renamed test collection'
209
+ end
210
+
211
+ it 'should change the collections name' do
212
+ @col.name.should == 'renamed test collection'
213
+ end
214
+ end
215
+
216
+ describe '#destroy!' do
217
+ before :all do
218
+ @col.destroy!
219
+ end
220
+
221
+ after :all do
222
+ @col = Rindle::Collection.create 'test collection'
223
+ @col.add "*18be6fcd5d5df39c1a96cd22596bbe7fe01db9b7"
224
+ @col.add "#B001UQ5HVA^EBSP"
225
+ end
226
+
227
+ it 'should remove the collections entry' do
228
+ Rindle.collections.should_not have_key 'test collection'
88
229
  end
89
230
  end
90
- end
91
231
 
92
- context '#to_hash' do
93
- it 'returns a hash that could be found in collections.json' do
94
- now = Time.now.to_i
95
- collection = Rindle::Collection.new("test", :indices => ['a','b','c'],
96
- :last_access => now)
97
- collection.to_hash.should == { "test@en-US" => { "items" => ['a', 'b', 'c'], "lastAccess" => now } }
232
+ describe '#add' do
233
+ context 'given a document' do
234
+ before :all do
235
+ @col.add Rindle::Document.find_by_name 'Definitely a Test.pdf'
236
+ end
237
+
238
+ after :all do
239
+ @col.remove "*0849dd9b85fc341d10104f56985e423b3848e1f3"
240
+ end
241
+
242
+ it 'should add the documents index' do
243
+ @col.indices.should =~ ["*18be6fcd5d5df39c1a96cd22596bbe7fe01db9b7",
244
+ "#B001UQ5HVB^EBSP",
245
+ "*0849dd9b85fc341d10104f56985e423b3848e1f3"]
246
+ end
247
+ end
248
+
249
+ context 'given an index' do
250
+ before :all do
251
+ @col.add "*0849dd9b85fc341d10104f56985e423b3848e1f3"
252
+ end
253
+
254
+ after :all do
255
+ @col.remove "*0849dd9b85fc341d10104f56985e423b3848e1f3"
256
+ end
257
+
258
+ it 'should add the index' do
259
+ @col.indices.should include "*0849dd9b85fc341d10104f56985e423b3848e1f3"
260
+ end
261
+ end
262
+ end
263
+
264
+ describe '#remove' do
265
+ context 'given a document' do
266
+ before :all do
267
+ @col.remove Rindle::Document.find_by_name 'A test aswell.mobi'
268
+ end
269
+
270
+ after :all do
271
+ @col.add "*18be6fcd5d5df39c1a96cd22596bbe7fe01db9b7"
272
+ end
273
+
274
+ it 'should remove the documents index' do
275
+ @col.indices.should_not include '*18be6fcd5d5df39c1a96cd22596bbe7fe01db9b7'
276
+ end
277
+ end
278
+
279
+ context 'given an index' do
280
+ before :all do
281
+ @col.remove "*18be6fcd5d5df39c1a96cd22596bbe7fe01db9b7"
282
+ end
283
+
284
+ after :all do
285
+ @col.add "*18be6fcd5d5df39c1a96cd22596bbe7fe01db9b7"
286
+ end
287
+
288
+ it 'should remove the index' do
289
+ @col.indices.should_not include '*18be6fcd5d5df39c1a96cd22596bbe7fe01db9b7'
290
+ end
291
+ end
292
+ end
293
+
294
+ describe '#to_hash' do
295
+ it 'returns a hash that could be found in collections.json' do
296
+ now = Time.now.to_i
297
+ collection = Rindle::Collection.new("test",
298
+ :indices => ['a','b','c'],
299
+ :last_access => now)
300
+ collection.to_hash.should == { "test@en-US" => { "items" => ['a', 'b', 'c'], "lastAccess" => now } }
301
+ end
98
302
  end
99
303
  end
100
304
  end
@@ -9,7 +9,7 @@ describe Rindle::Document do
9
9
  Rindle.reset
10
10
  end
11
11
 
12
- it 'equals another if the index is the same' do
12
+ it 'equals another document if the index is the same' do
13
13
  doc1 = Rindle::Document.new('documents/ABC-asin_B001UQ5HVA-type_EBSP-v1.azw')
14
14
  doc2 = Rindle::Document.new('documents/ABC-asin_B001UQ5HVA-type_EBSP-v1.azw')
15
15
  doc1.should == doc2
@@ -39,10 +39,12 @@ describe Rindle::Document do
39
39
  doc = Rindle::Document.find(:first, :named => 'A test aswell.mobi')
40
40
  doc.filename.should == 'A test aswell.mobi'
41
41
  end
42
+
42
43
  it 'by name with regular expression' do
43
44
  doc = Rindle::Document.find(:first, :named => /t([es]+)t/)
44
45
  doc.filename.should == 'A test aswell.mobi'
45
46
  end
47
+
46
48
  it 'by index' do
47
49
  doc = Rindle::Document.find(:first, :indexed => '*18be6fcd5d5df39c1a96cd22596bbe7fe01db9b7')
48
50
  doc.filename.should == 'A test aswell.mobi'
@@ -56,4 +58,91 @@ describe Rindle::Document do
56
58
  docs.map(&:filename).should == [ 'This is a test document.rtf' ]
57
59
  end
58
60
  end
61
+
62
+ describe '.find_by_name' do
63
+ it 'should return the document with given path' do
64
+ Rindle::Document.find_by_name('A test aswell.mobi').index.should == '*18be6fcd5d5df39c1a96cd22596bbe7fe01db9b7'
65
+ end
66
+
67
+ it 'should return nil if not found' do
68
+ Rindle::Document.find_by_name('Non-existent.pdf').should be_nil
69
+ end
70
+ end
71
+
72
+ describe '.find_by_index' do
73
+ it 'should return the document with given index' do
74
+ Rindle::Document.find_by_index('*18be6fcd5d5df39c1a96cd22596bbe7fe01db9b7').filename.should == 'A test aswell.mobi'
75
+ end
76
+
77
+ it 'should return nil if not found' do
78
+ Rindle::Document.find_by_index('*18be6fcd5d5df39c1a96cd22596bbe7fe01db9b6').should be_nil
79
+ end
80
+ end
81
+
82
+ describe '#generate_index' do
83
+ it 'should generate an amazon index' do
84
+ Rindle::Document.generate_index('documents/A test aswell.mobi').should == '*18be6fcd5d5df39c1a96cd22596bbe7fe01db9b7'
85
+ end
86
+
87
+ it 'should generate a non-amazon index' do
88
+ Rindle::Document.
89
+ generate_index('documents/Salvia Divinorum Shamanic Plant-asin_B001UQ5HVA-type_EBSP-v_0.azw').
90
+ should == '#B001UQ5HVA^EBSP'
91
+ end
92
+ end
93
+
94
+ describe '#filename' do
95
+ it 'should return the basename of the path' do
96
+ doc = Rindle::Document.find_by_name('A test aswell.mobi')
97
+ doc.filename.should == 'A test aswell.mobi'
98
+ end
99
+ end
100
+
101
+ describe '#amazon?' do
102
+ it 'should return true if the filename is an amazon-like name' do
103
+ doc = Rindle::Document.find_by_name('Salvia Divinorum Shamanic Plant-asin_B001UQ5HVA-type_EBSP-v_0.azw')
104
+ doc.amazon?.should == true
105
+ end
106
+
107
+ it 'should return false if the filename is not an amazon-like name' do
108
+ doc = Rindle::Document.find_by_name('A test aswell.mobi')
109
+ doc.amazon?.should == false
110
+ end
111
+ end
112
+
113
+ describe '#rename!' do
114
+ before :all do
115
+ @doc = Rindle::Document.find_by_name('A test aswell.mobi')
116
+ @doc.rename! 'Indeed a test.mobi'
117
+ end
118
+
119
+ after :all do
120
+ @doc.rename! 'A test aswell.mobi'
121
+ end
122
+
123
+ it 'should remove the old index' do
124
+ Rindle.index['*18be6fcd5d5df39c1a96cd22596bbe7fe01db9b7'].should == nil
125
+ end
126
+
127
+ it 'should add the new index' do
128
+ Rindle.index['*c0dc3fee060e3f49c6fd789fa366d99a9334e835'].should == @doc
129
+ end
130
+
131
+ it 'should update all references in the collections' do
132
+ col = Rindle::Collection.find_by_name('collection1')
133
+ col.indices.should include(@doc.index)
134
+ end
135
+
136
+ it 'should set the new path' do
137
+ @doc.path.should == '/documents/Indeed a test.mobi'
138
+ end
139
+ end
140
+
141
+ describe '#collections' do
142
+ it 'should return the associated collections' do
143
+ @doc = Rindle::Document.find_by_name('A test aswell.mobi')
144
+ @doc.collections.map(&:name).should =~ [ 'collection1' ]
145
+ end
146
+ end
147
+
59
148
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rindle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-17 00:00:00.000000000 Z
12
+ date: 2012-05-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &25561500 !ruby/object:Gem::Requirement
16
+ requirement: &18209800 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *25561500
24
+ version_requirements: *18209800
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: guard-rspec
27
- requirement: &25560820 !ruby/object:Gem::Requirement
27
+ requirement: &18209380 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *25560820
35
+ version_requirements: *18209380
36
36
  description: The Rindle gem provides an object-oriented way to manage kindle collection
37
37
  data.
38
38
  email:
@@ -87,7 +87,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
87
87
  version: '0'
88
88
  segments:
89
89
  - 0
90
- hash: -4501087522031064736
90
+ hash: -2614478778351994306
91
91
  required_rubygems_version: !ruby/object:Gem::Requirement
92
92
  none: false
93
93
  requirements:
@@ -96,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
96
  version: '0'
97
97
  segments:
98
98
  - 0
99
- hash: -4501087522031064736
99
+ hash: -2614478778351994306
100
100
  requirements: []
101
101
  rubyforge_project:
102
102
  rubygems_version: 1.8.17