document_file 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +15 -9
- data/lib/document_file/collection.rb +26 -4
- data/lib/document_file/version.rb +1 -1
- data/test/document_file_collection_test.rb +122 -0
- data/test/document_file_test.rb +0 -82
- metadata +3 -3
data/README.textile
CHANGED
@@ -40,12 +40,14 @@ h2. Available functionality
|
|
40
40
|
|
41
41
|
h3. Dynamic finders
|
42
42
|
|
43
|
-
<pre><code>doc = MyDocument.find_by_title("Ruby is great")
|
44
|
-
doc = MyDocument.find_by_number_of_foos(42)
|
45
|
-
doc = MyDocument.find_by_file_name("2010-09-13-ruby-stuff")
|
46
|
-
|
47
|
-
docs = MyDocument.find_all_by_status("published")
|
48
|
-
docs = MyDocument.find_all_by_tag("programming")
|
43
|
+
<pre><code>doc = MyDocument.find_by_title("Ruby is great") # => returns the document
|
44
|
+
doc = MyDocument.find_by_number_of_foos(42) # => returns the document
|
45
|
+
doc = MyDocument.find_by_file_name("2010-09-13-ruby-stuff") # => returns the document
|
46
|
+
|
47
|
+
docs = MyDocument.find_all_by_status("published") # => [doc_1, doc_2, ...]
|
48
|
+
docs = MyDocument.find_all_by_tag("programming") # => [doc_1, doc_2, ...]
|
49
|
+
docs = MyDocument.find_all_by_tag("programming", :offset => 1) # => [doc_2, ...]
|
50
|
+
docs = MyDocument.find_all_by_tag("programming", :limit => 2) # => [doc_1, doc_2]
|
49
51
|
</code></pre>
|
50
52
|
|
51
53
|
|
@@ -60,9 +62,13 @@ h3. Listing documents by the date attribute
|
|
60
62
|
|
61
63
|
Document file tries to parse the filename of your documents to create a date attribute. In order to use the automatic parsing, the filenames have to start with a date string, e.g. @2010-09-13-ruby-stuff.textile@. If you don't want to set the date in the filename, you can still do it in the YAML front matter with e.g. @date: 2010-09-10@.
|
62
64
|
|
63
|
-
<pre><code>docs = MyDocument.find_all_by_date(2010)
|
64
|
-
docs = MyDocument.find_all_by_date(2010, 9)
|
65
|
-
docs = MyDocument.find_all_by_date(2010, 9, 13)
|
65
|
+
<pre><code>docs = MyDocument.find_all_by_date(2010) # => returns all documents from 2010
|
66
|
+
docs = MyDocument.find_all_by_date(2010, 9) # => returns all documents from Sep., 2010
|
67
|
+
docs = MyDocument.find_all_by_date(2010, 9, 13) # => returns all documents from Sep. 13th, 2010
|
68
|
+
docs = MyDocument.find_all_by_date('*', 9, 13) # => returns all documents from any Sep. 13th
|
69
|
+
docs = MyDocument.find_all_by_date(2010, '*', 13) # => returns all documents from any 13th of 2010
|
70
|
+
docs = MyDocument.find_all_by_date('*', '*', 13) # => returns all documents from any 13th
|
71
|
+
docs = MyDocument.find_all_by_date('*', 9) # => returns all documents from any Sep.
|
66
72
|
</code></pre>
|
67
73
|
|
68
74
|
h3. Chaining
|
@@ -2,6 +2,12 @@ require 'active_support/inflector'
|
|
2
2
|
|
3
3
|
module DocumentFile
|
4
4
|
class Collection < Array
|
5
|
+
attr_writer :total
|
6
|
+
|
7
|
+
def total
|
8
|
+
@total || length
|
9
|
+
end
|
10
|
+
|
5
11
|
def <<(document)
|
6
12
|
self.class.ensure_document(document)
|
7
13
|
define_dynamic_finders document.data
|
@@ -34,6 +40,7 @@ module DocumentFile
|
|
34
40
|
date_parts = %w(year month day)
|
35
41
|
docs = self
|
36
42
|
args.size.times do |i|
|
43
|
+
next if args[i] == '*'
|
37
44
|
docs = docs.select { |doc| doc.date.send(date_parts[i]) == args[i] }
|
38
45
|
end
|
39
46
|
self.class.new docs
|
@@ -86,19 +93,20 @@ module DocumentFile
|
|
86
93
|
singular_attribute = attribute
|
87
94
|
end
|
88
95
|
find_all_by_attribute = <<-eos
|
89
|
-
def find_all_by_#{singular_attribute}(attribute)
|
90
|
-
if respond_to?
|
96
|
+
def find_all_by_#{singular_attribute}(attribute, options = {})
|
97
|
+
documents = if respond_to?(:by_#{attribute})
|
91
98
|
by_#{attribute}[attribute]
|
92
99
|
else
|
93
|
-
|
100
|
+
select do |document|
|
94
101
|
if document.respond_to? :#{attribute}
|
95
102
|
document.#{attribute} == attribute
|
96
103
|
else
|
97
104
|
false
|
98
105
|
end
|
99
106
|
end
|
100
|
-
Collection.new documents
|
101
107
|
end
|
108
|
+
|
109
|
+
self.class.new(documents).offset_and_limitize(options[:offset], options[:limit])
|
102
110
|
end
|
103
111
|
eos
|
104
112
|
instance_eval find_all_by_attribute
|
@@ -115,5 +123,19 @@ module DocumentFile
|
|
115
123
|
eos
|
116
124
|
instance_eval find_by_attribute
|
117
125
|
end
|
126
|
+
|
127
|
+
def offset_and_limitize(offset, limit)
|
128
|
+
documents = self
|
129
|
+
total_size = documents.length
|
130
|
+
if offset
|
131
|
+
documents = documents.last(length - offset)
|
132
|
+
end
|
133
|
+
if limit
|
134
|
+
documents = documents.first(limit)
|
135
|
+
end
|
136
|
+
self.class.new(documents).tap do |col|
|
137
|
+
col.total = total_size
|
138
|
+
end
|
139
|
+
end
|
118
140
|
end
|
119
141
|
end
|
@@ -87,9 +87,131 @@ describe DocumentFile::Collection do
|
|
87
87
|
assert_equal 1, documents.size
|
88
88
|
end
|
89
89
|
|
90
|
+
it 'should return all documents with the year and day specified' do
|
91
|
+
documents = MyDocument.find_all_by_date 2010, '*', 8
|
92
|
+
assert_equal 1, documents.size
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'should return all documents with the month and day specified' do
|
96
|
+
documents = MyDocument.find_all_by_date '*', 8, 8
|
97
|
+
assert_equal 1, documents.size
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'should return all documents with the day specified' do
|
101
|
+
documents = MyDocument.find_all_by_date '*', '*', 8
|
102
|
+
assert_equal 1, documents.size
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'should return all documents with the month specified' do
|
106
|
+
documents = MyDocument.find_all_by_date '*', 8, '*'
|
107
|
+
assert_equal 2, documents.size
|
108
|
+
end
|
109
|
+
|
90
110
|
it 'should return the first match' do
|
91
111
|
document = MyDocument.find_by_date 2010, 8
|
92
112
|
assert_equal 1, document.id
|
93
113
|
end
|
94
114
|
end
|
115
|
+
|
116
|
+
describe 'when listing document_files by an Array attribute' do
|
117
|
+
it 'should return a Hash' do
|
118
|
+
assert_equal Hash, MyDocument.by_tags.class
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'should use the tags as Hash keys' do
|
122
|
+
assert_equal Set.new(['tag', 'tug']), MyDocument.by_tags.keys.to_set
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'should use the document_files as Hash values' do
|
126
|
+
document_files = MyDocument.by_tags
|
127
|
+
assert_equal Set.new([1, 2]), document_files['tag'].map(&:id).to_set
|
128
|
+
assert_equal Set.new([2]), document_files['tug'].map(&:id).to_set
|
129
|
+
end
|
130
|
+
|
131
|
+
it 'should not be confused by attributes that only some documents have' do
|
132
|
+
document_files_by_authors = MyDocument.by_authors
|
133
|
+
assert_equal 1, document_files_by_authors['Frank'].first.id
|
134
|
+
|
135
|
+
document_files_by_friends = MyDocument.by_friends
|
136
|
+
assert_equal 2, document_files_by_friends['Anton'].first.id
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
describe 'when finding all document_files by an Array attribute value' do
|
141
|
+
it 'should return a DocumentFile::Collection' do
|
142
|
+
klass = MyDocument.find_all_by_tag('tag').class
|
143
|
+
assert_equal DocumentFile::Collection, klass
|
144
|
+
end
|
145
|
+
|
146
|
+
it 'should containt documents' do
|
147
|
+
assert_equal MyDocument, MyDocument.find_all_by_tag('tag').first.class
|
148
|
+
end
|
149
|
+
|
150
|
+
it 'should return the right documents' do
|
151
|
+
assert_equal [1, 2], MyDocument.find_all_by_tag('tag').map(&:id)
|
152
|
+
assert_equal [2], MyDocument.find_all_by_tag('tug').map(&:id)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
describe 'when finding all document_files by an attribute value' do
|
157
|
+
before do
|
158
|
+
@collection = MyDocument.find_all_by_status(:published)
|
159
|
+
end
|
160
|
+
|
161
|
+
it 'should return a DocumentFile::Collection' do
|
162
|
+
assert_equal DocumentFile::Collection, @collection.class
|
163
|
+
end
|
164
|
+
|
165
|
+
it 'should containt documents' do
|
166
|
+
assert_equal MyDocument, @collection.first.class
|
167
|
+
end
|
168
|
+
|
169
|
+
it 'should return the right documents' do
|
170
|
+
assert_equal [1, 2], @collection.map(&:id)
|
171
|
+
end
|
172
|
+
|
173
|
+
it 'should return an empty collection if the document was not found' do
|
174
|
+
empty_collection = MyDocument.find_all_by_status(:draft)
|
175
|
+
assert_equal [], empty_collection.map(&:id)
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
describe 'when finding a document_file' do
|
180
|
+
it 'should find the right document_file by an attribute' do
|
181
|
+
title = 'The shizzle!'
|
182
|
+
document_file = MyDocument.find_by_title(title)
|
183
|
+
assert_equal title, document_file.title
|
184
|
+
end
|
185
|
+
|
186
|
+
it 'should find the right document_file by file_name' do
|
187
|
+
file_name = '2010-08-08-test-document-file'
|
188
|
+
document_file = MyDocument.find_by_file_name file_name
|
189
|
+
assert_equal document_file.file_name, file_name
|
190
|
+
end
|
191
|
+
|
192
|
+
it 'should not be confused by attributes that only some dcuments have' do
|
193
|
+
document_file = MyDocument.find_by_special_attribute 'Yes!'
|
194
|
+
assert_equal 'Yes!', document_file.special_attribute
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
describe 'when using a finder with offset or limit' do
|
199
|
+
it 'should respect the offset' do
|
200
|
+
document_files = MyDocument.find_all_by_tag 'tag', :offset => 1
|
201
|
+
assert_equal 1, document_files.size
|
202
|
+
assert_equal 2, document_files.first.id
|
203
|
+
end
|
204
|
+
|
205
|
+
it 'should respect the limit' do
|
206
|
+
document_files = MyDocument.find_all_by_tag 'tag', :limit => 1
|
207
|
+
assert_equal 1, document_files.size
|
208
|
+
assert_equal 1, document_files.first.id
|
209
|
+
end
|
210
|
+
|
211
|
+
it 'should still deliver the total number of documents' do
|
212
|
+
document_files = MyDocument.find_all_by_tag 'tag', :offset => 1
|
213
|
+
assert_equal 1, document_files.size
|
214
|
+
assert_equal 2, document_files.total
|
215
|
+
end
|
216
|
+
end
|
95
217
|
end
|
data/test/document_file_test.rb
CHANGED
@@ -106,88 +106,6 @@ eos
|
|
106
106
|
end
|
107
107
|
end
|
108
108
|
|
109
|
-
describe 'when listing document_files by an Array attribute' do
|
110
|
-
it 'should return a Hash' do
|
111
|
-
assert_equal Hash, MyDocument.by_tags.class
|
112
|
-
end
|
113
|
-
|
114
|
-
it 'should use the tags as Hash keys' do
|
115
|
-
assert_equal Set.new(['tag', 'tug']), MyDocument.by_tags.keys.to_set
|
116
|
-
end
|
117
|
-
|
118
|
-
it 'should use the document_files as Hash values' do
|
119
|
-
document_files = MyDocument.by_tags
|
120
|
-
assert_equal Set.new([1, 2]), document_files['tag'].map(&:id).to_set
|
121
|
-
assert_equal Set.new([2]), document_files['tug'].map(&:id).to_set
|
122
|
-
end
|
123
|
-
|
124
|
-
it 'should not be confused by attributes that only some documents have' do
|
125
|
-
document_files_by_authors = MyDocument.by_authors
|
126
|
-
assert_equal 1, document_files_by_authors['Frank'].first.id
|
127
|
-
|
128
|
-
document_files_by_friends = MyDocument.by_friends
|
129
|
-
assert_equal 2, document_files_by_friends['Anton'].first.id
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
describe 'when finding all document_files by an Array attribute value' do
|
134
|
-
it 'should return a DocumentFile::Collection' do
|
135
|
-
klass = MyDocument.find_all_by_tag('tag').class
|
136
|
-
assert_equal DocumentFile::Collection, klass
|
137
|
-
end
|
138
|
-
|
139
|
-
it 'should containt documents' do
|
140
|
-
assert_equal MyDocument, MyDocument.find_all_by_tag('tag').first.class
|
141
|
-
end
|
142
|
-
|
143
|
-
it 'should return the right documents' do
|
144
|
-
assert_equal [1, 2], MyDocument.find_all_by_tag('tag').map(&:id)
|
145
|
-
assert_equal [2], MyDocument.find_all_by_tag('tug').map(&:id)
|
146
|
-
end
|
147
|
-
end
|
148
|
-
|
149
|
-
describe 'when finding all document_files by an attribute value' do
|
150
|
-
before do
|
151
|
-
@collection = MyDocument.find_all_by_status(:published)
|
152
|
-
end
|
153
|
-
|
154
|
-
it 'should return a DocumentFile::Collection' do
|
155
|
-
assert_equal DocumentFile::Collection, @collection.class
|
156
|
-
end
|
157
|
-
|
158
|
-
it 'should containt documents' do
|
159
|
-
assert_equal MyDocument, @collection.first.class
|
160
|
-
end
|
161
|
-
|
162
|
-
it 'should return the right documents' do
|
163
|
-
assert_equal [1, 2], @collection.map(&:id)
|
164
|
-
end
|
165
|
-
|
166
|
-
it 'should return an empty collection if the document was not found' do
|
167
|
-
empty_collection = MyDocument.find_all_by_status(:draft)
|
168
|
-
assert_equal [], empty_collection.map(&:id)
|
169
|
-
end
|
170
|
-
end
|
171
|
-
|
172
|
-
describe 'when finding a document_file' do
|
173
|
-
it 'should find the right document_file by an attribute' do
|
174
|
-
title = 'The shizzle!'
|
175
|
-
document_file = MyDocument.find_by_title(title)
|
176
|
-
assert_equal title, document_file.title
|
177
|
-
end
|
178
|
-
|
179
|
-
it 'should find the right document_file by file_name' do
|
180
|
-
file_name = '2010-08-08-test-document-file'
|
181
|
-
document_file = MyDocument.find_by_file_name file_name
|
182
|
-
assert_equal document_file.file_name, file_name
|
183
|
-
end
|
184
|
-
|
185
|
-
it 'should not be confused by attributes that only some dcuments have' do
|
186
|
-
document_file = MyDocument.find_by_special_attribute 'Yes!'
|
187
|
-
assert_equal 'Yes!', document_file.special_attribute
|
188
|
-
end
|
189
|
-
end
|
190
|
-
|
191
109
|
describe 'when getting the file name or file path' do
|
192
110
|
before do
|
193
111
|
@file_name = '2010-08-08-test-document-file.textile'
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 7
|
9
|
+
version: 0.0.7
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Ralph von der Heyden
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-09-
|
17
|
+
date: 2010-09-20 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|