anchormodel 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 +2 -0
- data/.ruby-version +1 -0
- data/.yardopts +1 -0
- data/LICENSE +165 -0
- data/README.md +96 -0
- data/Rakefile +27 -0
- data/anchormodel.gemspec +33 -0
- data/doc/Anchormodel/ActiveModelTypeValue.html +513 -0
- data/doc/Anchormodel/Attribute.html +502 -0
- data/doc/Anchormodel/ModelMixin.html +307 -0
- data/doc/Anchormodel/Version.html +195 -0
- data/doc/Anchormodel.html +846 -0
- data/doc/_index.html +151 -0
- data/doc/class_list.html +51 -0
- data/doc/css/common.css +1 -0
- data/doc/css/full_list.css +58 -0
- data/doc/css/style.css +497 -0
- data/doc/file.README.html +167 -0
- data/doc/file_list.html +56 -0
- data/doc/frames.html +17 -0
- data/doc/index.html +167 -0
- data/doc/js/app.js +314 -0
- data/doc/js/full_list.js +216 -0
- data/doc/js/jquery.js +4 -0
- data/doc/method_list.html +203 -0
- data/doc/top-level-namespace.html +110 -0
- data/lib/anchormodel/active_model_type_value.rb +37 -0
- data/lib/anchormodel/attribute.rb +24 -0
- data/lib/anchormodel/model_mixin.rb +46 -0
- data/lib/anchormodel/version.rb +11 -0
- data/lib/anchormodel.rb +65 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: bc6cfead46a797c2039091347636575edf3b46be9172aa0b5e859d64c8dec816
|
4
|
+
data.tar.gz: c195d9fbb46ea16961977818d30628c8223926708bde1385aa7ca50307820f43
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 91f34a409344033c7e04ac4eef8a5af81b0997c4c092c2a0baba3df1fcd3c00980a9e11aadee18ef410609bd2aff77476d0524384cad4a3e5d797e0e0e1fb7c2
|
7
|
+
data.tar.gz: e689dd2d03bc6932b6c7ebf444e3fd8db72136f430b2863c05c9d9f7d3b65dae91f320cadb94b0eb119326244263d34540ac59cccc19310dd2d33ff3bbea3f36
|
data/.gitignore
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.0.4
|
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--markup=markdown --plugin activesupport-concern
|
data/LICENSE
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
2
|
+
Version 3, 29 June 2007
|
3
|
+
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
6
|
+
of this license document, but changing it is not allowed.
|
7
|
+
|
8
|
+
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
11
|
+
License, supplemented by the additional permissions listed below.
|
12
|
+
|
13
|
+
0. Additional Definitions.
|
14
|
+
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
17
|
+
General Public License.
|
18
|
+
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
20
|
+
other than an Application or a Combined Work as defined below.
|
21
|
+
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
25
|
+
of using an interface provided by the Library.
|
26
|
+
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
28
|
+
Application with the Library. The particular version of the Library
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
30
|
+
Version".
|
31
|
+
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
35
|
+
based on the Application, and not on the Linked Version.
|
36
|
+
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
38
|
+
object code and/or source code for the Application, including any data
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
41
|
+
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
43
|
+
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
46
|
+
|
47
|
+
2. Conveying Modified Versions.
|
48
|
+
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
51
|
+
that uses the facility (other than as an argument passed when the
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
53
|
+
version:
|
54
|
+
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
56
|
+
ensure that, in the event an Application does not supply the
|
57
|
+
function or data, the facility still operates, and performs
|
58
|
+
whatever part of its purpose remains meaningful, or
|
59
|
+
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
61
|
+
this License applicable to that copy.
|
62
|
+
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
64
|
+
|
65
|
+
The object code form of an Application may incorporate material from
|
66
|
+
a header file that is part of the Library. You may convey such object
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
68
|
+
material is not limited to numerical parameters, data structure
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
71
|
+
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
73
|
+
Library is used in it and that the Library and its use are
|
74
|
+
covered by this License.
|
75
|
+
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
77
|
+
document.
|
78
|
+
|
79
|
+
4. Combined Works.
|
80
|
+
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
82
|
+
taken together, effectively do not restrict modification of the
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
85
|
+
the following:
|
86
|
+
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
88
|
+
the Library is used in it and that the Library and its use are
|
89
|
+
covered by this License.
|
90
|
+
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
92
|
+
document.
|
93
|
+
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
95
|
+
execution, include the copyright notice for the Library among
|
96
|
+
these notices, as well as a reference directing the user to the
|
97
|
+
copies of the GNU GPL and this license document.
|
98
|
+
|
99
|
+
d) Do one of the following:
|
100
|
+
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
102
|
+
License, and the Corresponding Application Code in a form
|
103
|
+
suitable for, and under terms that permit, the user to
|
104
|
+
recombine or relink the Application with a modified version of
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
107
|
+
Corresponding Source.
|
108
|
+
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
111
|
+
a copy of the Library already present on the user's computer
|
112
|
+
system, and (b) will operate properly with a modified version
|
113
|
+
of the Library that is interface-compatible with the Linked
|
114
|
+
Version.
|
115
|
+
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
117
|
+
be required to provide such information under section 6 of the
|
118
|
+
GNU GPL, and only to the extent that such information is
|
119
|
+
necessary to install and execute a modified version of the
|
120
|
+
Combined Work produced by recombining or relinking the
|
121
|
+
Application with a modified version of the Linked Version. (If
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
126
|
+
for conveying Corresponding Source.)
|
127
|
+
|
128
|
+
5. Combined Libraries.
|
129
|
+
|
130
|
+
You may place library facilities that are a work based on the
|
131
|
+
Library side by side in a single library together with other library
|
132
|
+
facilities that are not Applications and are not covered by this
|
133
|
+
License, and convey such a combined library under terms of your
|
134
|
+
choice, if you do both of the following:
|
135
|
+
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
137
|
+
on the Library, uncombined with any other library facilities,
|
138
|
+
conveyed under the terms of this License.
|
139
|
+
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
141
|
+
is a work based on the Library, and explaining where to find the
|
142
|
+
accompanying uncombined form of the same work.
|
143
|
+
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
145
|
+
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
148
|
+
versions will be similar in spirit to the present version, but may
|
149
|
+
differ in detail to address new problems or concerns.
|
150
|
+
|
151
|
+
Each version is given a distinguishing version number. If the
|
152
|
+
Library as you received it specifies that a certain numbered version
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
154
|
+
applies to it, you have the option of following the terms and
|
155
|
+
conditions either of that published version or of any later version
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
160
|
+
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
164
|
+
permanent authorization for you to choose that version for the
|
165
|
+
Library.
|
data/README.md
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
# Introducing Anchormodel
|
2
|
+
|
3
|
+
This gem provides a simple but powerful alternative to [Rails
|
4
|
+
Enums](https://api.rubyonrails.org/v7.0/classes/ActiveRecord/Enum.html). In
|
5
|
+
contrast to regular Enums, Anchormodels can hold application logic, making them
|
6
|
+
ideal for tying code to database objects.
|
7
|
+
|
8
|
+
# Use case
|
9
|
+
|
10
|
+
Typically, a Rails application consists of three kinds of state:
|
11
|
+
|
12
|
+
- The code, which we can consider static within a given version. Code can
|
13
|
+
reference to other code, e.g. `node.parent = other_node`.
|
14
|
+
- The database contents, which can fluctuate within the bounds of the DB schema.
|
15
|
+
Data can reference to other data, ideally via foreign keys.
|
16
|
+
- A mix of the two, where code needs to be specifically tailored for some kind
|
17
|
+
of data. A prominent example of such a mix would for instance be user roles:
|
18
|
+
roles must be hardcoded in the code because security logic is tied to them.
|
19
|
+
However, as users are assigned to roles in the database, roles also need to be
|
20
|
+
persisted in the database. This is where Anchormodel comes into play.
|
21
|
+
|
22
|
+
## Alternatives coviering the same use case
|
23
|
+
|
24
|
+
### Enum
|
25
|
+
|
26
|
+
In Rails and other frameworks, the third point in the listing above is typically
|
27
|
+
achieved via Enums. Enums map either Integers or Strings to constants and
|
28
|
+
therefore provide a link between the DB and the application code.
|
29
|
+
|
30
|
+
However, at least in Rails, Enums provide very limited customization options.
|
31
|
+
They are basic values that can be used in if-statements. Anchormodels however
|
32
|
+
are regular classes and can easily be extended.
|
33
|
+
|
34
|
+
### ActiveEnum
|
35
|
+
|
36
|
+
The gem (ActiveEnum)[https://github.com/adzap/active_enum] allows to create
|
37
|
+
Enum-like classes that can be extended. However it only supports Integer keys. I
|
38
|
+
find this unsatisfactory, as debugging with tools like `psql` or `mysql` is made
|
39
|
+
unnecessarily hard when you only see numbers. Keys for enums should be
|
40
|
+
meaningful, making you immediately understand what they stand for.
|
41
|
+
|
42
|
+
This is why Anchormodel is strictly relying on String keys corresponding to the
|
43
|
+
entries of an Anchormodel.
|
44
|
+
|
45
|
+
# Example
|
46
|
+
|
47
|
+
`app/anchormodels/role.rb`:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
class Anchormodels::Role < Anchormodel
|
51
|
+
# Expose the attribute privilege_level
|
52
|
+
attr_reader :privilege_level
|
53
|
+
|
54
|
+
# Overload <=> to make user roles comparable based on the privilege level
|
55
|
+
def <=>(other)
|
56
|
+
other.privilege_level <=> @privilege_level
|
57
|
+
end
|
58
|
+
|
59
|
+
# Declare all available roles
|
60
|
+
new :guest, privilege_level: 0
|
61
|
+
new :manager, privilege_level: 1
|
62
|
+
new :admin, privilege_level: 2
|
63
|
+
end
|
64
|
+
```
|
65
|
+
|
66
|
+
`app/models/user.rb`
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
# The DB table `users` must have a String column `users.role`
|
70
|
+
class User < ApplicationRecord
|
71
|
+
belongs_to_anchormodel :role
|
72
|
+
end
|
73
|
+
```
|
74
|
+
|
75
|
+
You may now use the following methods:
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
# Retrieve all user roles:
|
79
|
+
Anchormodels::Role.all
|
80
|
+
|
81
|
+
# Retrieve a specific role from the String and find its privilege level
|
82
|
+
Anchormodels::Role.find(:guest).privilege_level
|
83
|
+
|
84
|
+
# Implement a Rails helper that makes sure users can only edit other users that have a lower privilege level than themselves
|
85
|
+
def user_can_edit?(this_user, other_user)
|
86
|
+
this_user.role.privilege_level > other_user.role.privilege_level
|
87
|
+
end
|
88
|
+
|
89
|
+
# Pretty print a user's role, e.g. using the Rails FastGettext gem:
|
90
|
+
puts("User #{@user.name} has role #{@user.role.label}")
|
91
|
+
```
|
92
|
+
|
93
|
+
# Installation
|
94
|
+
|
95
|
+
1. Add gem to Gemfile: `gem 'anchormodel'`
|
96
|
+
2. In `application_record.rb`, add in the class body: `include Anchormodel::ModelMixin`
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require_relative 'lib/anchormodel/version'
|
3
|
+
|
4
|
+
task :gemspec do
|
5
|
+
specification = Gem::Specification.new do |s|
|
6
|
+
s.name = 'anchormodel'
|
7
|
+
s.version = Anchormodel::Version::LABEL
|
8
|
+
s.author = ['Sandro Kalbermatter']
|
9
|
+
s.summary = 'Bringing object-oriented programming to Rails enums'
|
10
|
+
s.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
11
|
+
s.executables = []
|
12
|
+
s.require_paths = ['lib']
|
13
|
+
s.required_ruby_version = '>= 3.0.0'
|
14
|
+
|
15
|
+
# Dependencies
|
16
|
+
s.add_runtime_dependency 'rails', '>= 7.0'
|
17
|
+
|
18
|
+
s.add_development_dependency 'yard', '>= 0.9.28'
|
19
|
+
s.add_development_dependency 'yard-activesupport-concern'
|
20
|
+
end
|
21
|
+
|
22
|
+
File.open('anchormodel.gemspec', 'w') do |f|
|
23
|
+
f.puts('# DO NOT EDIT')
|
24
|
+
f.puts("# This file is auto-generated via: 'rake gemspec'.\n\n")
|
25
|
+
f.write(specification.to_ruby.strip)
|
26
|
+
end
|
27
|
+
end
|
data/anchormodel.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# DO NOT EDIT
|
2
|
+
# This file is auto-generated via: 'rake gemspec'.
|
3
|
+
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
# stub: anchormodel 0.0.1 ruby lib
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "anchormodel".freeze
|
9
|
+
s.version = "0.0.1"
|
10
|
+
|
11
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
12
|
+
s.require_paths = ["lib".freeze]
|
13
|
+
s.authors = ["Sandro Kalbermatter".freeze]
|
14
|
+
s.date = "2022-12-17"
|
15
|
+
s.files = [".gitignore".freeze, ".ruby-version".freeze, ".yardopts".freeze, "LICENSE".freeze, "README.md".freeze, "Rakefile".freeze, "anchormodel.gemspec".freeze, "doc/Anchormodel.html".freeze, "doc/Anchormodel/ActiveModelTypeValue.html".freeze, "doc/Anchormodel/Attribute.html".freeze, "doc/Anchormodel/ModelMixin.html".freeze, "doc/Anchormodel/Version.html".freeze, "doc/_index.html".freeze, "doc/class_list.html".freeze, "doc/css/common.css".freeze, "doc/css/full_list.css".freeze, "doc/css/style.css".freeze, "doc/file.README.html".freeze, "doc/file_list.html".freeze, "doc/frames.html".freeze, "doc/index.html".freeze, "doc/js/app.js".freeze, "doc/js/full_list.js".freeze, "doc/js/jquery.js".freeze, "doc/method_list.html".freeze, "doc/top-level-namespace.html".freeze, "lib/anchormodel.rb".freeze, "lib/anchormodel/active_model_type_value.rb".freeze, "lib/anchormodel/attribute.rb".freeze, "lib/anchormodel/model_mixin.rb".freeze, "lib/anchormodel/version.rb".freeze]
|
16
|
+
s.required_ruby_version = Gem::Requirement.new(">= 3.0.0".freeze)
|
17
|
+
s.rubygems_version = "3.2.33".freeze
|
18
|
+
s.summary = "Bringing object-oriented programming to Rails enums".freeze
|
19
|
+
|
20
|
+
if s.respond_to? :specification_version then
|
21
|
+
s.specification_version = 4
|
22
|
+
end
|
23
|
+
|
24
|
+
if s.respond_to? :add_runtime_dependency then
|
25
|
+
s.add_runtime_dependency(%q<rails>.freeze, [">= 7.0"])
|
26
|
+
s.add_development_dependency(%q<yard>.freeze, [">= 0.9.28"])
|
27
|
+
s.add_development_dependency(%q<yard-activesupport-concern>.freeze, [">= 0"])
|
28
|
+
else
|
29
|
+
s.add_dependency(%q<rails>.freeze, [">= 7.0"])
|
30
|
+
s.add_dependency(%q<yard>.freeze, [">= 0.9.28"])
|
31
|
+
s.add_dependency(%q<yard-activesupport-concern>.freeze, [">= 0"])
|
32
|
+
end
|
33
|
+
end
|