rich_enums 0.1.2 → 0.1.3
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 +4 -4
- data/.gitignore +3 -1
- data/Gemfile.lock +2 -2
- data/README.md +54 -6
- data/lib/rich_enums/version.rb +1 -1
- data/lib/rich_enums.rb +2 -2
- data/rich_enums.gemspec +7 -3
- metadata +12 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1f6b109c5069cabe1da55839403d6f9efee399e46b5939be951a9604156911d2
|
4
|
+
data.tar.gz: 97582e336b09b8d5d2376bf73bc1b65522b9cd93f60daf17642bba5d9f02196a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a1d592ab62bf202968448bcc4833a5b4abddd1b42a919834fcc730216a130fac8294bf997fe772422b1e36e0d9730c95abec7d008fd30f3a763c66356f7e3978
|
7
|
+
data.tar.gz: '049113830f6ab99a5870e7f41125f8544bdcca86cbf6947643059d1e265b957aa27de48d46bd49a8db9cee91d0040cd3777fb71e837dc082cca2ac199481e911'
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,24 @@
|
|
1
1
|
# RichEnums
|
2
2
|
|
3
|
-
|
3
|
+
With Enums we are able to map a label to a value on the database.
|
4
|
+
Use Rich Enum if you need to maintain an additional mapping at the point of enum definition,
|
5
|
+
for e.g. for presentation purposes or for mapping to a different value on a different system.
|
4
6
|
|
5
|
-
|
7
|
+
e.g. rich enum definition
|
8
|
+
```ruby
|
9
|
+
class User < ApplicationRecord
|
10
|
+
# enum role: { admin: 1, user: 2 } # default enum definition
|
11
|
+
rich_enum role: { admin: [1, 'ROLE001'], user: [2, 'ROLE101'] }, alt: 'code'
|
12
|
+
end
|
13
|
+
|
14
|
+
user = User.new(role: :admin)
|
15
|
+
user.role # => 'admin'
|
16
|
+
user.role_code # => 'ROLE001'
|
17
|
+
user.role_for_database # => 1
|
18
|
+
User.roles # => {"admin"=>1, "user"=>2}
|
19
|
+
User.role_codes # => {"admin"=>"ROLE001", "user"=>"ROLE101"}
|
20
|
+
|
21
|
+
```
|
6
22
|
|
7
23
|
## Installation
|
8
24
|
|
@@ -16,13 +32,45 @@ And then execute:
|
|
16
32
|
|
17
33
|
$ bundle install
|
18
34
|
|
19
|
-
Or
|
35
|
+
Or simply run
|
36
|
+
|
37
|
+
$ bundle add rich_enums
|
38
|
+
to add to Gemfile and run bundle install in one go.
|
20
39
|
|
21
|
-
$ gem install rich_enums
|
22
40
|
|
23
41
|
## Usage
|
42
|
+
As shown in the example above, the rich_enum definition is similar to the default enum definition.
|
43
|
+
It simply augments the enum definition with an additional mapping.
|
44
|
+
|
45
|
+
The additional mapping can be named with the `alt` option. It defaults to 'alt_name' if unspecificed.
|
46
|
+
This comes in handy when you need to map to a different value on a different system.
|
47
|
+
|
48
|
+
1. Using rich_enum to define your enums provides you with an instance method (attribute name with a suffix specified with the alt property - defaults to _alt_name) to access the alternate value from the additional mapping.
|
49
|
+
2. It also provides you with a class method(attribute name with a plural suffix derived from the alt option - defaults to _alt_names) to access the additional mapping.
|
24
50
|
|
25
|
-
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
class User < ApplicationRecord
|
54
|
+
# enum role: { admin: 1, user: 2 } # default enum definition
|
55
|
+
rich_enum role: { admin: [1, 'ROLE001'], user: [2, 'ROLE101'] } # if alt is not specified, it defaults to 'alt_name'
|
56
|
+
end
|
57
|
+
|
58
|
+
user = User.new(role: :admin)
|
59
|
+
user.role # => 'admin'
|
60
|
+
user.role_alt_name # => 'ROLE001'
|
61
|
+
user.role_for_database # => 1
|
62
|
+
User.roles # => {"admin"=>1, "user"=>2}
|
63
|
+
User.role_alt_names # => {"admin"=>"ROLE001", "user"=>"ROLE101"}
|
64
|
+
ExternalSystem.sync(user.external_id, role_code: user.role_alt_name)
|
65
|
+
```
|
66
|
+
Any arguments other than 'alt' are forwarded to the default enum definition.
|
67
|
+
For e.g. in this case _prefix: true is forwarded to the default enum definition.
|
68
|
+
```ruby
|
69
|
+
rich_enum payment_type: {
|
70
|
+
upfront: [10, 'Full payment'],
|
71
|
+
installment: [20, 'Pay in parts'],
|
72
|
+
}, _prefix: true
|
73
|
+
```
|
26
74
|
|
27
75
|
## Development
|
28
76
|
|
@@ -32,7 +80,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
32
80
|
|
33
81
|
## Contributing
|
34
82
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
83
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/betacraft/rich_enums. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/betacraft/rich_enums/blob/master/CODE_OF_CONDUCT.md).
|
36
84
|
|
37
85
|
|
38
86
|
## License
|
data/lib/rich_enums/version.rb
CHANGED
data/lib/rich_enums.rb
CHANGED
@@ -9,7 +9,7 @@ module RichEnums
|
|
9
9
|
|
10
10
|
module ClassMethods
|
11
11
|
def rich_enum(column_symbol_value_string_options)
|
12
|
-
# rich_enum column1: { symbol1: [value1, string1], ... }, **options
|
12
|
+
# rich_enum column1: { symbol1: [value1, string1], ... }, alt: 'name', **options
|
13
13
|
# will transform to
|
14
14
|
# 1. enum column1: { symbol1: value1, ...}, to define the enums along with any options provided
|
15
15
|
# and
|
@@ -17,7 +17,7 @@ module RichEnums
|
|
17
17
|
# and can be accessed by ClassName.<column>_names which will return a hash like { symbol1: string1, symbol2: string2 ...}
|
18
18
|
# e.g.
|
19
19
|
# class Enrollment
|
20
|
-
# include
|
20
|
+
# include RichEnums
|
21
21
|
# rich_enum learner_payment_path: {
|
22
22
|
# greenfig_online: [10, 'GreenFig Online'],
|
23
23
|
# partner: [20, 'Partner'],
|
data/rich_enums.gemspec
CHANGED
@@ -6,13 +6,17 @@ Gem::Specification.new do |spec|
|
|
6
6
|
spec.authors = %w[harunkumars rtdp]
|
7
7
|
spec.email = %w[harun@betacraft.io rtdp@betacraft.io]
|
8
8
|
|
9
|
-
spec.summary = '
|
10
|
-
|
9
|
+
spec.summary = 'When a simple name to value mapping is not enough'
|
10
|
+
spec.description = <<-DESC
|
11
|
+
With Enums we are able to map a label to a value on the database.
|
12
|
+
Use Rich Enum if you need to maintain an additional mapping at the point of enum definition,
|
13
|
+
for e.g. for presentation purposes or for mapping to a different value on a different system.
|
14
|
+
DESC
|
11
15
|
spec.homepage = 'https://github.com/betacraft/rich_enums'
|
12
16
|
spec.license = 'MIT'
|
13
17
|
spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
|
14
18
|
|
15
|
-
|
19
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
16
20
|
|
17
21
|
spec.metadata['homepage_uri'] = spec.homepage
|
18
22
|
spec.metadata['source_code_uri'] = 'https://github.com/betacraft/rich_enums'
|
metadata
CHANGED
@@ -1,17 +1,20 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rich_enums
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- harunkumars
|
8
8
|
- rtdp
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2023-07-10 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
|
-
description:
|
14
|
+
description: "With Enums we are able to map a label to a value on the database. \nUse
|
15
|
+
Rich Enum if you need to maintain an additional mapping at the point of enum definition,
|
16
|
+
\nfor e.g. for presentation purposes or for mapping to a different value on a different
|
17
|
+
system.\n"
|
15
18
|
email:
|
16
19
|
- harun@betacraft.io
|
17
20
|
- rtdp@betacraft.io
|
@@ -37,10 +40,11 @@ homepage: https://github.com/betacraft/rich_enums
|
|
37
40
|
licenses:
|
38
41
|
- MIT
|
39
42
|
metadata:
|
43
|
+
allowed_push_host: https://rubygems.org
|
40
44
|
homepage_uri: https://github.com/betacraft/rich_enums
|
41
45
|
source_code_uri: https://github.com/betacraft/rich_enums
|
42
46
|
changelog_uri: https://github.com/betacraft/rich_enums/README.md
|
43
|
-
post_install_message:
|
47
|
+
post_install_message:
|
44
48
|
rdoc_options: []
|
45
49
|
require_paths:
|
46
50
|
- lib
|
@@ -55,9 +59,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
55
59
|
- !ruby/object:Gem::Version
|
56
60
|
version: '0'
|
57
61
|
requirements: []
|
58
|
-
rubygems_version: 3.
|
59
|
-
signing_key:
|
62
|
+
rubygems_version: 3.5.0.dev
|
63
|
+
signing_key:
|
60
64
|
specification_version: 4
|
61
|
-
summary:
|
62
|
-
definitions
|
65
|
+
summary: When a simple name to value mapping is not enough
|
63
66
|
test_files: []
|