acts_as_random 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.rdoc +2 -0
- data/Gemfile +2 -0
- data/MIT-LICENSE +20 -0
- data/Manifest +10 -0
- data/README.md +56 -0
- data/Rakefile +13 -0
- data/acts_as_random.gemspec +37 -0
- data/init.rb +1 -0
- data/lib/acts_as_random.rb +55 -0
- data/rspec/acts_as_random_spec.rb +31 -0
- data/rspec/spec_helper.rb +23 -0
- data.tar.gz.sig +2 -0
- metadata +115 -0
- metadata.gz.sig +0 -0
data/CHANGELOG.rdoc
ADDED
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Maciej Mensfeld
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest
ADDED
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# ActsAsRandom
|
2
|
+
|
3
|
+
## Install
|
4
|
+
|
5
|
+
gem install acts_as_random
|
6
|
+
|
7
|
+
and in your Gemfile:
|
8
|
+
|
9
|
+
gem 'acts_as_random'
|
10
|
+
|
11
|
+
## About
|
12
|
+
|
13
|
+
Gem allowing ActiveRecord models to have randomly generated
|
14
|
+
primary keys - not autoincremented
|
15
|
+
|
16
|
+
Don't use it when you create a lot of objects because when it generate random ID
|
17
|
+
it checks whether or not an row with generated ID exists (1 more SQL statement
|
18
|
+
per create). If a row exists it generates another random number however if you
|
19
|
+
have a lot of rows in table there is a possibility that generating a "clear"
|
20
|
+
ID will take a lot of time so watchout! ;)
|
21
|
+
|
22
|
+
I have written test to this plugin but unfortunately now it does not want to
|
23
|
+
work. If someone have enough time to write it I would be greatfull.
|
24
|
+
|
25
|
+
Also if you have a lot of rows it is not smart to use it. I created that to
|
26
|
+
protect data from viewing like 1,2,3,4 (hmm maybe there is a row 5 but i don't
|
27
|
+
have it - lets do /controller/action/5) so if you have 100 000 rows and my plugin
|
28
|
+
makes random from 0 to 1 000 000 you have 1/10 chances to gues so it is useless.
|
29
|
+
|
30
|
+
## Example
|
31
|
+
|
32
|
+
class CoolElement < ActiveRecord::Base
|
33
|
+
acts_as_random
|
34
|
+
end
|
35
|
+
|
36
|
+
# Creating first record
|
37
|
+
a = CoolElement.new
|
38
|
+
a.save
|
39
|
+
|
40
|
+
puts a.id
|
41
|
+
#Here you will have random value
|
42
|
+
423452
|
43
|
+
|
44
|
+
|
45
|
+
## Note on Patches/Pull Requests
|
46
|
+
|
47
|
+
* Fork the project.
|
48
|
+
* Make your feature addition or bug fix.
|
49
|
+
* Add tests for it. This is important so I don't break it in a future version unintentionally.
|
50
|
+
* Commit, do not mess with Rakefile, version, or history.
|
51
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
52
|
+
* Send me a pull request. Bonus points for topic branches.
|
53
|
+
|
54
|
+
## Copyright
|
55
|
+
|
56
|
+
Copyright (c) 2011 Maciej Mensfeld. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('acts_as_random', '0.1.1') do |p|
|
6
|
+
p.description = "Gem allowing ActiveRecord models to have randomly generated primary keys - not autoincremented"
|
7
|
+
p.url = "https://github.com/mensfeld/acts_as_random"
|
8
|
+
p.author = "Maciej Mensfeld"
|
9
|
+
p.email = "maciej@mensfeld.pl"
|
10
|
+
p.ignore_pattern = ["tmp/*", "script/*"]
|
11
|
+
p.development_dependencies = ["rspec >=2.0.0", "activerecord"]
|
12
|
+
end
|
13
|
+
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{acts_as_random}
|
5
|
+
s.version = "0.1.1"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Maciej Mensfeld"]
|
9
|
+
s.cert_chain = ["/home/mencio/.cert_keys/gem-public_cert.pem"]
|
10
|
+
s.date = %q{2011-04-09}
|
11
|
+
s.description = %q{Gem allowing ActiveRecord models to have randomly generated primary keys - not autoincremented}
|
12
|
+
s.email = %q{maciej@mensfeld.pl}
|
13
|
+
s.extra_rdoc_files = ["CHANGELOG.rdoc", "README.md", "lib/acts_as_random.rb"]
|
14
|
+
s.files = ["CHANGELOG.rdoc", "Gemfile", "MIT-LICENSE", "README.md", "Rakefile", "init.rb", "lib/acts_as_random.rb", "rspec/acts_as_random_spec.rb", "rspec/spec_helper.rb", "Manifest", "acts_as_random.gemspec"]
|
15
|
+
s.homepage = %q{https://github.com/mensfeld/acts_as_random}
|
16
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Acts_as_random", "--main", "README.md"]
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.rubyforge_project = %q{acts_as_random}
|
19
|
+
s.rubygems_version = %q{1.5.2}
|
20
|
+
s.signing_key = %q{/home/mencio/.cert_keys/gem-private_key.pem}
|
21
|
+
s.summary = %q{Gem allowing ActiveRecord models to have randomly generated primary keys - not autoincremented}
|
22
|
+
|
23
|
+
if s.respond_to? :specification_version then
|
24
|
+
s.specification_version = 3
|
25
|
+
|
26
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
27
|
+
s.add_development_dependency(%q<rspec>, [">= 2.0.0"])
|
28
|
+
s.add_development_dependency(%q<activerecord>, [">= 0"])
|
29
|
+
else
|
30
|
+
s.add_dependency(%q<rspec>, [">= 2.0.0"])
|
31
|
+
s.add_dependency(%q<activerecord>, [">= 0"])
|
32
|
+
end
|
33
|
+
else
|
34
|
+
s.add_dependency(%q<rspec>, [">= 2.0.0"])
|
35
|
+
s.add_dependency(%q<activerecord>, [">= 0"])
|
36
|
+
end
|
37
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'acts_as_random'
|
@@ -0,0 +1,55 @@
|
|
1
|
+
|
2
|
+
module Acts
|
3
|
+
module AsRandom
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.extend AddActsAsMethod
|
7
|
+
end
|
8
|
+
|
9
|
+
module AddActsAsMethod
|
10
|
+
def acts_as_random
|
11
|
+
class_eval <<-END
|
12
|
+
before_create :random_me!
|
13
|
+
include Acts::AsRandom::InstanceMethods
|
14
|
+
END
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
# Istance methods
|
20
|
+
module InstanceMethods
|
21
|
+
|
22
|
+
def self.included(aClass)
|
23
|
+
aClass.extend ClassMethods
|
24
|
+
end
|
25
|
+
|
26
|
+
module ClassMethods
|
27
|
+
def random(skip_cache = false)
|
28
|
+
unless skip_cache
|
29
|
+
order("RAND()").first
|
30
|
+
else
|
31
|
+
where("created_at < '#{Time.now.to_s(:db)}'").order("RAND()").first
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def random_me!
|
39
|
+
# Zakres integera obnizony o 1 zeby potem dodać 1 (zeby nie bylo opcji
|
40
|
+
# że komuś się przydzieli ID == 0)
|
41
|
+
range = 2147483646
|
42
|
+
id = rand(range)+1
|
43
|
+
while self.class.exists?(id) do
|
44
|
+
id = rand(range)+1
|
45
|
+
end
|
46
|
+
self.id = id
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
ActiveRecord::Base.send(:include, Acts::AsRandom)
|
54
|
+
|
55
|
+
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
ROOT = File.expand_path(File.dirname(__FILE__))
|
4
|
+
|
5
|
+
class CoolElement < ActiveRecord::Base
|
6
|
+
acts_as_random
|
7
|
+
end
|
8
|
+
|
9
|
+
describe CoolElement do
|
10
|
+
subject { CoolElement }
|
11
|
+
|
12
|
+
context "when creating element" do
|
13
|
+
it "should not have a 0 id" do
|
14
|
+
a = CoolElement.new
|
15
|
+
a.save
|
16
|
+
a.id.should > 0
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "when creating two elements" do
|
21
|
+
it "should not add them autoincrement ids" do
|
22
|
+
a = CoolElement.new
|
23
|
+
a.save
|
24
|
+
b = CoolElement.new
|
25
|
+
b.save
|
26
|
+
a.id.should_not eql b.id
|
27
|
+
a.id.should_not eql b.id-1
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'sqlite3'
|
6
|
+
require 'activerecord'
|
7
|
+
require 'acts_as_random'
|
8
|
+
|
9
|
+
ActiveRecord::Base.send(:include, Acts::AsRandom)
|
10
|
+
|
11
|
+
ActiveRecord::Base.logger = Logger.new(STDERR)
|
12
|
+
ActiveRecord::Base.colorize_logging = false
|
13
|
+
|
14
|
+
ActiveRecord::Base.establish_connection(
|
15
|
+
:adapter => "sqlite3",
|
16
|
+
:dbfile => ":memory:"
|
17
|
+
)
|
18
|
+
|
19
|
+
ActiveRecord::Schema.define do
|
20
|
+
create_table :cool_elements do |table|
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
data.tar.gz.sig
ADDED
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_random
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Maciej Mensfeld
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain:
|
12
|
+
- |
|
13
|
+
-----BEGIN CERTIFICATE-----
|
14
|
+
MIIDMjCCAhqgAwIBAgIBADANBgkqhkiG9w0BAQUFADA/MQ8wDQYDVQQDDAZtYWNp
|
15
|
+
ZWoxGDAWBgoJkiaJk/IsZAEZFghtZW5zZmVsZDESMBAGCgmSJomT8ixkARkWAnBs
|
16
|
+
MB4XDTExMDQwOTA5NDcyMloXDTEyMDQwODA5NDcyMlowPzEPMA0GA1UEAwwGbWFj
|
17
|
+
aWVqMRgwFgYKCZImiZPyLGQBGRYIbWVuc2ZlbGQxEjAQBgoJkiaJk/IsZAEZFgJw
|
18
|
+
bDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAL0+nG3V4/exIeiJ0IN+
|
19
|
+
wVfq8Utcu4Qpo+58EIVMIu3FiK+8w6MBvatZnUrRu12pqWLw9xrUkCiYeRErD+jF
|
20
|
+
AmdggIM/tu9CcjvURXH7VeTzOVA+pnV+eJWMD61o8HljFVcb/nyEYYVKErtr9/O4
|
21
|
+
QrIGv5lnszq1PMj2sBMy2gOP1YnzawncMLmkpp/T5SU4JZ5gAktGMRVz8RxmZzF5
|
22
|
+
6NVqFLbuqSRSU5U//WJvZVJt8dycCGgQzBM4Vi3nkOWyjIF0BANf1TqnlU2u6s8d
|
23
|
+
UK1AoDZfg5feef5e8eqoomHebX1opNGM/SOQhu3LRgax4rJfnl6VS3I2wighohsf
|
24
|
+
AgcCAwEAAaM5MDcwCQYDVR0TBAIwADAdBgNVHQ4EFgQUGlrWBqxVieAPk7NEzBDp
|
25
|
+
kM+iAMMwCwYDVR0PBAQDAgSwMA0GCSqGSIb3DQEBBQUAA4IBAQAJMoyBaJs8boiz
|
26
|
+
lFpbw6MWjk+7ZhqoHpFrWEV4nzb5GzyHZ7GU/pa1fSEQR0SCs+LnTLQbAYNQyUTT
|
27
|
+
O+UsTuA7xzI//v6cSodv3Q9NbfoDlou74xv1NXorWoosQFMpVWrXv+c/1RqU3cq4
|
28
|
+
WUr+rRiveEXG4tXOwkrpX8KH8xVp2vQZcGw3AXPqhzfqDGzpHd6ws3lk+8HoSrSo
|
29
|
+
2L68tDoxraF2Z2toAg9vfFw1+mOeDk1xVIPVcBy3tJxstHfHGHlQuMiRiDQX2b2D
|
30
|
+
YYU8UWVt2841IwB5Dgl4O+atXhe9ZTBO0W32pl4Bq5CP9lhQRT1KL7sxfznJlF7Y
|
31
|
+
BH3YFsdk
|
32
|
+
-----END CERTIFICATE-----
|
33
|
+
|
34
|
+
date: 2011-04-09 00:00:00 +02:00
|
35
|
+
default_executable:
|
36
|
+
dependencies:
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: rspec
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.0.0
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id001
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: activerecord
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
type: :development
|
58
|
+
version_requirements: *id002
|
59
|
+
description: Gem allowing ActiveRecord models to have randomly generated primary keys - not autoincremented
|
60
|
+
email: maciej@mensfeld.pl
|
61
|
+
executables: []
|
62
|
+
|
63
|
+
extensions: []
|
64
|
+
|
65
|
+
extra_rdoc_files:
|
66
|
+
- CHANGELOG.rdoc
|
67
|
+
- README.md
|
68
|
+
- lib/acts_as_random.rb
|
69
|
+
files:
|
70
|
+
- CHANGELOG.rdoc
|
71
|
+
- Gemfile
|
72
|
+
- MIT-LICENSE
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- init.rb
|
76
|
+
- lib/acts_as_random.rb
|
77
|
+
- rspec/acts_as_random_spec.rb
|
78
|
+
- rspec/spec_helper.rb
|
79
|
+
- Manifest
|
80
|
+
- acts_as_random.gemspec
|
81
|
+
has_rdoc: true
|
82
|
+
homepage: https://github.com/mensfeld/acts_as_random
|
83
|
+
licenses: []
|
84
|
+
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options:
|
87
|
+
- --line-numbers
|
88
|
+
- --inline-source
|
89
|
+
- --title
|
90
|
+
- Acts_as_random
|
91
|
+
- --main
|
92
|
+
- README.md
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: "0"
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
none: false
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: "1.2"
|
107
|
+
requirements: []
|
108
|
+
|
109
|
+
rubyforge_project: acts_as_random
|
110
|
+
rubygems_version: 1.5.2
|
111
|
+
signing_key:
|
112
|
+
specification_version: 3
|
113
|
+
summary: Gem allowing ActiveRecord models to have randomly generated primary keys - not autoincremented
|
114
|
+
test_files: []
|
115
|
+
|
metadata.gz.sig
ADDED
Binary file
|