rich_enums 0.1.3 → 0.1.5

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: 1f6b109c5069cabe1da55839403d6f9efee399e46b5939be951a9604156911d2
4
- data.tar.gz: 97582e336b09b8d5d2376bf73bc1b65522b9cd93f60daf17642bba5d9f02196a
3
+ metadata.gz: 23864e56ec97162d3609d1478dd9b42f51eec0a5b1b3ad6fefede47b9f629dfa
4
+ data.tar.gz: 69522c7f2420aa3135c213b159e871ad2958c20deaced2d02c6f839194369bdf
5
5
  SHA512:
6
- metadata.gz: a1d592ab62bf202968448bcc4833a5b4abddd1b42a919834fcc730216a130fac8294bf997fe772422b1e36e0d9730c95abec7d008fd30f3a763c66356f7e3978
7
- data.tar.gz: '049113830f6ab99a5870e7f41125f8544bdcca86cbf6947643059d1e265b957aa27de48d46bd49a8db9cee91d0040cd3777fb71e837dc082cca2ac199481e911'
6
+ metadata.gz: 7a88fcda980a44065ec3c23a9d1871ac800f57ebe2b045e58718cd26095507506c69aec0b97480c0f211f5e117eacab7ace002a4738410339e109c1a001f8f93
7
+ data.tar.gz: '00880f968a5f479663c987ce2c5991c4cc7f065bdda3701122dd73dcef0283cc93e7cb17521bcc1cbc2d36ca8c8c919ccc48805bca09be7ec2137f53659e602a'
@@ -0,0 +1,56 @@
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.3'
27
+ rails: '7.2'
28
+ - ruby: '3.2'
29
+ rails: '7.1'
30
+ - ruby: '3.2'
31
+ rails: '7.0'
32
+ - ruby: '3.2'
33
+ rails: '6.1'
34
+ - ruby: '3.1'
35
+ rails: '6.1'
36
+ env:
37
+ BUNDLE_GEMFILE: gemfiles/activerecord_${{ matrix.rails }}.gemfile
38
+ steps:
39
+ - name: Checkout
40
+ uses: actions/checkout@v4
41
+ - name: Install Ruby
42
+ uses: ruby/setup-ruby@v1
43
+ with:
44
+ ruby-version: ${{ matrix.ruby }}
45
+ - name: Gem cache
46
+ uses: actions/cache@v4
47
+ with:
48
+ path: vendor/bundle
49
+ key: ${{ runner.os }}-gem-use-ruby-${{ hashFiles('**/Gemfile.lock') }}
50
+ - name: Install dependencies
51
+ run: |
52
+ gem install bundler --version 2.3.25 --no-document
53
+ bundle config path vendor/bundle
54
+ bundle install --jobs 4 --retry 3
55
+ - name: Run tests
56
+ run: bundle exec rake spec
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.3.4
data/Gemfile.lock CHANGED
@@ -1,12 +1,29 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rich_enums (0.1.3)
4
+ rich_enums (0.1.5)
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
61
  2.4.15
data/README.md CHANGED
@@ -17,6 +17,7 @@ user.role_code # => 'ROLE001'
17
17
  user.role_for_database # => 1
18
18
  User.roles # => {"admin"=>1, "user"=>2}
19
19
  User.role_codes # => {"admin"=>"ROLE001", "user"=>"ROLE101"}
20
+ User.role_alt_name_to_ids # => {"ROLE001"=>1, "ROLE101"=>2}
20
21
 
21
22
  ```
22
23
 
@@ -61,6 +62,7 @@ user.role_alt_name # => 'ROLE001'
61
62
  user.role_for_database # => 1
62
63
  User.roles # => {"admin"=>1, "user"=>2}
63
64
  User.role_alt_names # => {"admin"=>"ROLE001", "user"=>"ROLE101"}
65
+ User.role_alt_name_to_ids # => {"ROLE001"=>1, "ROLE101"=>2}
64
66
  ExternalSystem.sync(user.external_id, role_code: user.role_alt_name)
65
67
  ```
66
68
  Any arguments other than 'alt' are forwarded to the default enum definition.
@@ -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: "../"
@@ -0,0 +1,72 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ rich_enums (0.1.5)
5
+ activerecord (>= 6.1, < 8.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (7.1.3.2)
11
+ activesupport (= 7.1.3.2)
12
+ activerecord (7.1.3.2)
13
+ activemodel (= 7.1.3.2)
14
+ activesupport (= 7.1.3.2)
15
+ timeout (>= 0.4.0)
16
+ activesupport (7.1.3.2)
17
+ base64
18
+ bigdecimal
19
+ concurrent-ruby (~> 1.0, >= 1.0.2)
20
+ connection_pool (>= 2.2.5)
21
+ drb
22
+ i18n (>= 1.6, < 2)
23
+ minitest (>= 5.1)
24
+ mutex_m
25
+ tzinfo (~> 2.0)
26
+ base64 (0.2.0)
27
+ bigdecimal (3.1.7)
28
+ concurrent-ruby (1.2.3)
29
+ connection_pool (2.4.1)
30
+ diff-lcs (1.5.1)
31
+ drb (2.2.1)
32
+ i18n (1.14.4)
33
+ concurrent-ruby (~> 1.0)
34
+ minitest (5.22.3)
35
+ mutex_m (0.2.0)
36
+ rake (12.3.3)
37
+ rspec (3.13.0)
38
+ rspec-core (~> 3.13.0)
39
+ rspec-expectations (~> 3.13.0)
40
+ rspec-mocks (~> 3.13.0)
41
+ rspec-core (3.13.0)
42
+ rspec-support (~> 3.13.0)
43
+ rspec-expectations (3.13.0)
44
+ diff-lcs (>= 1.2.0, < 2.0)
45
+ rspec-support (~> 3.13.0)
46
+ rspec-mocks (3.13.0)
47
+ diff-lcs (>= 1.2.0, < 2.0)
48
+ rspec-support (~> 3.13.0)
49
+ rspec-support (3.13.1)
50
+ sqlite3 (1.7.3-arm64-darwin)
51
+ sqlite3 (1.7.3-x86_64-darwin)
52
+ temping (4.1.1)
53
+ activerecord (>= 6.0, < 7.2)
54
+ activesupport (>= 6.0, < 7.2)
55
+ timeout (0.4.1)
56
+ tzinfo (2.0.6)
57
+ concurrent-ruby (~> 1.0)
58
+
59
+ PLATFORMS
60
+ arm64-darwin-23
61
+ x86_64-darwin-21
62
+
63
+ DEPENDENCIES
64
+ activerecord (~> 7.1)
65
+ rake (~> 12.0)
66
+ rich_enums!
67
+ rspec (~> 3.0)
68
+ sqlite3 (~> 1.4)
69
+ temping (~> 4.1)
70
+
71
+ BUNDLED WITH
72
+ 2.5.16
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "activerecord", "~> 7.2"
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,74 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ rich_enums (0.1.5)
5
+ activerecord (>= 6.1, < 8.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (7.2.0)
11
+ activesupport (= 7.2.0)
12
+ activerecord (7.2.0)
13
+ activemodel (= 7.2.0)
14
+ activesupport (= 7.2.0)
15
+ timeout (>= 0.4.0)
16
+ activesupport (7.2.0)
17
+ base64
18
+ bigdecimal
19
+ concurrent-ruby (~> 1.0, >= 1.3.1)
20
+ connection_pool (>= 2.2.5)
21
+ drb
22
+ i18n (>= 1.6, < 2)
23
+ logger (>= 1.4.2)
24
+ minitest (>= 5.1)
25
+ securerandom (>= 0.3)
26
+ tzinfo (~> 2.0, >= 2.0.5)
27
+ base64 (0.2.0)
28
+ bigdecimal (3.1.8)
29
+ concurrent-ruby (1.3.4)
30
+ connection_pool (2.4.1)
31
+ diff-lcs (1.5.1)
32
+ drb (2.2.1)
33
+ i18n (1.14.5)
34
+ concurrent-ruby (~> 1.0)
35
+ logger (1.6.0)
36
+ minitest (5.25.1)
37
+ rake (12.3.3)
38
+ rspec (3.13.0)
39
+ rspec-core (~> 3.13.0)
40
+ rspec-expectations (~> 3.13.0)
41
+ rspec-mocks (~> 3.13.0)
42
+ rspec-core (3.13.0)
43
+ rspec-support (~> 3.13.0)
44
+ rspec-expectations (3.13.2)
45
+ diff-lcs (>= 1.2.0, < 2.0)
46
+ rspec-support (~> 3.13.0)
47
+ rspec-mocks (3.13.1)
48
+ diff-lcs (>= 1.2.0, < 2.0)
49
+ rspec-support (~> 3.13.0)
50
+ rspec-support (3.13.1)
51
+ securerandom (0.3.1)
52
+ sqlite3 (1.7.3-arm64-darwin)
53
+ sqlite3 (1.7.3-x86_64-darwin)
54
+ temping (4.2.0)
55
+ activerecord (>= 6.0, < 7.3)
56
+ activesupport (>= 6.0, < 7.3)
57
+ timeout (0.4.1)
58
+ tzinfo (2.0.6)
59
+ concurrent-ruby (~> 1.0)
60
+
61
+ PLATFORMS
62
+ arm64-darwin-23
63
+ x86_64-darwin-21
64
+
65
+ DEPENDENCIES
66
+ activerecord (~> 7.2)
67
+ rake (~> 12.0)
68
+ rich_enums!
69
+ rspec (~> 3.0)
70
+ sqlite3 (~> 1.4)
71
+ temping (~> 4.1)
72
+
73
+ BUNDLED WITH
74
+ 2.5.16
@@ -1,3 +1,3 @@
1
1
  module RichEnums
2
- VERSION = '0.1.3'
2
+ VERSION = '0.1.5'
3
3
  end
data/lib/rich_enums.rb CHANGED
@@ -15,6 +15,8 @@ module RichEnums
15
15
  # and
16
16
  # 2. Class method "column1_names" that will map the enum values to the full String description
17
17
  # and can be accessed by ClassName.<column>_names which will return a hash like { symbol1: string1, symbol2: string2 ...}
18
+ # 3. Class method "column1_alt_name_to_ids" will map the enum values the string values and can
19
+ # be accessed by ClassName.<column>_alt_name_to_ids which will return a hash like { string1: value1, string2: value2 ...}
18
20
  # e.g.
19
21
  # class Enrollment
20
22
  # include RichEnums
@@ -31,6 +33,9 @@ module RichEnums
31
33
  # Calling learner_payment_path_names returns
32
34
  # {"greenfig_online"=>"GreenFig Online", "partner"=>"Partner", "partner_online"=>"Partner Online",
33
35
  # "po_check"=>"P.O. / Check", "other"=>"Other"}
36
+ # iii. a class method called learner_payment_path_alt_name_to_ids
37
+ # Calling learner_payment_path_alt_name_to_ids returns
38
+ # {"GreenFig Online"=>10, "Partner"=>20, "Partner Online"=>30, "P.O. / Check"=>40, "Other"=>100}
34
39
  # 3. Instance method "column1_name" will return the String associated with the enum value
35
40
  # e = Enrollment.new
36
41
  # e.learner_payment_path_po_check! -> normal enum method to update the object with enum value
@@ -38,32 +43,52 @@ module RichEnums
38
43
  # e.learner_payment_path_name --> "P.O. / Check" -> our custom method that returns the string/description
39
44
  # TODO: explore if enum options in Array format instead of Hash format will need to be handled
40
45
 
41
- raise 'rich_enum error' unless column_symbol_value_string_options.keys.count.positive?
46
+ unless column_symbol_value_string_options.is_a? Hash
47
+ raise RichEnums::Error
48
+ end
49
+ if column_symbol_value_string_options.keys.count.zero?
50
+ raise RichEnums::Error
51
+ end
42
52
 
43
53
  # extract out the column
44
54
  column = column_symbol_value_string_options.keys.first
45
55
 
46
56
  # extract the Enum options for the column which may be in standard enum hash format or our custom format
47
57
  symbol_value_string = column_symbol_value_string_options.delete(column)
58
+
59
+ raise RichEnums::Error unless symbol_value_string.is_a? Hash
60
+
48
61
  # at this point, only the enum options like _prefix etc. are present in the original argument
49
62
  options = column_symbol_value_string_options
50
63
  # 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'
64
+ alt = options.delete(:alt).to_s || 'alt_name'
65
+ alt_map_method = 'alt_name_to_ids'
52
66
 
53
67
  # create two hashes from the provided input - 1 to be used to define the enum and the other for the name map
54
- 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
68
+ split_hash = symbol_value_string.each_with_object({ for_enum: {}, for_display: {}, for_filter: {} }) do |(symbol, value_string), obj|
69
+ value = value_string.is_a?(Array) ? value_string.first : value_string
70
+ display_string = value_string.is_a?(Array) ? value_string.second : symbol.to_s
71
+
72
+ obj[:for_enum][symbol] = value
73
+ obj[:for_display][symbol.to_s] = display_string
74
+ obj[:for_filter][display_string] = value
57
75
  end
58
76
 
59
77
  # 1. Define the Enum
60
- enum "#{column}": split_hash[:for_enum], **options
78
+ if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('7.2')
79
+ enum column, split_hash[:for_enum], **options
80
+ else
81
+ enum "#{column}": split_hash[:for_enum], **options
82
+ end
61
83
 
62
- # 2. Define our custom class method
63
- # - the data to be returned by our custom method is available os a class instance variable
84
+ # 2. Define our custom class methods
85
+ # - the data to be returned by our custom methods is available as a class instance variable
64
86
  instance_variable_set("@#{column}_#{alt.pluralize}", split_hash[:for_display])
65
- # - the custom method is just a getter for the class instance variable
87
+ instance_variable_set("@#{column}_#{alt_map_method}", split_hash[:for_filter])
88
+
89
+ # - the custom methods are just a getter for the class instance variables
66
90
  define_singleton_method("#{column}_#{alt.pluralize}") { instance_variable_get("@#{column}_#{alt.pluralize}") }
91
+ define_singleton_method("#{column}_#{alt_map_method}") { instance_variable_get("@#{column}_#{alt_map_method}") }
67
92
 
68
93
  # 3. Define our custom instance method to show the String associated with the enum value
69
94
  define_method("#{column}_#{alt}") { self.class.send("#{column}_#{alt.pluralize}")[send(column.to_s)] }
data/rich_enums.gemspec CHANGED
@@ -22,6 +22,9 @@ DESC
22
22
  spec.metadata['source_code_uri'] = 'https://github.com/betacraft/rich_enums'
23
23
  spec.metadata['changelog_uri'] = 'https://github.com/betacraft/rich_enums/README.md'
24
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'
25
28
  # Specify which files should be added to the gem when it is released.
26
29
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
27
30
  spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rich_enums
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - harunkumars
@@ -9,8 +9,56 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2023-07-10 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2024-08-21 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'
14
62
  description: "With Enums we are able to map a label to a value on the database. \nUse
15
63
  Rich Enum if you need to maintain an additional mapping at the point of enum definition,
16
64
  \nfor e.g. for presentation purposes or for mapping to a different value on a different
@@ -22,8 +70,10 @@ executables: []
22
70
  extensions: []
23
71
  extra_rdoc_files: []
24
72
  files:
73
+ - ".github/workflows/ci.yml"
25
74
  - ".gitignore"
26
75
  - ".rspec"
76
+ - ".ruby-version"
27
77
  - ".travis.yml"
28
78
  - CODE_OF_CONDUCT.md
29
79
  - Gemfile
@@ -33,6 +83,12 @@ files:
33
83
  - Rakefile
34
84
  - bin/console
35
85
  - bin/setup
86
+ - gemfiles/activerecord_6.1.gemfile
87
+ - gemfiles/activerecord_7.0.gemfile
88
+ - gemfiles/activerecord_7.1.gemfile
89
+ - gemfiles/activerecord_7.1.gemfile.lock
90
+ - gemfiles/activerecord_7.2.gemfile
91
+ - gemfiles/activerecord_7.2.gemfile.lock
36
92
  - lib/rich_enums.rb
37
93
  - lib/rich_enums/version.rb
38
94
  - rich_enums.gemspec
@@ -59,7 +115,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
115
  - !ruby/object:Gem::Version
60
116
  version: '0'
61
117
  requirements: []
62
- rubygems_version: 3.5.0.dev
118
+ rubygems_version: 3.5.11
63
119
  signing_key:
64
120
  specification_version: 4
65
121
  summary: When a simple name to value mapping is not enough