mongoid-sequence2 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MmQwNjQ3YTBkYWQ3MDNhNmExNWEwNWIyZTM3NDRkYzMwMTA4ZTk5OA==
4
+ OWM3NzA3NWExMGFmOWQzZTg3YWI3ZWJiMjYzOGM4YmJiNjAzNWUyZQ==
5
5
  data.tar.gz: !binary |-
6
- ODlkMDY0ODU1YjBmZjgwNWU0MWQyZTdmYWY4OWY0ZWExOTE4ZTE3NQ==
6
+ OGI3YTVkMjJjMGIzNDg5ZDhiZGM3YjY5YzgzMmViZjc4OTY0OWM4Zg==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- ZmU4NDc0MWJiMjU2MjBkNzQzNDU4ZDA5ZTIyMjk0Yzc2YjBhYzA4ZGQ0ZDYx
10
- OWMzNWNkODJhMzVkNWEyY2ViOGVkMjBjMGI3ZTc3YzE2ODlmYjgzMTZmZTdl
11
- MmIxNWExNGNiOWEwZGE4NDdjODk1ODZkZTk1NmJhYjNkOWUwNTE=
9
+ OWQzNTlhMjFkNmNkNTQzNmQ3NjI2NmU5MmUzODFkYjg2MDY5MmYwNGJiMThk
10
+ MjQ5YTY2OWY4NjE2MGY0M2U3YmYyM2YwZmRjNzkxYzU2MzRhNWYwMTJkODhk
11
+ ZmI3NTBlOTY2ZGJkNDRmOTUwZGE3NDcwNThmNDIwYzI2ZTBkNjY=
12
12
  data.tar.gz: !binary |-
13
- YTQwMjdiN2JkMjk5YTYxOGMyZTk2OGVlNTIxZDE0ZDYxODdhNmMyYzA2YTMz
14
- ZDRiOTVlNWEyYWE3NzVmZGRmNzE2ODM5NmQ2MDZhMDA3ZTFhYTY3NjE4MDdm
15
- MDY0OTgxYjI2ZGZjZDAxNDhhMjQyYjFiZWUyYWE0NDBlNmRhNDg=
13
+ NDhlN2U0NjIyZTcyOWZlNTg1MzBlMWJlODliOTBjY2YzOGMxMjQ5MWY5YmQ2
14
+ MGQ1ZjVjZjgxNDJmN2NmZGRmMDI4NzFkNDQwOWVmZGY4NTdlY2RjMWQxMDE0
15
+ MzdlZWNlYTg4NDE4NjhlODc3OGRlZDVmOWMyOTNlNDA1NWZhZWE=
@@ -1,3 +1,8 @@
1
+ ## 0.2.5
2
+
3
+ * #1 Add `next_sequence` which allows to have any per model sequence, without need to create given model's objects. ([dawid-sklodowski](https://github.com/dawid-sklodowski))
4
+ * `next_sequence` support give an special step value, but only work with no `auto_increment` field
5
+
1
6
  ## 0.2.4
2
7
 
3
8
  * fix call `sequence` with `nil` error, usually just `include Mongoid::Sequence` and no `sequence` been called will throw `undefined method `each' for nil:NilClas`
data/README.md CHANGED
@@ -60,6 +60,26 @@ s2 = Sequenced.create
60
60
  s2.id #=> 2 # and so on
61
61
  ```
62
62
 
63
+ You can also have any per model sequence without need to create given model's objects:
64
+
65
+ ```ruby
66
+ class Sequenced
67
+ include Mongoid::Document
68
+ include Mongoid::Sequence
69
+ end
70
+
71
+ Sequenced.next_sequence('some_name_1') #=> 1
72
+ Sequenced.next_sequence('some_name_1') #=> 2
73
+ Sequenced.next_sequence('some_name_2') #=> 1
74
+ # can also give an special step
75
+ Sequenced.next_sequence('some_name_2', 2) #=> 3
76
+ ```
77
+
78
+ **notice**:
79
+
80
+ for `v0.2.5`, `step` should only work with no `auto_increment` field, more info check `test/reference_test.rb`
81
+
82
+
63
83
  ## Consistency
64
84
 
65
85
  Mongoid::Sequence uses the atomic [findAndModify](http://www.mongodb.org/display/DOCS/findAndModify+Command) command, so you shouldn't have to worry about the sequence's consistency.
@@ -17,12 +17,21 @@ module Mongoid
17
17
  self.sequence_fields ||= []
18
18
  self.sequence_fields << fieldname
19
19
  end
20
+
21
+ def next_sequence(field, step = 1)
22
+ step ||= 1
23
+ Sequences.get_next_sequence(self.name.underscore, field, step)
24
+ end
25
+
26
+ def get_sequence(field)
27
+ Sequences.get_sequence(self.name.underscore, field)
28
+ end
20
29
  end
21
30
 
22
31
  def set_sequence
23
32
  if self.class.sequence_fields.present?
24
33
  self.class.sequence_fields.each do |f|
25
- self[f] = Sequences.get_next_sequence(self.class.name.underscore, f)
34
+ self[f] = self.class.next_sequence(f)
26
35
  end
27
36
  end
28
37
  end
@@ -7,9 +7,13 @@ module Mongoid
7
7
  field :fieldname
8
8
  field :seq, type: Integer
9
9
 
10
- def self.get_next_sequence(collection, fieldname)
11
- #Sequences.where(fieldname: "#{collection}_#{fieldname}").find_and_modify({'$inc' => {'seq' => 1}}, {'upsert' => 'true', :new => true}).seq
12
- self.where(fieldname: "#{collection}_#{fieldname}").find_and_modify({'$inc' => {'seq' => 1}}, {'upsert' => 'true', :new => true}).seq
10
+ def self.get_next_sequence(collection, fieldname, step = 1)
11
+ step ||= 1
12
+ self.where(fieldname: "#{collection}_#{fieldname}").find_and_modify({'$inc' => {'seq' => step}}, {'upsert' => 'true', :new => true}).seq
13
+ end
14
+
15
+ def self.get_sequence(collection, fieldname)
16
+ seq = self.where(fieldname: "#{collection}_#{fieldname}").limit(1).first.try(:seq) || 1
13
17
  end
14
18
  end
15
19
  end
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Sequence2
3
- VERSION = "0.2.4"
3
+ VERSION = "0.2.5"
4
4
  end
5
5
  end
@@ -5,4 +5,24 @@ class ReferenceTest < BaseTest
5
5
  s = ThirdSequencedModel.create(auto_increment: 1)
6
6
  assert_equal s.auto_increment, 1
7
7
  end
8
+
9
+ def test_next_sequence
10
+ assert_equal ThirdSequencedModel.next_sequence('test_field'), 1
11
+ assert_equal ThirdSequencedModel.next_sequence('test_field'), 2
12
+ end
13
+
14
+ def test_next_sequence_with_step
15
+ assert_equal ThirdSequencedModel.next_sequence('test_field', 2), 2
16
+ assert_equal ThirdSequencedModel.next_sequence('test_field', 1), 3
17
+ end
18
+
19
+ # 'step' should only work with no auto_increment field
20
+ def test_auto_increment_with_step
21
+ s1 = FirstSequencedModel.create
22
+
23
+ s = FirstSequencedModel.next_sequence('auto_increment', 3)
24
+ s2 = FirstSequencedModel.create(auto_increment: s)
25
+ assert_not_equal s2.auto_increment, 4
26
+ assert_equal FirstSequencedModel.get_sequence('auto_increment'), 5
27
+ end
8
28
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-sequence2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - jhjguxin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-08 00:00:00.000000000 Z
11
+ date: 2013-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid