model_id 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +53 -0
- data/Rakefile +2 -0
- data/lib/model_id/base.rb +41 -0
- data/lib/model_id/version.rb +3 -0
- data/lib/model_id.rb +5 -0
- data/model_id.gemspec +24 -0
- data/spec/model_id_spec.rb +47 -0
- data/spec/spec_helper.rb +97 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0c932a7c5e539e0e99a37cdd3923255a69a147a2
|
4
|
+
data.tar.gz: 75a705190a4ff44ebaeeead15f1889d46556886e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6af130420c80adaba5c255212d7f9999c2e0f3e793ff6d1531e5fd7e1eb6ed955525bba1aeb614780cde004bdd067702acac5541212fd812b4454f1f9c2162e8
|
7
|
+
data.tar.gz: 9bef358e76b0670e24a7682279d4415e4d06d4063bb020be7363f11edd89c9a167bd689aac26ca0f9bbe98d19bfc9141019d629ddb82a4189e381896e7c6bbd9
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2016 abonec
|
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,53 @@
|
|
1
|
+
# ModelId
|
2
|
+
|
3
|
+
Gem add feature for id and find by this id across all instances of the model.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'model_id'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install model_id
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Include ModelId::Base to model's class:
|
22
|
+
|
23
|
+
class User
|
24
|
+
include ModelId::Base
|
25
|
+
end
|
26
|
+
|
27
|
+
Now your model has #model_id:
|
28
|
+
|
29
|
+
User.new.model_id
|
30
|
+
|
31
|
+
And you cand find instances by id:
|
32
|
+
|
33
|
+
user = User.new
|
34
|
+
User.find_by_id(user.model_id)
|
35
|
+
|
36
|
+
## Notes
|
37
|
+
|
38
|
+
Since ruby < 2.0 don't have prepend method you should manually call #set_next_model_id in your constructor:
|
39
|
+
|
40
|
+
class User
|
41
|
+
include ModelId::Base
|
42
|
+
def initialize
|
43
|
+
set_next_model_id
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
## Contributing
|
48
|
+
|
49
|
+
1. Fork it ( https://github.com/abonec/model_id/fork )
|
50
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
51
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
52
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
53
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'thread'
|
2
|
+
module ModelId
|
3
|
+
module Base
|
4
|
+
def model_id
|
5
|
+
@model_id
|
6
|
+
end
|
7
|
+
|
8
|
+
def set_next_model_id
|
9
|
+
self.class.model_id_mutex.synchronize do
|
10
|
+
@model_id = self.class.last_model_id + 1
|
11
|
+
self.class.last_model_id = @model_id
|
12
|
+
end
|
13
|
+
self.class.model_id_instances[@model_id] = self
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.included(base)
|
17
|
+
base.class_eval do
|
18
|
+
class << self
|
19
|
+
attr_accessor :last_model_id, :model_id_mutex, :model_id_instances
|
20
|
+
end
|
21
|
+
end
|
22
|
+
base.extend ClassMethods
|
23
|
+
base.last_model_id = 0
|
24
|
+
base.model_id_mutex = Mutex.new
|
25
|
+
base.model_id_instances = {}
|
26
|
+
base.prepend Ruby2Initializer if base.respond_to? :prepend
|
27
|
+
end
|
28
|
+
|
29
|
+
module ClassMethods
|
30
|
+
def find_by_id(id)
|
31
|
+
model_id_instances[id]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
module Ruby2Initializer
|
35
|
+
def initialize(*args)
|
36
|
+
set_next_model_id
|
37
|
+
super(*args)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/lib/model_id.rb
ADDED
data/model_id.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'model_id/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "model_id"
|
8
|
+
spec.version = ModelId::VERSION
|
9
|
+
spec.authors = ["abonec"]
|
10
|
+
spec.email = ["abonec@gmail.com"]
|
11
|
+
spec.summary = %q{gem add support for id instances of your model}
|
12
|
+
spec.description = %q{gem add support for id instances of your model and find instances across your model}
|
13
|
+
spec.homepage = "https://github.com/abonec/model_id"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake", "~> 11.1"
|
23
|
+
spec.add_development_dependency "rspec", '~>3.4'
|
24
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
describe ModelId::Base do
|
2
|
+
before :all do
|
3
|
+
@class_with_id = Class.new
|
4
|
+
@class_with_id.include ModelId::Base
|
5
|
+
end
|
6
|
+
|
7
|
+
it 'should be a module' do
|
8
|
+
expect(ModelId::Base).to be_a(Module)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should support models with initializer with arguments' do
|
12
|
+
class_with_custom_initialializer = Class.new
|
13
|
+
class_with_custom_initialializer.include ModelId::Base
|
14
|
+
class_with_custom_initialializer.send(:define_method, :initialize) {|arg1,arg2|}
|
15
|
+
expect{class_with_custom_initialializer.new(1,2)}.not_to raise_error
|
16
|
+
end
|
17
|
+
it 'should support models with default initializer' do
|
18
|
+
class_without_initializer = Class.new
|
19
|
+
class_without_initializer.include ModelId::Base
|
20
|
+
expect{class_without_initializer.new}.not_to raise_error
|
21
|
+
end
|
22
|
+
it 'should support models with noargs initializer' do
|
23
|
+
class_with_noargs_initializer = Class.new
|
24
|
+
class_with_noargs_initializer.include ModelId::Base
|
25
|
+
class_with_noargs_initializer.send(:define_method, :initialize){}
|
26
|
+
expect{class_with_noargs_initializer.new}.not_to raise_error
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'instance should have an id' do
|
30
|
+
expect(@class_with_id.new).to respond_to(:model_id)
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'new instance should have next id' do
|
34
|
+
expect do
|
35
|
+
@first_instance = @class_with_id.new
|
36
|
+
@second_instance = @class_with_id.new
|
37
|
+
end.to change(@class_with_id, :last_model_id).by(2)
|
38
|
+
expect(@second_instance.model_id).to be_eql(@first_instance.model_id.next)
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should find model instance by id' do
|
42
|
+
first_instance = @class_with_id.new
|
43
|
+
second_instance = @class_with_id.new
|
44
|
+
expect(@class_with_id.find_by_id(first_instance.model_id)).to eql(first_instance)
|
45
|
+
expect(@class_with_id.find_by_id(second_instance.model_id)).to eql(second_instance)
|
46
|
+
end
|
47
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
4
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
5
|
+
# files.
|
6
|
+
#
|
7
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
8
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
9
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
10
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
11
|
+
# a separate helper file that requires the additional dependencies and performs
|
12
|
+
# the additional setup, and require it from the spec files that actually need
|
13
|
+
# it.
|
14
|
+
#
|
15
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
16
|
+
# users commonly want.
|
17
|
+
#
|
18
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
19
|
+
require 'model_id'
|
20
|
+
RSpec.configure do |config|
|
21
|
+
# rspec-expectations config goes here. You can use an alternate
|
22
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
23
|
+
# assertions if you prefer.
|
24
|
+
config.expect_with :rspec do |expectations|
|
25
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
26
|
+
# and `failure_message` of custom matchers include text for helper methods
|
27
|
+
# defined using `chain`, e.g.:
|
28
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
29
|
+
# # => "be bigger than 2 and smaller than 4"
|
30
|
+
# ...rather than:
|
31
|
+
# # => "be bigger than 2"
|
32
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
33
|
+
end
|
34
|
+
|
35
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
36
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
37
|
+
config.mock_with :rspec do |mocks|
|
38
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
39
|
+
# a real object. This is generally recommended, and will default to
|
40
|
+
# `true` in RSpec 4.
|
41
|
+
mocks.verify_partial_doubles = true
|
42
|
+
end
|
43
|
+
|
44
|
+
# The settings below are suggested to provide a good initial experience
|
45
|
+
# with RSpec, but feel free to customize to your heart's content.
|
46
|
+
=begin
|
47
|
+
# These two settings work together to allow you to limit a spec run
|
48
|
+
# to individual examples or groups you care about by tagging them with
|
49
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
50
|
+
# get run.
|
51
|
+
config.filter_run :focus
|
52
|
+
config.run_all_when_everything_filtered = true
|
53
|
+
|
54
|
+
# Allows RSpec to persist some state between runs in order to support
|
55
|
+
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
56
|
+
# you configure your source control system to ignore this file.
|
57
|
+
config.example_status_persistence_file_path = "spec/examples.txt"
|
58
|
+
|
59
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
60
|
+
# recommended. For more details, see:
|
61
|
+
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
|
62
|
+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
63
|
+
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
|
64
|
+
config.disable_monkey_patching!
|
65
|
+
|
66
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
67
|
+
# be too noisy due to issues in dependencies.
|
68
|
+
config.warnings = true
|
69
|
+
|
70
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
71
|
+
# file, and it's useful to allow more verbose output when running an
|
72
|
+
# individual spec file.
|
73
|
+
if config.files_to_run.one?
|
74
|
+
# Use the documentation formatter for detailed output,
|
75
|
+
# unless a formatter has already been configured
|
76
|
+
# (e.g. via a command-line flag).
|
77
|
+
config.default_formatter = 'doc'
|
78
|
+
end
|
79
|
+
|
80
|
+
# Print the 10 slowest examples and example groups at the
|
81
|
+
# end of the spec run, to help surface which specs are running
|
82
|
+
# particularly slow.
|
83
|
+
config.profile_examples = 10
|
84
|
+
|
85
|
+
# Run specs in random order to surface order dependencies. If you find an
|
86
|
+
# order dependency and want to debug it, you can fix the order by providing
|
87
|
+
# the seed, which is printed after each run.
|
88
|
+
# --seed 1234
|
89
|
+
config.order = :random
|
90
|
+
|
91
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
92
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
93
|
+
# test failures related to randomization by passing the same `--seed` value
|
94
|
+
# as the one that triggered the failure.
|
95
|
+
Kernel.srand config.seed
|
96
|
+
=end
|
97
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: model_id
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- abonec
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '11.1'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '11.1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.4'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.4'
|
55
|
+
description: gem add support for id instances of your model and find instances across
|
56
|
+
your model
|
57
|
+
email:
|
58
|
+
- abonec@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/model_id.rb
|
69
|
+
- lib/model_id/base.rb
|
70
|
+
- lib/model_id/version.rb
|
71
|
+
- model_id.gemspec
|
72
|
+
- spec/model_id_spec.rb
|
73
|
+
- spec/spec_helper.rb
|
74
|
+
homepage: https://github.com/abonec/model_id
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.4.8
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: gem add support for id instances of your model
|
98
|
+
test_files:
|
99
|
+
- spec/model_id_spec.rb
|
100
|
+
- spec/spec_helper.rb
|