sequence_on 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 347fdd365e120101402acfd7bc501502a7298ecf
4
- data.tar.gz: e5ef400318ea5e1cbf3f2c79d53f654c8db48076
3
+ metadata.gz: 435e96d6c211ac5457fd9281370e48645fbce435
4
+ data.tar.gz: c43524bd0d6fe081b126040c096b8ce8720fc982
5
5
  SHA512:
6
- metadata.gz: 35d70739e173a97bb1af6f68c5f110fc956d5977669f106124242b9d8fda8b91c80a3f8a5a39435243afa55177c8af4ad6037623349d9730cde3ee6c09788ac0
7
- data.tar.gz: df53d0d71df3091c598a8dc36c1677e68e2268900b4bc62f0d3f5922db5e156891f49e12e65a25b71fbe40fb18662cefc90ac15c0180c4130dc436638fb7a215
6
+ metadata.gz: b15f347b9b90370735c8c23ec15ea88fdf12652654ae07c006c02602b3788d1b88c63a29e6b340e73ca8eb811b0119da01b3d2d8f1c029d6caecb67da8823d0b
7
+ data.tar.gz: 58cc6f67aca213d3d5af2cb309fdf068b4184ebe8ba9ed388fb3b51041dc265b968ab0fbdd525aea0961f9d18141ddce29b93476e6e5b85fe8eb39acec0498bb
@@ -0,0 +1,50 @@
1
+ require 'active_support/core_ext/hash/slice'
2
+ require 'active_support/core_ext/class/attribute_accessors'
3
+
4
+ module SequenceOn
5
+
6
+ module SequencedOn
7
+
8
+ DEFAULT_OPTIONS = {
9
+ column: :sequential_id,
10
+ start_at: 1
11
+ }.freeze
12
+
13
+ module InstanceMethods
14
+
15
+ private
16
+
17
+ def postgresql?
18
+ defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) &&
19
+ self.class.connection.instance_of?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
20
+ end
21
+
22
+ def generate_sequence_id
23
+ options = self.class.sequence_options
24
+ self.class.connection.execute("LOCK TABLE #{self.class.table_name} IN EXCLUSIVE MODE") if postgresql?
25
+ last_record = self.class.class_exec(self, &options[:lmd]).
26
+ order("#{options[:column]} DESC").
27
+ first
28
+ self.sequential_id = if last_record
29
+ last_record.send(options[:column]) + 1
30
+ else
31
+ options[:start_at]
32
+ end
33
+ end
34
+ end
35
+
36
+ module ClassMethods
37
+
38
+ def sequenced_on(lmd, options = {})
39
+ include InstanceMethods
40
+ mattr_accessor :sequence_options, instance_accessor: false
41
+ before_create :generate_sequence_id, prepend: true
42
+ self.sequence_options = DEFAULT_OPTIONS.merge(options).merge(lmd: lmd)
43
+ end
44
+ end
45
+
46
+ def self.included(base)
47
+ base.extend(ClassMethods)
48
+ end
49
+ end
50
+ end
data/lib/sequence_on.rb CHANGED
@@ -1,36 +1,3 @@
1
- module SequencedOn
2
- extend ActiveSupport::Concern
1
+ require 'sequence_on/sequenced_on'
3
2
 
4
- class_methods do
5
-
6
- def sequence_on(l, opts={})
7
- @@seq_on = l
8
- @@seq_on_opts = opts
9
- end
10
- end
11
-
12
- included do
13
- before_create :generate_sequence_id, prepend: true
14
-
15
- private
16
-
17
- def postgresql?
18
- defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) &&
19
- self.class.connection.instance_of?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
20
- end
21
-
22
- def generate_sequence_id
23
- if postgresql?
24
- self.class.connection.execute("LOCK TABLE #{self.class.table_name} IN EXCLUSIVE MODE")
25
- end
26
- last_record = self.class.class_exec(self, &@@seq_on).
27
- order("sequential_id DESC").
28
- first
29
- self.sequential_id = if last_record
30
- last_record.sequential_id + 1
31
- else
32
- @@seq_on_opts[:starts_at] || 1
33
- end
34
- end
35
- end
36
- end
3
+ ActiveRecord::Base.send(:include, SequenceOn::SequencedOn)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequence_on
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy SEBAN
@@ -9,7 +9,49 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2017-05-18 00:00:00.000000000 Z
12
- dependencies: []
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '3.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '3.1'
13
55
  description: Flexible acts_as_sequenced replacement
14
56
  email: tech@qonto.eu
15
57
  executables: []
@@ -17,6 +59,7 @@ extensions: []
17
59
  extra_rdoc_files: []
18
60
  files:
19
61
  - lib/sequence_on.rb
62
+ - lib/sequence_on/sequenced_on.rb
20
63
  homepage: https://github.com/qonto/sequence_on
21
64
  licenses:
22
65
  - MIT