document_mapper 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -89,11 +89,12 @@ Not all of the documents in the directory need to have all of the attributes. Yo
89
89
 
90
90
  The document queries do support more operators than just equality. The following operators are available:
91
91
 
92
- <pre><code>MyDocument.where(:year.gt => 2010)
93
- MyDocument.where(:year.gte => 2010)
94
- MyDocument.where(:year.in => [2010,2011])
95
- MyDocument.where(:year.lt => 2010)
96
- MyDocument.where(:year.lte => 2010)
92
+ <pre><code>MyDocument.where(:year.gt => 2010) # year > 2010
93
+ MyDocument.where(:year.gte => 2010) # year >= 2010
94
+ MyDocument.where(:year.in => [2010,2011]) # year one of [2010,2011]
95
+ MyDocument.where(:tags.include => 'ruby') # 'ruby' is included in tags = ['ruby', 'rails', ...]
96
+ MyDocument.where(:year.lt => 2010) # year < 2010
97
+ MyDocument.where(:year.lte => 2010) # year <= 2010
97
98
  </code></pre>
98
99
 
99
100
 
@@ -10,14 +10,8 @@ module DocumentMapper
10
10
 
11
11
  module ClassMethods
12
12
  def define_read_method(attr_name)
13
- access_code = "attributes['#{attr_name}']"
13
+ access_code = "attributes[:#{attr_name}]"
14
14
  generated_attribute_methods.module_eval("def #{attr_name}; #{access_code}; end", __FILE__, __LINE__)
15
-
16
- %w(year month day).each do |attr_name|
17
- access_code = "date.#{attr_name} if date"
18
- generated_attribute_methods.module_eval("def #{attr_name}; #{access_code}; end", __FILE__, __LINE__)
19
- end
20
-
21
15
  end
22
16
  end
23
17
  end
@@ -1,12 +1,13 @@
1
1
  module DocumentMapper
2
- REVERSE_OPERATOR_MAPPING = {
3
- 'equal' => :==,
4
- 'gt' => :<,
5
- 'gte' => :<=,
6
- 'in' => :include?,
7
- 'lt' => :>,
8
- 'lte' => :>=
2
+ OPERATOR_MAPPING = {
3
+ 'equal' => :==,
4
+ 'gt' => :>,
5
+ 'gte' => :>=,
6
+ 'in' => :in?,
7
+ 'include' => :include?,
8
+ 'lt' => :<,
9
+ 'lte' => :<=
9
10
  }
10
11
 
11
- VALID_OPERATORS = REVERSE_OPERATOR_MAPPING.keys
12
+ VALID_OPERATORS = OPERATOR_MAPPING.keys
12
13
  end
@@ -0,0 +1,18 @@
1
+ # Backported from ActiveSupport 3.1. Should be removed once the 3.1
2
+ # version is out.
3
+
4
+ class Object
5
+ # Returns true if this object is included in the argument. Argument must be
6
+ # any object which responds to +#include?+. Usage:
7
+ #
8
+ # characters = ["Konata", "Kagami", "Tsukasa"]
9
+ # "Konata".in?(characters) # => true
10
+ #
11
+ # This will throw an ArgumentError if the argument doesn't respond
12
+ # to +#include?+.
13
+ def in?(another_object)
14
+ another_object.include?(self)
15
+ rescue NoMethodError
16
+ raise ArgumentError.new("The parameter passed to #in? must respond to #include?")
17
+ end
18
+ end
@@ -2,7 +2,7 @@ class Symbol
2
2
  DocumentMapper::VALID_OPERATORS.each do |operator|
3
3
  class_eval <<-OPERATORS
4
4
  def #{operator}
5
- Selector.new(:attribute => self, :operator => '#{operator}')
5
+ DocumentMapper::Selector.new(:attribute => self, :operator => '#{operator}')
6
6
  end
7
7
  OPERATORS
8
8
  end
@@ -7,7 +7,7 @@ module DocumentMapper
7
7
  include AttributeMethods::Read
8
8
  include YamlParsing
9
9
 
10
- attr_accessor :attributes, :content, :directory, :file_path
10
+ attr_accessor :attributes, :content, :directory
11
11
 
12
12
  included do
13
13
  @@documents = []
@@ -33,7 +33,9 @@ module DocumentMapper
33
33
  raise FileNotFoundError
34
34
  end
35
35
  self.new.tap do |document|
36
- document.file_path = File.expand_path(file_path)
36
+ document.attributes = {
37
+ :file_path => File.expand_path(file_path)
38
+ }
37
39
  document.read_yaml
38
40
  @@documents << document
39
41
  end
@@ -53,10 +55,10 @@ module DocumentMapper
53
55
  documents = @@documents.dup
54
56
  options[:where].each do |selector, selector_value|
55
57
  documents.select! do |document|
56
- next unless document.respond_to? selector.attribute
58
+ next unless document.attributes.has_key? selector.attribute
57
59
  document_value = document.send(selector.attribute)
58
- operator = REVERSE_OPERATOR_MAPPING[selector.operator]
59
- selector_value.send operator, document_value
60
+ operator = OPERATOR_MAPPING[selector.operator]
61
+ document_value.send operator, selector_value
60
62
  end
61
63
  end
62
64
  documents
@@ -1,3 +1,3 @@
1
1
  module DocumentMapper
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
@@ -1,22 +1,37 @@
1
1
  module DocumentMapper
2
2
  module YamlParsing
3
3
  def read_yaml
4
+ file_path = self.attributes[:file_path]
4
5
  @content = File.read(file_path)
5
6
 
6
7
  if @content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
7
8
  @content = @content[($1.size + $2.size)..-1]
8
- self.attributes = YAML.load($1)
9
+ self.attributes.update(YAML.load($1).symbolize_keys)
9
10
  end
10
11
 
11
- self.attributes ||= {}
12
- if !self.attributes.has_key? 'date'
12
+ file_name = File.basename(file_path)
13
+ extension = File.extname(file_path)
14
+ self.attributes.update({
15
+ :file_name => file_name,
16
+ :extension => extension.sub(/^\./, ''),
17
+ :file_name_without_extension => File.basename(file_path, extension)
18
+ })
19
+
20
+ if !self.attributes.has_key? :date
13
21
  begin
14
- match = File.basename(@file_path).match(/(\d{4})-(\d{1,2})-(\d{1,2}).*/)
15
- self.attributes['date'] = Date.new(match[1].to_i, match[2].to_i, match[3].to_i)
22
+ match = attributes[:file_name].match(/(\d{4})-(\d{1,2})-(\d{1,2}).*/)
23
+ year, month, day = match[1].to_i, match[2].to_i, match[3].to_i
24
+ self.attributes[:date] = Date.new(year, month, day)
16
25
  rescue NoMethodError => err
17
26
  end
18
27
  end
19
28
 
29
+ if self.attributes.has_key? :date
30
+ self.attributes[:year] = self.attributes[:date].year
31
+ self.attributes[:month] = self.attributes[:date].month
32
+ self.attributes[:day] = self.attributes[:date].day
33
+ end
34
+
20
35
  self.class.define_attribute_methods self.attributes.keys
21
36
  self.attributes.keys.each { |attr| self.class.define_read_method attr }
22
37
  end
@@ -11,4 +11,5 @@ require 'document_mapper/query'
11
11
  require 'document_mapper/selector'
12
12
  require 'document_mapper/version'
13
13
 
14
+ require 'document_mapper/core_ext/object'
14
15
  require 'document_mapper/core_ext/symbol'
@@ -13,10 +13,10 @@ describe MyDocument do
13
13
  end
14
14
 
15
15
  it 'should load the document from a yaml file' do
16
- assert_equal 1, @document.attributes['id']
17
- assert_equal 'Some fancy title', @document.attributes['title']
18
- assert_equal ['ruby'], @document.attributes['tags']
19
- assert_equal :published, @document.attributes['status']
16
+ assert_equal 1, @document.attributes[:id]
17
+ assert_equal 'Some fancy title', @document.attributes[:title]
18
+ assert_equal ['ruby'], @document.attributes[:tags]
19
+ assert_equal :published, @document.attributes[:status]
20
20
  end
21
21
 
22
22
  it 'should specify attributes from the YAML header' do
@@ -24,9 +24,26 @@ describe MyDocument do
24
24
  assert_equal 'Some fancy title', @document.title
25
25
  assert_equal ['ruby'], @document.tags
26
26
  assert_equal :published, @document.status
27
+ end
28
+
29
+ it 'should return the file path' do
27
30
  assert_equal File.expand_path(@file_path), @document.file_path
28
31
  end
29
32
 
33
+ it 'should return the file name' do
34
+ expected_name = '2010-08-08-test-document-file.textile'
35
+ assert_equal expected_name, @document.file_name
36
+ end
37
+
38
+ it 'should return the file name without extension' do
39
+ expected_name = '2010-08-08-test-document-file'
40
+ assert_equal expected_name, @document.file_name_without_extension
41
+ end
42
+
43
+ it 'should return the extension' do
44
+ assert_equal 'textile', @document.extension
45
+ end
46
+
30
47
  describe 'specifying the date of the document' do
31
48
  it 'should get the date from the filename' do
32
49
  assert_equal '2010-08-08', @document.date.to_s
@@ -131,6 +148,19 @@ describe MyDocument do
131
148
  assert_equal @document_1, document_proxy.first
132
149
  end
133
150
 
151
+ it 'should work with file names without extensions' do
152
+ file_name = '2010-08-08-test-document-file'
153
+ selector_hash = { :file_name_without_extension => file_name }
154
+ found_document = MyDocument.where(selector_hash).first
155
+ assert_equal sample_document_1, found_document
156
+ end
157
+
158
+ it 'should work with file names' do
159
+ file_name = '2010-08-08-test-document-file.textile'
160
+ found_document = MyDocument.where(:file_name => file_name).first
161
+ assert_equal sample_document_1, found_document
162
+ end
163
+
134
164
  it 'should work with dates' do
135
165
  found_documents = MyDocument.where(:year => 2010).all
136
166
  expected_documents = [sample_document_1, sample_document_2]
@@ -146,7 +176,7 @@ describe MyDocument do
146
176
 
147
177
  describe 'with a gt operator' do
148
178
  it 'should return the right documents' do
149
- selector = Selector.new :attribute => 'id', :operator => 'gt'
179
+ selector = Selector.new :attribute => :id, :operator => 'gt'
150
180
  found_documents = MyDocument.where(selector => 2).all
151
181
  assert_equal [3,4], found_documents.map(&:id)
152
182
  end
@@ -154,7 +184,7 @@ describe MyDocument do
154
184
 
155
185
  describe 'with a gte operator' do
156
186
  it 'should return the right documents' do
157
- selector = Selector.new :attribute => 'id', :operator => 'gte'
187
+ selector = Selector.new :attribute => :id, :operator => 'gte'
158
188
  found_documents = MyDocument.where(selector => 2).all
159
189
  assert_equal [2,3,4], found_documents.map(&:id)
160
190
  end
@@ -162,7 +192,7 @@ describe MyDocument do
162
192
 
163
193
  describe 'with an in operator' do
164
194
  it 'should return the right documents' do
165
- selector = Selector.new :attribute => 'id', :operator => 'in'
195
+ selector = Selector.new :attribute => :id, :operator => 'in'
166
196
  found_documents = MyDocument.where(selector => [2,3]).all
167
197
  assert_equal [2,3], found_documents.map(&:id)
168
198
  end
@@ -170,7 +200,7 @@ describe MyDocument do
170
200
 
171
201
  describe 'with an lt operator' do
172
202
  it 'should return the right documents' do
173
- selector = Selector.new :attribute => 'id', :operator => 'lt'
203
+ selector = Selector.new :attribute => :id, :operator => 'lt'
174
204
  found_documents = MyDocument.where(selector => 2).all
175
205
  assert_equal [1], found_documents.map(&:id)
176
206
  end
@@ -178,16 +208,24 @@ describe MyDocument do
178
208
 
179
209
  describe 'with an lte operator' do
180
210
  it 'should return the right documents' do
181
- selector = Selector.new :attribute => 'id', :operator => 'lte'
211
+ selector = Selector.new :attribute => :id, :operator => 'lte'
182
212
  found_documents = MyDocument.where(selector => 2).all
183
213
  assert_equal [1,2], found_documents.map(&:id)
184
214
  end
185
215
  end
186
216
 
217
+ describe 'with an include operator' do
218
+ it 'include should return the right documents' do
219
+ selector = Selector.new :attribute => :tags, :operator => 'include'
220
+ found_documents = MyDocument.where(selector => 'ruby').all
221
+ assert_equal [1,2], found_documents.map(&:id)
222
+ end
223
+ end
224
+
187
225
  describe 'with mixed operators' do
188
226
  it 'should return the right documents' do
189
- in_selector = Selector.new :attribute => 'id', :operator => 'in'
190
- gt_selector = Selector.new :attribute => 'id', :operator => 'gt'
227
+ in_selector = Selector.new :attribute => :id, :operator => 'in'
228
+ gt_selector = Selector.new :attribute => :id, :operator => 'gt'
191
229
  documents_proxy = MyDocument.where(in_selector => [2,3])
192
230
  found_documents = documents_proxy.where(gt_selector => 2).all
193
231
  assert_equal [3], found_documents.map(&:id)
@@ -196,7 +234,7 @@ describe MyDocument do
196
234
 
197
235
  describe 'using multiple constrains in one where' do
198
236
  it 'should return the right documents' do
199
- selector = Selector.new :attribute => 'id', :operator => 'lte'
237
+ selector = Selector.new :attribute => :id, :operator => 'lte'
200
238
  found_documents = MyDocument.where(selector => 2, :status => :published).all
201
239
  assert_equal [1,2], found_documents.map(&:id)
202
240
  end
@@ -3,14 +3,14 @@ include DocumentMapper
3
3
 
4
4
  describe Selector do
5
5
  it 'should initialize with an attribute and an operator' do
6
- selector = Selector.new :attribute => 'author', :operator => 'equal'
7
- assert_equal 'author', selector.attribute
6
+ selector = Selector.new :attribute => :author, :operator => 'equal'
7
+ assert_equal :author, selector.attribute
8
8
  assert_equal 'equal', selector.operator
9
9
  end
10
10
 
11
11
  it 'should raise an exception if the operator is not supported' do
12
12
  assert_raises OperatorNotSupportedError do
13
- selector = Selector.new :attribute => 'author', :operator => 'zomg'
13
+ selector = Selector.new :attribute => :author, :operator => 'zomg'
14
14
  end
15
15
  end
16
16
  end
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.0
5
+ version: 0.1.1
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-04-30 00:00:00 Z
13
+ date: 2011-05-02 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -47,6 +47,7 @@ files:
47
47
  - README.textile
48
48
  - lib/document_mapper/attribute_methods.rb
49
49
  - lib/document_mapper/constants.rb
50
+ - lib/document_mapper/core_ext/object.rb
50
51
  - lib/document_mapper/core_ext/symbol.rb
51
52
  - lib/document_mapper/document.rb
52
53
  - lib/document_mapper/errors.rb