rich_enums 0.1.2 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 46447b40cc9c43e26f1543e974579f45b0df6df7cf8f8266228d6938668a8524
4
- data.tar.gz: bb0ec8a360ed8b6ef49b038fb56bace8b3511a70146667d9a12d426dedba9b10
3
+ metadata.gz: 15ff8f1970f8dfcea0ea54020b43e39e9a2107978e1cc432e38797698e8b5262
4
+ data.tar.gz: d1f1cfd235a02224c8b92d9847daf799512ea9c34d5cf847e128d6a9473b8322
5
5
  SHA512:
6
- metadata.gz: 8680649383f45fedc1b388500b8f5c06dfb34e6450b16e20583d24d6a774b68ddf177eb18025dbb00ccbc4d8c6f297214e183bad7535dc01655e02a1b26297a2
7
- data.tar.gz: 7c8e3743e48bd6bf09c76b4bcf4f81c8927ce6070f86e02a69d8122de5af7b783c1cd6d5f51f1c4900a1407f2031432441d8727e2a83ebb67dfe5fdc11e44c4e
6
+ metadata.gz: 61f4cd02dab095f81082e66daafb43709edb159a7dbbd906362e5d31458fef82064855b8ebe1e32eed8143818b0cf96974aa571012d53e9fac80539b3b6653e8
7
+ data.tar.gz: 50305e6f7e0377be49cb7289dd52aaf0c00263bf5d45f472a8d4e86b9f67789613740c2359d411ae6d6f51932530fd6049bd408a2082353af9b1e18311ca6372
@@ -0,0 +1,54 @@
1
+ env:
2
+ RUBY_VERSION: 3.2.2
3
+
4
+ name: RichEnums CI
5
+ on:
6
+ pull_request:
7
+ paths-ignore:
8
+ - .gitignore
9
+ - CHANGELOG.md
10
+ - CONTRIBUTING.md
11
+ - README.md
12
+ push:
13
+ paths-ignore:
14
+ - .gitignore
15
+ - CHANGELOG.md
16
+ - CONTRIBUTING.md
17
+ - README.md
18
+
19
+ jobs:
20
+ rspec-test:
21
+ name: Run tests (ruby ${{ matrix.ruby }}, rails ${{ matrix.rails }})
22
+ runs-on: ubuntu-22.04
23
+ strategy:
24
+ matrix:
25
+ include:
26
+ - ruby: '3.2'
27
+ rails: '7.1'
28
+ - ruby: '3.2'
29
+ rails: '7.0'
30
+ - ruby: '3.2'
31
+ rails: '6.1'
32
+ - ruby: '3.1'
33
+ rails: '6.1'
34
+ env:
35
+ BUNDLE_GEMFILE: gemfiles/activerecord_${{ matrix.rails }}.gemfile
36
+ steps:
37
+ - name: Checkout
38
+ uses: actions/checkout@v4
39
+ - name: Install Ruby
40
+ uses: ruby/setup-ruby@v1
41
+ with:
42
+ ruby-version: ${{ matrix.ruby }}
43
+ - name: Gem cache
44
+ uses: actions/cache@v4
45
+ with:
46
+ path: vendor/bundle
47
+ key: ${{ runner.os }}-gem-use-ruby-${{ hashFiles('**/Gemfile.lock') }}
48
+ - name: Install dependencies
49
+ run: |
50
+ gem install bundler --version 2.3.25 --no-document
51
+ bundle config path vendor/bundle
52
+ bundle install --jobs 4 --retry 3
53
+ - name: Run tests
54
+ run: bundle exec rake spec
data/.gitignore CHANGED
@@ -10,4 +10,6 @@
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
12
  .idea
13
- /*.gem
13
+ /*.gem
14
+ .byebug_history
15
+ /*.db
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.1.1
data/Gemfile.lock CHANGED
@@ -1,12 +1,29 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rich_enums (0.1.0)
4
+ rich_enums (0.1.4)
5
+ activerecord (>= 6.1, < 8.0)
5
6
 
6
7
  GEM
7
8
  remote: https://rubygems.org/
8
9
  specs:
10
+ activemodel (6.1.7.7)
11
+ activesupport (= 6.1.7.7)
12
+ activerecord (6.1.7.7)
13
+ activemodel (= 6.1.7.7)
14
+ activesupport (= 6.1.7.7)
15
+ activesupport (6.1.7.7)
16
+ concurrent-ruby (~> 1.0, >= 1.0.2)
17
+ i18n (>= 1.6, < 2)
18
+ minitest (>= 5.1)
19
+ tzinfo (~> 2.0)
20
+ zeitwerk (~> 2.3)
21
+ concurrent-ruby (1.2.3)
9
22
  diff-lcs (1.5.0)
23
+ i18n (1.14.4)
24
+ concurrent-ruby (~> 1.0)
25
+ mini_portile2 (2.8.6)
26
+ minitest (5.22.3)
10
27
  rake (12.3.3)
11
28
  rspec (3.11.0)
12
29
  rspec-core (~> 3.11.0)
@@ -21,6 +38,14 @@ GEM
21
38
  diff-lcs (>= 1.2.0, < 2.0)
22
39
  rspec-support (~> 3.11.0)
23
40
  rspec-support (3.11.0)
41
+ sqlite3 (1.7.3)
42
+ mini_portile2 (~> 2.8.0)
43
+ temping (4.1.1)
44
+ activerecord (>= 6.0, < 7.2)
45
+ activesupport (>= 6.0, < 7.2)
46
+ tzinfo (2.0.6)
47
+ concurrent-ruby (~> 1.0)
48
+ zeitwerk (2.6.13)
24
49
 
25
50
  PLATFORMS
26
51
  ruby
@@ -29,6 +54,8 @@ DEPENDENCIES
29
54
  rake (~> 12.0)
30
55
  rich_enums!
31
56
  rspec (~> 3.0)
57
+ sqlite3 (~> 1.4)
58
+ temping (~> 4.1)
32
59
 
33
60
  BUNDLED WITH
34
- 2.1.4
61
+ 2.4.15
data/README.md CHANGED
@@ -1,8 +1,24 @@
1
1
  # RichEnums
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rich_enums`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- TODO: Delete this and the text above, and describe your gem
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 install it yourself as:
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
- TODO: Write usage instructions here
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/[USERNAME]/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/[USERNAME]/rich_enums/blob/master/CODE_OF_CONDUCT.md).
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
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "activerecord", "~> 6.1"
4
+ gem "rake", "~> 12.0"
5
+ gem "rspec", "~> 3.0"
6
+
7
+ # Specify your gem's dependencies in rich_enums.gemspec
8
+ gemspec path: "../"
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "activerecord", "~> 7.0"
4
+ gem "rake", "~> 12.0"
5
+ gem "rspec", "~> 3.0"
6
+
7
+ # Specify your gem's dependencies in rich_enums.gemspec
8
+ gemspec path: "../"
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "activerecord", "~> 7.1"
4
+ gem "rake", "~> 12.0"
5
+ gem "rspec", "~> 3.0"
6
+
7
+ # Specify your gem's dependencies in rich_enums.gemspec
8
+ gemspec path: "../"
@@ -1,3 +1,3 @@
1
1
  module RichEnums
2
- VERSION = '0.1.2'
2
+ VERSION = '0.1.4'
3
3
  end
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 EnumMappable
20
+ # include RichEnums
21
21
  # rich_enum learner_payment_path: {
22
22
  # greenfig_online: [10, 'GreenFig Online'],
23
23
  # partner: [20, 'Partner'],
@@ -38,22 +38,30 @@ module RichEnums
38
38
  # e.learner_payment_path_name --> "P.O. / Check" -> our custom method that returns the string/description
39
39
  # TODO: explore if enum options in Array format instead of Hash format will need to be handled
40
40
 
41
- raise 'rich_enum error' unless column_symbol_value_string_options.keys.count.positive?
41
+ unless column_symbol_value_string_options.is_a? Hash
42
+ raise RichEnums::Error
43
+ end
44
+ if column_symbol_value_string_options.keys.count.zero?
45
+ raise RichEnums::Error
46
+ end
42
47
 
43
48
  # extract out the column
44
49
  column = column_symbol_value_string_options.keys.first
45
50
 
46
51
  # extract the Enum options for the column which may be in standard enum hash format or our custom format
47
52
  symbol_value_string = column_symbol_value_string_options.delete(column)
53
+
54
+ raise RichEnums::Error unless symbol_value_string.is_a? Hash
55
+
48
56
  # at this point, only the enum options like _prefix etc. are present in the original argument
49
57
  options = column_symbol_value_string_options
50
58
  # we allow for an option called alt: to allow the users to tag the alternate mapping. Defaults to 'alt_name'
51
- alt = options.delete(:alt) || 'alt_name'
59
+ alt = options.delete(:alt).to_s || 'alt_name'
52
60
 
53
61
  # create two hashes from the provided input - 1 to be used to define the enum and the other for the name map
54
62
  split_hash = symbol_value_string.each_with_object({ for_enum: {}, for_display: {} }) do |(symbol, value_string), obj|
55
- obj[:for_enum][symbol] = value_string.is_a?(Array) ? value_string.first : value_string
56
- obj[:for_display][symbol.to_s] = value_string.is_a?(Array) ? value_string.second : symbol.to_s
63
+ obj[:for_enum][symbol] = value_string.is_a?(Array) ? value_string[0] : value_string
64
+ obj[:for_display][symbol.to_s] = value_string.is_a?(Array) ? value_string[1] : symbol.to_s
57
65
  end
58
66
 
59
67
  # 1. Define the Enum
data/rich_enums.gemspec CHANGED
@@ -6,18 +6,25 @@ 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 = 'Provides a rich_enum class method to include String descriptions during Enum definitions'
10
- # spec.description = 'TODO: Write a longer description or delete this line.'
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
- # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
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'
19
23
  spec.metadata['changelog_uri'] = 'https://github.com/betacraft/rich_enums/README.md'
20
24
 
25
+ spec.add_development_dependency 'sqlite3', '~> 1.4'
26
+ spec.add_development_dependency 'temping', '~> 4.1'
27
+ spec.add_runtime_dependency 'activerecord', '>= 6.1', '< 8.0'
21
28
  # Specify which files should be added to the gem when it is released.
22
29
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
30
  spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
metadata CHANGED
@@ -1,17 +1,68 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rich_enums
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
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: 2022-03-22 00:00:00.000000000 Z
13
- dependencies: []
14
- description:
12
+ date: 2024-04-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sqlite3
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.4'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.4'
28
+ - !ruby/object:Gem::Dependency
29
+ name: temping
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '4.1'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '4.1'
42
+ - !ruby/object:Gem::Dependency
43
+ name: activerecord
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '6.1'
49
+ - - "<"
50
+ - !ruby/object:Gem::Version
51
+ version: '8.0'
52
+ type: :runtime
53
+ prerelease: false
54
+ version_requirements: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '6.1'
59
+ - - "<"
60
+ - !ruby/object:Gem::Version
61
+ version: '8.0'
62
+ description: "With Enums we are able to map a label to a value on the database. \nUse
63
+ Rich Enum if you need to maintain an additional mapping at the point of enum definition,
64
+ \nfor e.g. for presentation purposes or for mapping to a different value on a different
65
+ system.\n"
15
66
  email:
16
67
  - harun@betacraft.io
17
68
  - rtdp@betacraft.io
@@ -19,8 +70,10 @@ executables: []
19
70
  extensions: []
20
71
  extra_rdoc_files: []
21
72
  files:
73
+ - ".github/workflows/ci.yml"
22
74
  - ".gitignore"
23
75
  - ".rspec"
76
+ - ".ruby-version"
24
77
  - ".travis.yml"
25
78
  - CODE_OF_CONDUCT.md
26
79
  - Gemfile
@@ -30,6 +83,9 @@ files:
30
83
  - Rakefile
31
84
  - bin/console
32
85
  - bin/setup
86
+ - gemfiles/activerecord_6.1.gemfile
87
+ - gemfiles/activerecord_7.0.gemfile
88
+ - gemfiles/activerecord_7.1.gemfile
33
89
  - lib/rich_enums.rb
34
90
  - lib/rich_enums/version.rb
35
91
  - rich_enums.gemspec
@@ -37,10 +93,11 @@ homepage: https://github.com/betacraft/rich_enums
37
93
  licenses:
38
94
  - MIT
39
95
  metadata:
96
+ allowed_push_host: https://rubygems.org
40
97
  homepage_uri: https://github.com/betacraft/rich_enums
41
98
  source_code_uri: https://github.com/betacraft/rich_enums
42
99
  changelog_uri: https://github.com/betacraft/rich_enums/README.md
43
- post_install_message:
100
+ post_install_message:
44
101
  rdoc_options: []
45
102
  require_paths:
46
103
  - lib
@@ -55,9 +112,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
112
  - !ruby/object:Gem::Version
56
113
  version: '0'
57
114
  requirements: []
58
- rubygems_version: 3.2.22
59
- signing_key:
115
+ rubygems_version: 3.5.0.dev
116
+ signing_key:
60
117
  specification_version: 4
61
- summary: Provides a rich_enum class method to include String descriptions during Enum
62
- definitions
118
+ summary: When a simple name to value mapping is not enough
63
119
  test_files: []