rindle 0.2.0 → 0.3.0

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.
@@ -5,6 +5,23 @@ class Rindle
5
5
  class NotFound < Exception; end
6
6
 
7
7
  class << self
8
+
9
+ def create filename, options = {}
10
+ doc = Rindle::Document.new filename
11
+ Rindle.index[doc.index] = doc
12
+
13
+ absolute_path = File.join(Rindle.root_path, doc.path)
14
+ if options[:data]
15
+ File.open(absolute_path, 'w+') do |f|
16
+ f.write options[:data]
17
+ end
18
+ else
19
+ FileUtils.touch absolute_path
20
+ end
21
+
22
+ doc
23
+ end
24
+
8
25
  def all options = {}
9
26
  filtered = []
10
27
  Rindle.index.each_pair do |index, doc|
@@ -83,6 +100,8 @@ class Rindle
83
100
  attr_reader :index, :path
84
101
 
85
102
  def initialize path
103
+ path = "/#{File.join('documents', path)}" unless path =~ %r{^/{0,1}documents/}
104
+ path = "/#{path}" unless path =~ %r{^/}
86
105
  @path = path
87
106
  @index = Rindle::Document.generate_index(path)
88
107
  end
@@ -126,6 +145,18 @@ class Rindle
126
145
  Rindle.index[@index] = self
127
146
  end
128
147
 
148
+ def destroy!
149
+ Rindle.collections.values.each do |col|
150
+ col.remove index if col.include? index
151
+ end
152
+ Rindle.index.delete index
153
+ end
154
+
155
+ def delete!
156
+ destroy!
157
+ FileUtils.rm_f File.join(Rindle.root_path, path)
158
+ end
159
+
129
160
  # Returns an array of all the collections, this document is in.
130
161
  def collections
131
162
  Rindle.collections.values.select do |col|
@@ -1,3 +1,3 @@
1
1
  class Rindle
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -0,0 +1 @@
1
+ Some dummy data
@@ -0,0 +1 @@
1
+ Some dummy data
@@ -0,0 +1 @@
1
+ Some dummy data
@@ -0,0 +1 @@
1
+ Some dummy data
@@ -10,11 +10,44 @@ describe Rindle::Document do
10
10
  end
11
11
 
12
12
  it 'equals another document if the index is the same' do
13
- doc1 = Rindle::Document.new('documents/ABC-asin_B001UQ5HVA-type_EBSP-v1.azw')
14
- doc2 = Rindle::Document.new('documents/ABC-asin_B001UQ5HVA-type_EBSP-v1.azw')
13
+ doc1 = Rindle::Document.new('ABC-asin_B001UQ5HVA-type_EBSP-v1.azw')
14
+ doc2 = Rindle::Document.new('ABC-asin_B001UQ5HVA-type_EBSP-v1.azw')
15
15
  doc1.should == doc2
16
16
  end
17
17
 
18
+ describe '.create' do
19
+ before :all do
20
+ @doc1 = Rindle::Document.create 'A test file.pdf'
21
+ @doc2 = Rindle::Document.create 'Another test file.rtf',
22
+ :data => 'Some dummy data'
23
+ end
24
+
25
+ after :all do
26
+ Rindle.index.delete @doc1.index
27
+ FileUtils.rm_f File.join(Rindle.root_path, @doc1.path)
28
+ Rindle.index.delete @doc2.index
29
+ FileUtils.rm_f File.join(Rindle.root_path, @doc2.path)
30
+ end
31
+
32
+ it 'should return a new document' do
33
+ @doc1.should be_a(Rindle::Document)
34
+ @doc2.should be_a(Rindle::Document)
35
+ end
36
+
37
+ it 'should add the document to the index' do
38
+ Rindle.index['*d596158a92f064c94e690c2480e2d09ba891dcc2'].should == @doc1
39
+ Rindle.index['*ecb3bda81a628550bd056dca449cc1a37b523475'].should == @doc2
40
+ end
41
+
42
+ it 'should write data if given' do
43
+ File.read(File.join(Rindle.root_path, @doc2.path)).should == 'Some dummy data'
44
+ end
45
+
46
+ it 'should touch file if no data given' do
47
+ File.should exist File.join(Rindle.root_path, @doc1.path)
48
+ end
49
+ end
50
+
18
51
  describe '.find' do
19
52
  context 'finds :all filtered' do
20
53
  it 'by name with string' do
@@ -52,6 +85,21 @@ describe Rindle::Document do
52
85
  end
53
86
  end
54
87
 
88
+ describe '.new' do
89
+ it 'should prepend slash if missing' do
90
+ doc = Rindle::Document.new 'documents/ABC.pdf'
91
+ doc.path.should == '/documents/ABC.pdf'
92
+ end
93
+
94
+ it 'should prepend /documents if missing' do
95
+ doc = Rindle::Document.new 'ABC.pdf'
96
+ doc.path.should == '/documents/ABC.pdf'
97
+ doc = Rindle::Document.new '/ABC.pdf'
98
+ doc.path.should == '/documents/ABC.pdf'
99
+ end
100
+ end
101
+
102
+
55
103
  describe '.unassociated' do
56
104
  it 'should return unassociated documents' do
57
105
  docs = Rindle::Document.unassociated
@@ -138,6 +186,54 @@ describe Rindle::Document do
138
186
  end
139
187
  end
140
188
 
189
+ describe '#destroy!' do
190
+ before :all do
191
+ @doc = Rindle::Document.find_by_name 'A test aswell.mobi'
192
+ @doc.destroy!
193
+ end
194
+
195
+ after :all do
196
+ doc = Rindle::Document.create 'A test aswell.mobi', :data => 'Some dummy data'
197
+ Rindle.collections['collection1'].add doc
198
+ end
199
+
200
+ it 'should remove the document from index' do
201
+ Rindle.index.should_not have_key '*18be6fcd5d5df39c1a96cd22596bbe7fe01db9b7'
202
+ end
203
+
204
+ it 'should remove the document from any collection' do
205
+ Rindle.collections['collection1'].
206
+ include?('*18be6fcd5d5df39c1a96cd22596bbe7fe01db9b7').
207
+ should == false
208
+ end
209
+ end
210
+
211
+ describe '#delete!' do
212
+ before :all do
213
+ @doc = Rindle::Document.find_by_name 'A test aswell.mobi'
214
+ @doc.delete!
215
+ end
216
+
217
+ after :all do
218
+ doc = Rindle::Document.create 'A test aswell.mobi', :data => 'Some dummy data'
219
+ Rindle.collections['collection1'].add doc
220
+ end
221
+
222
+ it 'should remove the document from index' do
223
+ Rindle.index.should_not have_key '*18be6fcd5d5df39c1a96cd22596bbe7fe01db9b7'
224
+ end
225
+
226
+ it 'should remove the document from any collection' do
227
+ Rindle.collections['collection1'].
228
+ include?('*18be6fcd5d5df39c1a96cd22596bbe7fe01db9b7').
229
+ should == false
230
+ end
231
+
232
+ it 'should delete the documents file' do
233
+ File.should_not exist File.join(Rindle.root_path, @doc.path)
234
+ end
235
+ end
236
+
141
237
  describe '#collections' do
142
238
  it 'should return the associated collections' do
143
239
  @doc = Rindle::Document.find_by_name('A test aswell.mobi')
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.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-05-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &18209800 !ruby/object:Gem::Requirement
16
+ requirement: &22965600 !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: *18209800
24
+ version_requirements: *22965600
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: guard-rspec
27
- requirement: &18209380 !ruby/object:Gem::Requirement
27
+ requirement: &22965080 !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: *18209380
35
+ version_requirements: *22965080
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: -2614478778351994306
90
+ hash: 4157338638094343741
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: -2614478778351994306
99
+ hash: 4157338638094343741
100
100
  requirements: []
101
101
  rubyforge_project:
102
102
  rubygems_version: 1.8.17