document_mapper 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,10 +3,10 @@ module DocumentMapper
3
3
  module Read
4
4
  extend ActiveSupport::Concern
5
5
 
6
- included do
7
- # Undefine id so it can be used as an attribute name
8
- undef_method(:id) if method_defined?(:id)
9
- end
6
+ # included do
7
+ # # Undefine id so it can be used as an attribute name
8
+ # undef_method(:id) if method_defined?(:id)
9
+ # end
10
10
 
11
11
  module ClassMethods
12
12
  def define_read_method(attr_name)
@@ -6,4 +6,10 @@ class Symbol
6
6
  end
7
7
  OPERATORS
8
8
  end
9
+
10
+ unless method_defined?(:"<=>")
11
+ def <=>(other)
12
+ self.to_s <=> other.to_s
13
+ end
14
+ end
9
15
  end
@@ -8,7 +8,7 @@ module DocumentMapper
8
8
  include ToHtml
9
9
  include YamlParsing
10
10
 
11
- attr_accessor :attributes, :content, :directory
11
+ attr_accessor :attributes, :content
12
12
 
13
13
  included do
14
14
  @@documents = []
@@ -26,7 +26,7 @@ module DocumentMapper
26
26
 
27
27
  def reload
28
28
  self.reset
29
- self.directory = @@directory
29
+ self.directory = @@directory.path
30
30
  end
31
31
 
32
32
  def from_file(file_path)
@@ -47,7 +47,7 @@ module DocumentMapper
47
47
  self.reset
48
48
  @@directory = Dir.new File.expand_path(new_directory)
49
49
  @@directory.each do |file|
50
- next if file[0] == '.'
50
+ next if file[0,1] == '.'
51
51
  self.from_file [@@directory.path, file].join('/')
52
52
  end
53
53
  end
@@ -55,7 +55,7 @@ module DocumentMapper
55
55
  def select(options = {})
56
56
  documents = @@documents.dup
57
57
  options[:where].each do |selector, selector_value|
58
- documents.select! do |document|
58
+ documents = documents.select do |document|
59
59
  next unless document.attributes.has_key? selector.attribute
60
60
  document_value = document.send(selector.attribute)
61
61
  operator = OPERATOR_MAPPING[selector.operator]
@@ -66,10 +66,12 @@ module DocumentMapper
66
66
  if options[:order_by].present?
67
67
  order_attribute = options[:order_by].keys.first
68
68
  asc_or_desc = options[:order_by].values.first
69
- documents.select! do |document|
69
+ documents = documents.select do |document|
70
70
  document.attributes.include? order_attribute
71
71
  end
72
- documents.sort_by! { |document| document.send order_attribute }
72
+ documents = documents.sort_by do |document|
73
+ document.send order_attribute
74
+ end
73
75
  documents.reverse! if asc_or_desc == :desc
74
76
  end
75
77
 
@@ -6,9 +6,8 @@ module DocumentMapper
6
6
  end
7
7
 
8
8
  def where(constraints_hash)
9
- differentiator = ->(key, value){ key.is_a? Selector }
10
- selector_hash = constraints_hash.select &differentiator
11
- symbol_hash = constraints_hash.reject &differentiator
9
+ selector_hash = constraints_hash.reject { |key, value| !key.is_a? Selector }
10
+ symbol_hash = constraints_hash.reject { |key, value| key.is_a? Selector }
12
11
  symbol_hash.each do |attribute, value|
13
12
  selector = Selector.new(:attribute => attribute, :operator => 'equal')
14
13
  selector_hash.update({ selector => value })
@@ -43,7 +42,7 @@ module DocumentMapper
43
42
  def all
44
43
  result = @model.select(:where => @where, :order_by => @order_by)
45
44
  if @offset.present?
46
- result = result.last(result.size - @offset)
45
+ result = result.last([result.size - @offset, 0].max)
47
46
  end
48
47
  if @limit.present?
49
48
  result = result.first(@limit)
@@ -1,3 +1,3 @@
1
1
  module DocumentMapper
2
- VERSION = '0.1.4'
2
+ VERSION = '0.1.5'
3
3
  end
@@ -121,6 +121,10 @@ describe MyDocument do
121
121
  it 'should support offset and limit at the same time' do
122
122
  assert_equal_set [2,3], MyDocument.order_by(:id).offset(1).limit(2).all.map(&:id)
123
123
  end
124
+
125
+ it 'should not freak out about an offset higher than the document count' do
126
+ assert_equal_set [], MyDocument.order_by(:id).offset(5).all
127
+ end
124
128
  end
125
129
 
126
130
  describe 'resetting the MyDocument class' do
data/test/test_base.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
1
3
  require 'set'
2
4
  require 'minitest/spec'
3
5
  MiniTest::Unit.autorun
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: document_mapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
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: 2011-06-23 00:00:00.000000000Z
12
+ date: 2011-07-20 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &2153304900 !ruby/object:Gem::Requirement
16
+ requirement: &2156281780 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2153304900
24
+ version_requirements: *2156281780
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activemodel
27
- requirement: &2153304400 !ruby/object:Gem::Requirement
27
+ requirement: &2156280460 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 3.0.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2153304400
35
+ version_requirements: *2156280460
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &2153303940 !ruby/object:Gem::Requirement
38
+ requirement: &2156278980 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 0.9.0
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *2153303940
46
+ version_requirements: *2156278980
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: tilt
49
- requirement: &2153335980 !ruby/object:Gem::Requirement
49
+ requirement: &2156278120 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 1.3.0
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *2153335980
57
+ version_requirements: *2156278120
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: RedCloth
60
- requirement: &2153335520 !ruby/object:Gem::Requirement
60
+ requirement: &2156277280 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,7 +65,18 @@ dependencies:
65
65
  version: 4.2.0
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *2153335520
68
+ version_requirements: *2156277280
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: &2156276220 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 2.3.1
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *2156276220
69
80
  description: ! ' DocumentMapper is an object mapper for plain text documents. The
70
81
  documents look like the ones used in jekyll (http://github.com/mojombo/jekyll).
71
82
  They consist of a preambel written in YAML (also called YAML front matter), and
@@ -113,12 +124,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
113
124
  - - ! '>='
114
125
  - !ruby/object:Gem::Version
115
126
  version: '0'
127
+ segments:
128
+ - 0
129
+ hash: 3820388701645222296
116
130
  required_rubygems_version: !ruby/object:Gem::Requirement
117
131
  none: false
118
132
  requirements:
119
133
  - - ! '>='
120
134
  - !ruby/object:Gem::Version
121
135
  version: '0'
136
+ segments:
137
+ - 0
138
+ hash: 3820388701645222296
122
139
  requirements: []
123
140
  rubyforge_project:
124
141
  rubygems_version: 1.8.5