acts_as_sequence 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ spec/tmp/*/**
2
+ pkg
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --backtrace
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,42 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ acts_as_sequence (0.0.1)
5
+ activerecord (~> 3.0.0)
6
+ activesupport (~> 3.0.0)
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ activemodel (3.0.5)
12
+ activesupport (= 3.0.5)
13
+ builder (~> 2.1.2)
14
+ i18n (~> 0.4)
15
+ activerecord (3.0.5)
16
+ activemodel (= 3.0.5)
17
+ activesupport (= 3.0.5)
18
+ arel (~> 2.0.2)
19
+ tzinfo (~> 0.3.23)
20
+ activesupport (3.0.5)
21
+ arel (2.0.9)
22
+ builder (2.1.2)
23
+ diff-lcs (1.1.2)
24
+ i18n (0.5.0)
25
+ rspec (2.5.0)
26
+ rspec-core (~> 2.5.0)
27
+ rspec-expectations (~> 2.5.0)
28
+ rspec-mocks (~> 2.5.0)
29
+ rspec-core (2.5.1)
30
+ rspec-expectations (2.5.0)
31
+ diff-lcs (~> 1.1.2)
32
+ rspec-mocks (2.5.0)
33
+ sqlite3 (1.3.3)
34
+ tzinfo (0.3.25)
35
+
36
+ PLATFORMS
37
+ ruby
38
+
39
+ DEPENDENCIES
40
+ acts_as_sequence!
41
+ rspec
42
+ sqlite3
data/README.rdoc ADDED
@@ -0,0 +1,27 @@
1
+ = ActsAsSequence
2
+
3
+ Emulates database sequences for MySQL. It is useful when you want to have a unique value for an unsaved object.
4
+
5
+ rails g model Sequence id_order:integer
6
+
7
+ = Installation
8
+
9
+ gem 'acts_as_sequence'
10
+
11
+ = Example
12
+
13
+ > rails g model Sequence uniq_id_order:integer
14
+
15
+ app/models/sequence.rb:
16
+
17
+ class Sequence < ActiveRecord::Base
18
+ acts_as_sequencer
19
+ end
20
+
21
+ class Order < ActiveRecord::Base
22
+ acts_as_sequenced :uniq_id, Sequence
23
+ end
24
+
25
+ Order.new.uniq_id.should == 0
26
+ Order.new.uniq_id.should == 1
27
+ Sequence.next_sequence_value(:uniq_id).should == 2
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rspec/core'
4
+ require 'rspec/core/rake_task'
5
+ require 'acts_as_sequence'
6
+ require 'bundler'
7
+
8
+ Bundler::GemHelper.install_tasks
9
+ RSpec::Core::RakeTask.new(:spec)
10
+ task :default => :spec
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+
3
+ $:.push File.expand_path("../lib", __FILE__)
4
+ require "acts_as_sequence/version"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "acts_as_sequence"
8
+ s.version = ActsAsSequence::VERSION
9
+ s.author = "Victor Sokolov"
10
+ s.email = "gzigzigzeo@gmail.com"
11
+ s.homepage = "http://github.com/gzigzigzeo/acts_as_sequence"
12
+ s.description = "Emulates sequence in MySQL database. Useful when you want your newly created object to have unique id value."
13
+ s.summary = "Database sequence emulation for MySQL"
14
+
15
+ s.rubyforge_project = "acts_as_sequence"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency 'activerecord', '~> 3.0.0'
23
+ s.add_dependency 'activesupport', '~> 3.0.0'
24
+ s.add_development_dependency 'sqlite3'
25
+ s.add_development_dependency 'rspec'
26
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), "lib", "paperclip-meta")
@@ -0,0 +1,40 @@
1
+ # encoding: utf-8
2
+ require 'active_support/concern'
3
+
4
+ module ActsAsSequence
5
+ module ActsAsSequence
6
+ extend ::ActiveSupport::Concern
7
+
8
+ module ClassMethods
9
+ def acts_as_sequenced(field, sequencer, *args)
10
+ field = field.to_sym
11
+ options = args.extract_options!
12
+ sequence_name = options.delete(:sequence) || "#{field}_#{self.name.underscore}"
13
+
14
+ after_initialize do
15
+ write_attribute(field, sequencer.next_sequence_value(sequence_name)) if read_attribute(field).blank? && new_record?
16
+ end
17
+
18
+ (class << self; self; end).send(:define_method, :"next_sequence_#{field}") do
19
+ sequencer.next_sequence_value(sequence_name)
20
+ end
21
+ end
22
+
23
+ def acts_as_sequencer
24
+ class_eval do
25
+ def self.next_sequence_value(sequence)
26
+ raise ArgumentError, "Sequence column #{sequence} not found in model #{name}" unless column_names.include?(sequence.to_s)
27
+ transaction do
28
+ record = first(:lock => true)
29
+ record = create! if record.nil?
30
+ value = record[sequence].to_i
31
+ record[sequence] = value + 1
32
+ record.save!
33
+ value
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,8 @@
1
+ # encoding: utf-8
2
+ module ActsAsSequence
3
+ class Railtie < ::Rails::Railtie
4
+ ActiveSupport.on_load :active_record do
5
+ ::ActiveRecord::Base.send(:include, ActsAsSequence::ActsAsSequence)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,4 @@
1
+ # encoding: utf-8
2
+ module ActsAsSequence
3
+ VERSION = "0.0.1"
4
+ end
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+ module ActsAsSequence
3
+ require 'acts_as_sequence/acts_as_sequence'
4
+ require 'acts_as_sequence/version'
5
+ require 'acts_as_sequence/railtie' if defined?(Rails)
6
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Geometry saver plugin" do
4
+ before(:each) do
5
+ if Sequence.first
6
+ Sequence.first.update_attributes({
7
+ :new_id1_item => 1,
8
+ :id_2_sequence => 10
9
+ })
10
+ end
11
+ end
12
+
13
+ context "sequencer" do
14
+ it "gets next values for a sequences" do
15
+ Sequence.next_sequence_value(:new_id1_item).should == 1
16
+ Sequence.next_sequence_value(:id_2_sequence).should == 10
17
+ end
18
+ end
19
+
20
+ context "sequences" do
21
+ it "sets values for next sequences on initialize record" do
22
+ n = Item.new
23
+ n.new_id1.should == 1
24
+ n.new_id2.should == 10
25
+ end
26
+ end
27
+ end
data/spec/schema.rb ADDED
@@ -0,0 +1,11 @@
1
+ ActiveRecord::Schema.define :version => 0 do
2
+ create_table "sequences", :force => true do |t|
3
+ t.integer :new_id1_item, :default => 1
4
+ t.integer :id_2_sequence, :default => 10
5
+ end
6
+
7
+ create_table "items" do |t|
8
+ t.integer :new_id1
9
+ t.integer :new_id2
10
+ end
11
+ end
@@ -0,0 +1,34 @@
1
+ $LOAD_PATH << "." unless $LOAD_PATH.include?(".")
2
+
3
+ require 'logger'
4
+ require "bundler"
5
+
6
+ begin
7
+ Bundler.setup
8
+ Bundler.require
9
+ rescue Bundler::GemNotFound
10
+ raise RuntimeError, "Bundler couldn't find some gems." +
11
+ "Did you run `bundle install`?"
12
+ end
13
+
14
+ require 'active_record'
15
+
16
+ ActiveRecord::Base.send(:include, ActsAsSequence::ActsAsSequence)
17
+ ActiveRecord::Base.establish_connection(
18
+ "adapter" => "sqlite3",
19
+ "database" => ":memory:"
20
+ )
21
+
22
+ ActiveRecord::Base.logger = Logger.new(nil)
23
+
24
+ load(File.dirname(__FILE__) + '/schema.rb')
25
+ $: << File.join(File.dirname(__FILE__), '..', 'lib')
26
+
27
+ class Sequence < ActiveRecord::Base
28
+ acts_as_sequencer
29
+ end
30
+
31
+ class Item < ActiveRecord::Base
32
+ acts_as_sequenced :new_id1, Sequence
33
+ acts_as_sequenced :new_id2, Sequence, :sequence => :id_2_sequence
34
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_sequence
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Victor Sokolov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-23 00:00:00 +03:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: activerecord
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ~>
23
+ - !ruby/object:Gem::Version
24
+ version: 3.0.0
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: 3.0.0
36
+ type: :runtime
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: sqlite3
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: rspec
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ type: :development
59
+ version_requirements: *id004
60
+ description: Emulates sequence in MySQL database. Useful when you want your newly created object to have unique id value.
61
+ email: gzigzigzeo@gmail.com
62
+ executables: []
63
+
64
+ extensions: []
65
+
66
+ extra_rdoc_files: []
67
+
68
+ files:
69
+ - .gitignore
70
+ - .rspec
71
+ - Gemfile
72
+ - Gemfile.lock
73
+ - README.rdoc
74
+ - Rakefile
75
+ - acts_as_sequence.gemspec
76
+ - init.rb
77
+ - lib/acts_as_sequence.rb
78
+ - lib/acts_as_sequence/acts_as_sequence.rb
79
+ - lib/acts_as_sequence/railtie.rb
80
+ - lib/acts_as_sequence/version.rb
81
+ - spec/acts_as_sequence/acts_as_sequence_spec.rb
82
+ - spec/schema.rb
83
+ - spec/spec_helper.rb
84
+ has_rdoc: true
85
+ homepage: http://github.com/gzigzigzeo/acts_as_sequence
86
+ licenses: []
87
+
88
+ post_install_message:
89
+ rdoc_options: []
90
+
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: "0"
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: "0"
105
+ requirements: []
106
+
107
+ rubyforge_project: acts_as_sequence
108
+ rubygems_version: 1.5.3
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: Database sequence emulation for MySQL
112
+ test_files:
113
+ - spec/acts_as_sequence/acts_as_sequence_spec.rb
114
+ - spec/schema.rb
115
+ - spec/spec_helper.rb