mordor 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -8,6 +8,7 @@ class ExampleResource
8
8
  attribute :first, :index => true
9
9
  attribute :second
10
10
  attribute :third, :finder_method => :find_by_third_attribute
11
+ attribute :at, :timestamp => true
11
12
  end
12
13
  ```
13
14
 
@@ -17,3 +18,8 @@ as can be seen with the third attribute.
17
18
 
18
19
  When the `:index => true` option is set, indices are ensured before each query on
19
20
  the collection. Indices are descending by default, but this can be changed by also supplying a `:index_type => Mongo::ASCENDING` option.
21
+
22
+ At most one attribute per Resource can have the option `:timestamp => true` set. This means that the attribute will be saved as one of the two first
23
+ attributes (the other one being the `_id` attribute. When no value is given for the timestamped attribute, a timestamp with value 0 will be inserted,
24
+ which results in a timestamp being assigned to it by the MongoDB.
25
+ An exception is raised when more than one attribute is given the timestamp option
@@ -52,10 +52,29 @@ module Mordor
52
52
  return !new?
53
53
  end
54
54
 
55
+ def reload
56
+ return unless _id
57
+ res = self.class.get(_id).to_hash.each do |k, v|
58
+ self.send("#{k}=".to_sym, v)
59
+ end
60
+ self
61
+ end
62
+
55
63
  def save
56
64
  unless self._id
57
- insert_id = self.class.collection.insert(self.to_hash)
65
+ self_hash = self.to_hash
66
+ if timestamp_attribute = self.class.timestamped_attribute
67
+ timestamp_value = self_hash.delete(timestamp_attribute)
68
+ ordered_self_hash = BSON::OrderedHash.new
69
+ ordered_self_hash[timestamp_attribute] = (timestamp_value.nil? || timestamp_value.empty?) ? BSON::Timestamp.new(0, 0) : timestamp_value
70
+ self_hash.each do |key, value|
71
+ ordered_self_hash[key] = value
72
+ end
73
+ self_hash = ordered_self_hash
74
+ end
75
+ insert_id = self.class.collection.insert(self_hash)
58
76
  self._id = insert_id
77
+ self.reload
59
78
  else
60
79
  insert_id = self.update
61
80
  end
@@ -148,6 +167,9 @@ module Mordor
148
167
  Collection.new(self, cursor)
149
168
  end
150
169
 
170
+ def timestamped_attribute
171
+ @timestamped_attribute
172
+ end
151
173
 
152
174
  def attribute(name, options = {})
153
175
  @attributes ||= []
@@ -160,6 +182,11 @@ module Mordor
160
182
  @index_types[name] = options[:index_type] ? options[:index_type] : Mongo::DESCENDING
161
183
  end
162
184
 
185
+ if options[:timestamp]
186
+ raise ArgumentError.new("Only one timestamped attribute is allowed, '#{@timestamped_attribute}' is already timestamped") unless @timestamped_attribute.nil?
187
+ @timestamped_attribute = name
188
+ end
189
+
163
190
  method_name = options.key?(:finder_method) ? options[:finder_method] : "find_by_#{name}"
164
191
 
165
192
  class_eval <<-EOS, __FILE__, __LINE__
@@ -2,8 +2,8 @@ Gem::Specification.new do |s|
2
2
  s.name = "mordor"
3
3
 
4
4
  # Do not set the version and date field manually, this is done by the release script
5
- s.version = "0.2.1"
6
- s.date = "2011-12-27"
5
+ s.version = "0.2.2"
6
+ s.date = "2011-12-28"
7
7
 
8
8
  s.summary = "mordor"
9
9
  s.description = <<-eos
@@ -2,19 +2,20 @@ require File.join(File.dirname(__FILE__), '..', '/spec_helper.rb')
2
2
 
3
3
  describe "with respect to resources" do
4
4
  before :each do
5
+ Object.send(:remove_const, :TestResource) if Object.const_defined?(:TestResource)
5
6
  class TestResource
6
7
  include Mordor::Resource
7
8
 
8
9
  attribute :first, :index => true
9
10
  attribute :second, :index => true, :index_type => Mongo::ASCENDING
10
11
  attribute :third, :finder_method => :find_by_third_attribute
12
+ attribute :at, :timestamp => true
11
13
 
12
14
  # Put this in here again to ensure the original method is still here
13
15
  class_eval do
14
16
  def self.ensure_indices
15
17
  collection.ensure_index( indices.map{|index| [index.to_s, Mongo::DESCENDING]} ) if indices.any?
16
18
  end
17
-
18
19
  end
19
20
  end
20
21
  end
@@ -49,6 +50,10 @@ describe "with respect to resources" do
49
50
 
50
51
  it "should call ensure_index on the collection for each index when a query is performed" do
51
52
  TestResource.class_eval do
53
+ def self.reset_ensure_count
54
+ @count = 0
55
+ end
56
+
52
57
  def self.ensure_count
53
58
  @count ||= 0
54
59
  end
@@ -70,10 +75,31 @@ describe "with respect to resources" do
70
75
  end
71
76
  end
72
77
  TestResource.create({:first => 'first', :second => 'second', :third => 'third'})
78
+ TestResource.reset_ensure_count
73
79
  TestResource.all()
74
80
  TestResource.ensure_count.should == 1
75
81
  end
76
82
 
83
+ it "should be possible to designate an attribute as a timestamp" do
84
+ TestResource.timestamped_attribute.should_not be_nil
85
+ TestResource.timestamped_attribute.should == :at
86
+ end
87
+
88
+ it "should only be possible to have one attribute as a timestamp" do
89
+ lambda {
90
+ TestResource2.class_eval do
91
+ attribute :some_timestamp, :timestamp => true
92
+ attribute :another_timestamp, :timestamp => true
93
+ end
94
+ }.should raise_error
95
+ end
96
+
97
+ it "should provide timestamped attribute as first attribute when creating a Resource" do
98
+ tr = TestResource.create({:first => 'first'})
99
+ tr.at.should_not be_nil
100
+ TestResource.get(tr._id).at.should_not == BSON::Timestamp.new(0,0)
101
+ end
102
+
77
103
  context "with respect to replacing params" do
78
104
  before :each do
79
105
  clean_sheet
@@ -111,7 +137,7 @@ describe "with respect to resources" do
111
137
  it "should correctly respond to to_hash" do
112
138
  resource = TestResource.new({:first => "first", :second => "second", :third => "third"})
113
139
  hash = resource.to_hash
114
- hash.size.should == 3
140
+ hash.size.should == 4
115
141
  hash[:first].should == "first"
116
142
  hash[:second].should == "second"
117
143
  hash[:third].should == "third"
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mordor
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 1
10
- version: 0.2.1
9
+ - 2
10
+ version: 0.2.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jan-Willem Koelewijn
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-12-27 00:00:00 +01:00
19
+ date: 2011-12-28 00:00:00 +01:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency