document_mapper 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -97,6 +97,13 @@ MyDocument.where(:year.lt => 2010) # year < 2010
97
97
  MyDocument.where(:year.lte => 2010) # year <= 2010
98
98
  </code></pre>
99
99
 
100
+ While retrieving documents, you can also define the way the documents should be ordered. By default, the documents will be returned in the order they were loaded from the file system, which usually means by file name ascending. If you define an ordering, the documents that don't own the ordering attribute will be excluded.
101
+
102
+ <pre><code>MyDocument.order_by(:title => :asc).all # Order by title attribute, ascending
103
+ MyDocument.order_by(:title).all # Same as order_by(:title => :asc)
104
+ MyDocument.order_by(:title => :desc).all # Order by title attribute, descending
105
+ </code></pre>
106
+
100
107
 
101
108
  h3. Chaining
102
109
 
@@ -61,6 +61,17 @@ module DocumentMapper
61
61
  document_value.send operator, selector_value
62
62
  end
63
63
  end
64
+
65
+ if options[:order_by].present?
66
+ order_attribute = options[:order_by].keys.first
67
+ asc_or_desc = options[:order_by].values.first
68
+ documents.select! do |document|
69
+ document.attributes.include? order_attribute
70
+ end
71
+ documents.sort_by! { |document| document.send order_attribute }
72
+ documents.reverse! if asc_or_desc == :desc
73
+ end
74
+
64
75
  documents
65
76
  end
66
77
 
@@ -68,8 +79,8 @@ module DocumentMapper
68
79
  Query.new(self).where(hash)
69
80
  end
70
81
 
71
- def sort(field)
72
- Query.new(self).sort(field)
82
+ def order_by(field)
83
+ Query.new(self).order_by(field)
73
84
  end
74
85
 
75
86
  def offset(number)
@@ -17,8 +17,8 @@ module DocumentMapper
17
17
  self
18
18
  end
19
19
 
20
- def sort(field)
21
- @sort = field
20
+ def order_by(field)
21
+ @order_by = field.is_a?(Symbol) ? {field => :asc} : field
22
22
  self
23
23
  end
24
24
 
@@ -41,7 +41,7 @@ module DocumentMapper
41
41
  end
42
42
 
43
43
  def all
44
- result = @model.select(:where => @where, :sort => @sort)
44
+ result = @model.select(:where => @where, :order_by => @order_by)
45
45
  if @offset.present?
46
46
  result = result.last(result.size - @offset)
47
47
  end
@@ -1,3 +1,3 @@
1
1
  module DocumentMapper
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
@@ -241,6 +241,32 @@ describe MyDocument do
241
241
  end
242
242
  end
243
243
 
244
+ describe 'sorting the documents' do
245
+ before do
246
+ MyDocument.directory = 'test/documents'
247
+ end
248
+
249
+ it 'should support ordering by attribute ascending' do
250
+ found_documents = MyDocument.order_by(:title => :asc).all
251
+ assert_equal [2,3,1,4], found_documents.map(&:id)
252
+ end
253
+
254
+ it 'should support ordering by attribute descending' do
255
+ found_documents = MyDocument.order_by(:title => :desc).all
256
+ assert_equal [4,1,3,2], found_documents.map(&:id)
257
+ end
258
+
259
+ it 'should order by attribute ascending by default' do
260
+ found_documents = MyDocument.order_by(:title).all
261
+ assert_equal [2,3,1,4], found_documents.map(&:id)
262
+ end
263
+
264
+ it 'should exclude documents that do not own the attribute' do
265
+ found_documents = MyDocument.order_by(:status).all
266
+ assert_equal [1,2], found_documents.map(&:id)
267
+ end
268
+ end
269
+
244
270
  describe 'reloading the Document class' do
245
271
  it 'should discover new documents' do
246
272
  @file_path = 'test/documents/2011-04-26-new-stuff.textile'
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: document_mapper
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.1
5
+ version: 0.1.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ralph von der Heyden
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-02 00:00:00 Z
13
+ date: 2011-05-16 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -87,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
87
  requirements: []
88
88
 
89
89
  rubyforge_project:
90
- rubygems_version: 1.7.2
90
+ rubygems_version: 1.8.2
91
91
  signing_key:
92
92
  specification_version: 3
93
93
  summary: DocumentMapper is an object mapper for plain text documents.