swifter_enum 0.9.1 → 0.9.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +31 -25
- data/Rakefile +0 -2
- data/lib/swifter_enum/base.rb +23 -3
- data/lib/swifter_enum/generators/enum/templates/enum.rb.tt +7 -5
- data/lib/swifter_enum/swifter_enum.rb +0 -1
- data/lib/swifter_enum/swifter_enum_validator.rb +7 -2
- data/lib/swifter_enum/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c619ac4afac5bcbad7b49e421d04faa75bd558611297effa54c992a9509b7d7f
|
4
|
+
data.tar.gz: 2fa59214a51e2417b6834362fcce525f5ca10a4fdf962261576252a0f6f6071f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 02d2882937c808e0c9c652ee7cf85cbea7a0d4425e33e7656718ac906bd475a2173ab5ec8d0f79d1a61b9ef8f634d76696429eac7500e7da33d039adb52a7038
|
7
|
+
data.tar.gz: 5f4186f334a5f9fedb4e670a8f0dea82872e9d9733e96f44c0f9867438effa96d1408b85fa2f13d628487cf922ca5ee763f2c36e2c2dc963ce0eea431cf7fb1e
|
data/README.md
CHANGED
@@ -36,9 +36,7 @@ logic encapsluated within the enum class
|
|
36
36
|
|
37
37
|
#app/models/swifter_enum/camera_enum.rb
|
38
38
|
class CameraEnum < SwifterEnum::Base
|
39
|
-
|
40
|
-
{ videographer: 0, handcam: 1 }.freeze
|
41
|
-
end
|
39
|
+
set_values ({ videographer: 0, handcam: 1 })
|
42
40
|
|
43
41
|
def icon
|
44
42
|
...
|
@@ -76,9 +74,7 @@ We have a Video ActiveModel with an enum defined by
|
|
76
74
|
CameraEnum is a class like the following
|
77
75
|
|
78
76
|
class CameraEnum < SwifterEnum::Base
|
79
|
-
|
80
|
-
{ videographer: 0, handcam: 1 }.freeze
|
81
|
-
end
|
77
|
+
set_values ({ videographer: 0, handcam: 1 })
|
82
78
|
|
83
79
|
def icon
|
84
80
|
case @value
|
@@ -125,9 +121,7 @@ Models are by convention stored in `/models/swifter_enum/your_model_enum.rb`
|
|
125
121
|
Example:
|
126
122
|
|
127
123
|
class CameraEnum < SwifterEnum::Base
|
128
|
-
|
129
|
-
{ videographer: 0, handcam: 1 }.freeze
|
130
|
-
end
|
124
|
+
set_values ({ videographer: 0, handcam: 1 })
|
131
125
|
|
132
126
|
def icon
|
133
127
|
case @value
|
@@ -168,18 +162,16 @@ run the generator to create an appropriate enum class
|
|
168
162
|
Insert the values of your enum into `models/swifter_enum_album_status_enum.rb`
|
169
163
|
|
170
164
|
class AlbumStatusEnum < SwifterEnum::Base
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
}.freeze
|
182
|
-
end
|
165
|
+
set_values ({
|
166
|
+
waiting_for_footage: 0,
|
167
|
+
waiting_for_upload: 10,
|
168
|
+
uploading: 20,
|
169
|
+
processing_preview: 24,
|
170
|
+
delivered_preview: 26,
|
171
|
+
processing_paid: 30,
|
172
|
+
delivered_paid: 50,
|
173
|
+
processing_failed: 60
|
174
|
+
})
|
183
175
|
end
|
184
176
|
|
185
177
|
Now replace the definition in your model file with
|
@@ -240,10 +232,7 @@ For example, to create a `CameraEnum`, run:
|
|
240
232
|
This command will generate a file `app/models/swifter_enum/camera_enum.rb` with the following structure:
|
241
233
|
|
242
234
|
class CameraEnum < SwifterEnum::Base
|
243
|
-
|
244
|
-
# Insert your values here. e.g. { foo: 1, bar: 2 }.freeze
|
245
|
-
{ }.freeze
|
246
|
-
end
|
235
|
+
set_values <<Your Values Here>>
|
247
236
|
end
|
248
237
|
|
249
238
|
After generating your enum, you can add your specific enum values and use it in your ActiveRecord models.
|
@@ -264,6 +253,23 @@ Locale file example (`config/locales/en.yml`):
|
|
264
253
|
#example usage
|
265
254
|
v.camera.t => "Videographer"
|
266
255
|
|
256
|
+
### Using string values to store Enum
|
257
|
+
|
258
|
+
DHH has described using integer values for enums as a mistake he regrets.
|
259
|
+
|
260
|
+
He has shown code like
|
261
|
+
|
262
|
+
enum direction: %w[ up down left right ].index_by(&:itself)
|
263
|
+
|
264
|
+
which uses string values in the db by generating the hash `{"up"=>"up", "down"=>"down", "left"=>"left", "right"=>"right"}`
|
265
|
+
|
266
|
+
Swifter enum allows the same - but lets you simply set your values using an array of strings or symbols
|
267
|
+
|
268
|
+
set_values %w[ up down left right]
|
269
|
+
#or
|
270
|
+
set_values [:up,:down,:left,:right]
|
271
|
+
|
272
|
+
|
267
273
|
### Raw Value Escape Hatch
|
268
274
|
|
269
275
|
SwifterEnum is built on top of the normal Rails enum functionality.
|
data/Rakefile
CHANGED
@@ -12,7 +12,6 @@ Rake::TestTask.new do |t|
|
|
12
12
|
t.verbose = true
|
13
13
|
end
|
14
14
|
|
15
|
-
|
16
15
|
# publish with rake release:publish
|
17
16
|
namespace :release do
|
18
17
|
desc "Read version, build gem, tag and push release"
|
@@ -42,5 +41,4 @@ namespace :release do
|
|
42
41
|
end
|
43
42
|
end
|
44
43
|
|
45
|
-
|
46
44
|
task default: [:standard, :test]
|
data/lib/swifter_enum/base.rb
CHANGED
@@ -1,11 +1,31 @@
|
|
1
1
|
module SwifterEnum
|
2
2
|
class Base
|
3
|
-
|
3
|
+
class << self
|
4
|
+
attr_accessor :values
|
5
|
+
|
6
|
+
def set_values(input)
|
7
|
+
case input
|
8
|
+
when Hash
|
9
|
+
@values = input.freeze
|
10
|
+
when Array
|
11
|
+
validate_array_elements!(input)
|
12
|
+
@values = input.map { |item| [item.to_sym, item.to_s] }.to_h.freeze
|
13
|
+
else
|
14
|
+
raise ArgumentError, "Input must be a Hash or an Array of symbols or strings"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
4
19
|
|
5
|
-
|
6
|
-
|
20
|
+
def validate_array_elements!(array)
|
21
|
+
unless array.all? { |item| item.is_a?(Symbol) || item.is_a?(String) }
|
22
|
+
raise ArgumentError, "Array elements must all be symbols or strings"
|
23
|
+
end
|
24
|
+
end
|
7
25
|
end
|
8
26
|
|
27
|
+
attr_reader :value
|
28
|
+
|
9
29
|
def initialize(value)
|
10
30
|
@value = value&.to_sym
|
11
31
|
end
|
@@ -3,12 +3,14 @@
|
|
3
3
|
|
4
4
|
class <%= class_name %>Enum < SwifterEnum::Base
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
#set your values using a hash, or array of symbols
|
7
|
+
#if you're using a hash with symbol:integer, then your database column should be of integer type
|
8
|
+
# e.g. set_values ({active: 10, inactive: 20})
|
9
|
+
# or set_values active: 10, inactive: 20
|
10
|
+
#if you're using an array of symbols (or strings), then your database column should be of string type
|
11
|
+
# e.g. set_values [:active,:passive]
|
9
12
|
|
10
|
-
|
11
|
-
end
|
13
|
+
set_values ##Your Values Here##
|
12
14
|
|
13
15
|
# you can now define methods on the enum
|
14
16
|
# this would allow you to access YourModel.<%= file_name %>.squared
|
@@ -3,13 +3,18 @@ require "active_model"
|
|
3
3
|
module SwifterEnum
|
4
4
|
class SwifterEnumValidator < ActiveModel::EachValidator
|
5
5
|
def validate_each(record, attribute, a_value)
|
6
|
-
return if options[:allow_nil] && a_value.nil?
|
7
|
-
|
8
6
|
if !a_value.is_a?(SwifterEnum::Base) || a_value.instance_of?(SwifterEnum::Base)
|
9
7
|
record.errors.add(attribute, "#{a_value.value} is not a valid subclass of SwifterEnum")
|
10
8
|
end
|
11
9
|
|
10
|
+
if a_value.value.nil?
|
11
|
+
return if options[:allow_nil]
|
12
|
+
|
13
|
+
record.errors.add(attribute, "nil value for #{attribute} is not allowed")
|
14
|
+
end
|
15
|
+
|
12
16
|
unless a_value.class.values.key?(a_value.value)
|
17
|
+
# binding.b
|
13
18
|
record.errors.add(attribute, "#{a_value.value} is not a valid #{attribute} type")
|
14
19
|
end
|
15
20
|
end
|
data/lib/swifter_enum/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: swifter_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rob Jonson
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-03-
|
11
|
+
date: 2024-03-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|