fantasy_identifiable 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9057c9817d0effbe2c1ad66061eaf1f972c44b5b828d99e0daaf11ee51a48c84
4
+ data.tar.gz: 2ec4cf4407bcbfd845c45695c77c992c1e7b56c3ffcae5961fc7950fef0c198f
5
+ SHA512:
6
+ metadata.gz: 942db56529c0f117552550da0e014640620fd935a1efd57d243e3ba2eae87b88d0007f80c4c6a4fc0b8247fd9ebc0885275b9eed91ea5d10649cd7a73af6a691
7
+ data.tar.gz: 2e9406ce0905156be7721a89f85007333b929793e8fac7c1ea55df97403c91147b5043786fb6e63472ace80ae91e4c82e7ff5cfe4f7dcad2db904e85e8533099
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,99 @@
1
+ # FantasyIdentifiable
2
+
3
+ This gem allows you to create unique readable fantasy names as identifiers for your active record objects.
4
+
5
+ You can also define multiple identifier columns and alternatively use a UUID or friendly_token as identifier.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'fantasy_identifiable'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install fantasy_identifiable
22
+
23
+ ## Usage
24
+
25
+ Make sure your ActiveRecord Model has a string field that you want to use as an identifier.
26
+
27
+ ### Examples
28
+
29
+ Here you can see the db schema and model definitions for multiple usage examples.
30
+
31
+ **DB Schema:**
32
+
33
+ ```ruby
34
+ ActiveRecord::Schema.define do
35
+ self.verbose = false
36
+
37
+ create_table :fantasy_objects, force: true do |t|
38
+ t.string :identifier
39
+ end
40
+
41
+ create_table :friendly_objects, force: true do |t|
42
+ t.string :identifier
43
+ end
44
+
45
+ create_table :uuid_objects, force: true do |t|
46
+ t.string :identifier
47
+ end
48
+
49
+ create_table :multi_objects, force: true do |t|
50
+ t.string :identifier
51
+ t.string :friendly_name
52
+ end
53
+ end
54
+ ```
55
+
56
+ **ActiveRecord Models**
57
+
58
+ ```ruby
59
+ class FantasyObject < ActiveRecord::Base
60
+ include FantasyIdentifiable
61
+ identifiable({ identifier: :fantasy })
62
+ end
63
+
64
+ class FriendlyObject < ActiveRecord::Base
65
+ include FantasyIdentifiable
66
+ identifiable({ identifier: :friendly })
67
+ end
68
+
69
+ class UuidObject < ActiveRecord::Base
70
+ include FantasyIdentifiable
71
+ identifiable({ identifier: :uuid })
72
+ end
73
+
74
+ class MultiObject < ActiveRecord::Base
75
+ include FantasyIdentifiable
76
+ identifiable({
77
+ friendly_name: :friendly,
78
+ identifier: :uuid
79
+ })
80
+ end
81
+ ```
82
+
83
+ ## Development
84
+
85
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
86
+
87
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
88
+
89
+ ## Contributing
90
+
91
+ Bug reports and pull requests are welcome on GitHub at https://github.com/arusa/fantasy_identifiable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
92
+
93
+ ## License
94
+
95
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
96
+
97
+ ## Code of Conduct
98
+
99
+ Everyone interacting in the FantasyIdentifiable project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/arusa/fantasy_identifiable/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,96 @@
1
+ require "fantasy_identifiable/version"
2
+ require "active_support/concern"
3
+ require "faker"
4
+
5
+ module FantasyIdentifiable
6
+ # class Error < StandardError; end
7
+
8
+ extend ::ActiveSupport::Concern
9
+
10
+ included do
11
+ # class << self; attr_reader :identifier_fields; end
12
+ before_create :generate_identifiers
13
+ end
14
+
15
+ def generate_identifiers
16
+ self.class.identifier_fields.each do |field_name, field_type|
17
+ case field_type
18
+ when :friendly
19
+ set_friendly_identifier field_name
20
+ when :fantasy
21
+ set_fantasy_identifier field_name
22
+ else
23
+ set_uuid_identifier field_name
24
+ end
25
+ end
26
+ end
27
+
28
+ class_methods do
29
+ attr_reader :identifier_fields
30
+
31
+ def identifiable(fields)
32
+ @identifier_fields = fields
33
+ end
34
+ end
35
+
36
+ def identifier_with(field_name, &block)
37
+ loop do
38
+ new_id = block.call
39
+ if self.class.where("#{field_name} LIKE ?", new_id).empty?
40
+ send("#{field_name}=", new_id)
41
+ break
42
+ end
43
+ end
44
+ end
45
+
46
+ def set_fantasy_identifier field_name
47
+ identifier_with(field_name) do
48
+ random_fantasy_identifier
49
+ end
50
+ end
51
+
52
+ def set_friendly_identifier field_name
53
+ identifier_with(field_name) do
54
+ friendly_token(20).downcase.tr("-_", "ab")
55
+ end
56
+ end
57
+
58
+ def set_uuid_identifier field_name
59
+ identifier_with(field_name) do
60
+ SecureRandom.uuid
61
+ end
62
+ end
63
+
64
+ def friendly_token(length = 20)
65
+ # To calculate real characters, we must perform this operation.
66
+ # See SecureRandom.urlsafe_base64
67
+ rlength = (length * 3) / 4
68
+ SecureRandom.urlsafe_base64(rlength).tr('lIO0', 'sxyz')
69
+ end
70
+
71
+ def random_fantasy_identifier
72
+ (
73
+ (
74
+ Faker::Hipster.words(number: 4) +
75
+ [
76
+ Faker::TvShows::Stargate.planet,
77
+ Faker::TvShows::GameOfThrones.city,
78
+ Faker::TvShows::GameOfThrones.dragon,
79
+ Faker::Movies::HitchhikersGuideToTheGalaxy.planet,
80
+ Faker::Creature::Animal.name,
81
+ Faker::Creature::Cat.name,
82
+ Faker::Creature::Dog.name,
83
+ Faker::Fantasy::Tolkien.race,
84
+ Faker::Books::Dune.city,
85
+ Faker::Books::Dune.planet,
86
+ Faker::Verb.past,
87
+ Faker::Science.element,
88
+ Faker::Name.first_name,
89
+ Faker::Job.key_skill,
90
+ Faker::Hacker.noun
91
+ ]
92
+ ).sample(3) +
93
+ [friendly_token(3).downcase.tr("-_", "ab")]
94
+ ).join("-").delete(" ").downcase
95
+ end
96
+ end
@@ -0,0 +1,3 @@
1
+ module FantasyIdentifiable
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fantasy_identifiable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Rusa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-06-06 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: 6.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 6.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 6.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 6.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: faker
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: sqlite3
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: This gem allows you to create unique readable fantasy names as identifiers
112
+ for your active record objects. You can also define multiple identifier columns
113
+ and alternatively use a UUID or Devise.friendly_token as identifier.
114
+ email:
115
+ - alex@rusa.at
116
+ executables: []
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - LICENSE.txt
121
+ - README.md
122
+ - Rakefile
123
+ - lib/fantasy_identifiable.rb
124
+ - lib/fantasy_identifiable/version.rb
125
+ homepage: https://github.com/arusa/fantasy_identifiable
126
+ licenses:
127
+ - MIT
128
+ metadata:
129
+ homepage_uri: https://github.com/arusa/fantasy_identifiable
130
+ source_code_uri: https://github.com/arusa/fantasy_identifiable
131
+ changelog_uri: https://github.com/arusa/fantasy_identifiable/blob/master/CHANGELOG.md
132
+ post_install_message:
133
+ rdoc_options: []
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ requirements: []
147
+ rubygems_version: 3.1.4
148
+ signing_key:
149
+ specification_version: 4
150
+ summary: Create unique fantasy names as identifier for your activerecord objects
151
+ test_files: []