mad_id 0.0.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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +56 -0
- data/Rakefile +1 -0
- data/lib/mad_id/identity_methods.rb +28 -0
- data/lib/mad_id/locator.rb +0 -0
- data/lib/mad_id/railtie.rb +9 -0
- data/lib/mad_id/version.rb +3 -0
- data/lib/mad_id.rb +20 -0
- data/mad_id.gemspec +28 -0
- data/spec/mad_id_spec.rb +58 -0
- data/spec/spec_helper.rb +15 -0
- data/spec/support/.gitkeep +0 -0
- data/spec/support/schema.rb +5 -0
- metadata +161 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1f7151e5b6b6e1b85ce61310bee3e7261af373bc
|
4
|
+
data.tar.gz: 4d3a29c03becf7d3c0447889d2aeb488fd5be28e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3b9cdc1473f81f082551ee543b891ae1d383971d90bd5868e2c50f0275c7dbfd3975429f30a9c15f8e05507023c7e6f7f2398f448e422ebd2d06fa43929c88f2
|
7
|
+
data.tar.gz: 2d210548a728c1a9fc96bdbfef3b47448873a640e400463878496861e204f7669be7deb3dccb97e0fc7e6dc2fe6efb386d70ca2a6109da51cc085abd336db45a
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Lars Brillert
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# MadId
|
2
|
+
|
3
|
+
Will can help you to inject easy identifiers into your `ActiveRecord` models.
|
4
|
+
|
5
|
+
It will:
|
6
|
+
|
7
|
+
* set up a `before_save` callback to set `identifier`
|
8
|
+
* override `to_param` to return `identifier`
|
9
|
+
* give you a `short_identifer` that returns the first 12 chars of `identifier`
|
10
|
+
|
11
|
+
Rightnow its very opinonated and not configurable in any way, this might change.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
gem 'mad_id'
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install mad_id
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
### Rails
|
30
|
+
|
31
|
+
class YourModel < ActiveRecord::Base
|
32
|
+
identify_with :foo
|
33
|
+
end
|
34
|
+
|
35
|
+
after you create the object it will have the `identifier` attribute set to
|
36
|
+
|
37
|
+
"foo-<UUID>"
|
38
|
+
|
39
|
+
### Plain
|
40
|
+
|
41
|
+
If your not using Rails you'll have to include `MadId` on your own
|
42
|
+
|
43
|
+
class YourModel < ActiveRecord::Base
|
44
|
+
include MadID
|
45
|
+
|
46
|
+
identify_with :baz
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
## Contributing
|
51
|
+
|
52
|
+
1. Fork it ( http://github.com/<my-github-username>/mad_id/fork )
|
53
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
54
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
55
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
56
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module MadId
|
2
|
+
|
3
|
+
module IdentityMethods
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
before_save :set_identifier, on: :create
|
8
|
+
|
9
|
+
def self.object_identifier_prefix
|
10
|
+
@identifier
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def set_identifier
|
15
|
+
self.identifier = "#{self.class.object_identifier_prefix}-#{SecureRandom.uuid}"[0..34]
|
16
|
+
end
|
17
|
+
|
18
|
+
def short_identifier
|
19
|
+
self.identifier[0..11]
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_param
|
23
|
+
self.identifier
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
File without changes
|
data/lib/mad_id.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require "mad_id/version"
|
2
|
+
|
3
|
+
require "active_record"
|
4
|
+
require "active_support"
|
5
|
+
|
6
|
+
require "mad_id/identity_methods"
|
7
|
+
require "mad_id/railtie" if defined?(Rails)
|
8
|
+
|
9
|
+
module MadId
|
10
|
+
extend ActiveSupport::Concern
|
11
|
+
|
12
|
+
included do
|
13
|
+
def self.identify_with(value)
|
14
|
+
@identifier = value
|
15
|
+
|
16
|
+
self.send(:include, MadId::IdentityMethods)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
data/mad_id.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mad_id/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mad_id"
|
8
|
+
spec.version = MadId::VERSION
|
9
|
+
spec.authors = ["Lars Brillert"]
|
10
|
+
spec.email = ["lars@railslove.com"]
|
11
|
+
spec.summary = %q{Adds prefixed identifiers to your models.}
|
12
|
+
spec.homepage = "https://github.com/kangguru/mad_id"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "activerecord", "~> 4.1.1"
|
21
|
+
spec.add_dependency "activesupport", "~> 4.1.1"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
24
|
+
spec.add_development_dependency "sqlite3"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency "rspec"
|
27
|
+
spec.add_development_dependency "rspec-collection_matchers"
|
28
|
+
end
|
data/spec/mad_id_spec.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Pony < ActiveRecord::Base
|
4
|
+
include MadId
|
5
|
+
end
|
6
|
+
|
7
|
+
class LittlePony < Pony
|
8
|
+
identify_with :pny
|
9
|
+
end
|
10
|
+
|
11
|
+
describe MadId do
|
12
|
+
|
13
|
+
it 'inserts #identify_with' do
|
14
|
+
expect(Pony.respond_to?(:identify_with)).to be(true)
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'wont set any callbacks' do
|
18
|
+
expect(Pony._save_callbacks.select {|cb| cb.filter == :set_identifier}).to be_empty
|
19
|
+
end
|
20
|
+
|
21
|
+
describe 'when #identify_with is called' do
|
22
|
+
subject { LittlePony.new }
|
23
|
+
|
24
|
+
it 'inserts the #set_identifier callback' do
|
25
|
+
expect(LittlePony._save_callbacks.select {|cb| cb.filter == :set_identifier}).to have(1).item
|
26
|
+
end
|
27
|
+
|
28
|
+
describe '#to_param' do
|
29
|
+
before { subject.set_identifier }
|
30
|
+
it 'will return the identifier' do
|
31
|
+
expect(subject.to_param).to eq(subject.identifier)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe '#short_identifier' do
|
36
|
+
before { subject.set_identifier }
|
37
|
+
it 'will shorten the identifier to the first 12 chars' do
|
38
|
+
expect(subject.short_identifier).to eq(subject.identifier[0..11])
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe '#identifier' do
|
43
|
+
before { subject.set_identifier }
|
44
|
+
it 'will start with the #identify_with argument' do
|
45
|
+
expect(subject.identifier).to match(/pny-.+/)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe 'callbacks' do
|
50
|
+
subject { LittlePony.new }
|
51
|
+
|
52
|
+
it 'will set the identifier upon create' do
|
53
|
+
expect{ subject.save! }.to change{ subject.identifier }.from(nil)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
|
4
|
+
require 'mad_id'
|
5
|
+
require 'rspec'
|
6
|
+
require 'rspec/collection_matchers'
|
7
|
+
|
8
|
+
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
|
9
|
+
|
10
|
+
Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each { |f| require f }
|
11
|
+
|
12
|
+
RSpec.configure do |config|
|
13
|
+
config.color = true
|
14
|
+
config.order = "random"
|
15
|
+
end
|
File without changes
|
metadata
ADDED
@@ -0,0 +1,161 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mad_id
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lars Brillert
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.1.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.1.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 4.1.1
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 4.1.1
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.5'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.5'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: sqlite3
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec-collection_matchers
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description:
|
112
|
+
email:
|
113
|
+
- lars@railslove.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- .gitignore
|
119
|
+
- Gemfile
|
120
|
+
- LICENSE.txt
|
121
|
+
- README.md
|
122
|
+
- Rakefile
|
123
|
+
- lib/mad_id.rb
|
124
|
+
- lib/mad_id/identity_methods.rb
|
125
|
+
- lib/mad_id/locator.rb
|
126
|
+
- lib/mad_id/railtie.rb
|
127
|
+
- lib/mad_id/version.rb
|
128
|
+
- mad_id.gemspec
|
129
|
+
- spec/mad_id_spec.rb
|
130
|
+
- spec/spec_helper.rb
|
131
|
+
- spec/support/.gitkeep
|
132
|
+
- spec/support/schema.rb
|
133
|
+
homepage: https://github.com/kangguru/mad_id
|
134
|
+
licenses:
|
135
|
+
- MIT
|
136
|
+
metadata: {}
|
137
|
+
post_install_message:
|
138
|
+
rdoc_options: []
|
139
|
+
require_paths:
|
140
|
+
- lib
|
141
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - '>='
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
147
|
+
requirements:
|
148
|
+
- - '>='
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: '0'
|
151
|
+
requirements: []
|
152
|
+
rubyforge_project:
|
153
|
+
rubygems_version: 2.2.2
|
154
|
+
signing_key:
|
155
|
+
specification_version: 4
|
156
|
+
summary: Adds prefixed identifiers to your models.
|
157
|
+
test_files:
|
158
|
+
- spec/mad_id_spec.rb
|
159
|
+
- spec/spec_helper.rb
|
160
|
+
- spec/support/.gitkeep
|
161
|
+
- spec/support/schema.rb
|