rich_enums 0.1.4 → 0.1.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 15ff8f1970f8dfcea0ea54020b43e39e9a2107978e1cc432e38797698e8b5262
4
- data.tar.gz: d1f1cfd235a02224c8b92d9847daf799512ea9c34d5cf847e128d6a9473b8322
3
+ metadata.gz: 23864e56ec97162d3609d1478dd9b42f51eec0a5b1b3ad6fefede47b9f629dfa
4
+ data.tar.gz: 69522c7f2420aa3135c213b159e871ad2958c20deaced2d02c6f839194369bdf
5
5
  SHA512:
6
- metadata.gz: 61f4cd02dab095f81082e66daafb43709edb159a7dbbd906362e5d31458fef82064855b8ebe1e32eed8143818b0cf96974aa571012d53e9fac80539b3b6653e8
7
- data.tar.gz: 50305e6f7e0377be49cb7289dd52aaf0c00263bf5d45f472a8d4e86b9f67789613740c2359d411ae6d6f51932530fd6049bd408a2082353af9b1e18311ca6372
6
+ metadata.gz: 7a88fcda980a44065ec3c23a9d1871ac800f57ebe2b045e58718cd26095507506c69aec0b97480c0f211f5e117eacab7ace002a4738410339e109c1a001f8f93
7
+ data.tar.gz: '00880f968a5f479663c987ce2c5991c4cc7f065bdda3701122dd73dcef0283cc93e7cb17521bcc1cbc2d36ca8c8c919ccc48805bca09be7ec2137f53659e602a'
@@ -23,6 +23,8 @@ jobs:
23
23
  strategy:
24
24
  matrix:
25
25
  include:
26
+ - ruby: '3.3'
27
+ rails: '7.2'
26
28
  - ruby: '3.2'
27
29
  rails: '7.1'
28
30
  - ruby: '3.2'
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 3.1.1
1
+ 3.3.4
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rich_enums (0.1.4)
4
+ rich_enums (0.1.5)
5
5
  activerecord (>= 6.1, < 8.0)
6
6
 
7
7
  GEM
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,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.4'
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
@@ -57,21 +62,33 @@ module RichEnums
57
62
  options = column_symbol_value_string_options
58
63
  # we allow for an option called alt: to allow the users to tag the alternate mapping. Defaults to 'alt_name'
59
64
  alt = options.delete(:alt).to_s || 'alt_name'
65
+ alt_map_method = 'alt_name_to_ids'
60
66
 
61
67
  # create two hashes from the provided input - 1 to be used to define the enum and the other for the name map
62
- split_hash = symbol_value_string.each_with_object({ for_enum: {}, for_display: {} }) do |(symbol, value_string), obj|
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
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
65
75
  end
66
76
 
67
77
  # 1. Define the Enum
68
- 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
69
83
 
70
- # 2. Define our custom class method
71
- # - 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
72
86
  instance_variable_set("@#{column}_#{alt.pluralize}", split_hash[:for_display])
73
- # - 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
74
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}") }
75
92
 
76
93
  # 3. Define our custom instance method to show the String associated with the enum value
77
94
  define_method("#{column}_#{alt}") { self.class.send("#{column}_#{alt.pluralize}")[send(column.to_s)] }
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.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - harunkumars
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2024-04-29 00:00:00.000000000 Z
12
+ date: 2024-08-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sqlite3
@@ -86,6 +86,9 @@ files:
86
86
  - gemfiles/activerecord_6.1.gemfile
87
87
  - gemfiles/activerecord_7.0.gemfile
88
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
89
92
  - lib/rich_enums.rb
90
93
  - lib/rich_enums/version.rb
91
94
  - rich_enums.gemspec
@@ -112,7 +115,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
115
  - !ruby/object:Gem::Version
113
116
  version: '0'
114
117
  requirements: []
115
- rubygems_version: 3.5.0.dev
118
+ rubygems_version: 3.5.11
116
119
  signing_key:
117
120
  specification_version: 4
118
121
  summary: When a simple name to value mapping is not enough