enum_fields 0.1.1 → 0.1.2
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/README.md +61 -61
- data/lib/enum_fields/definition.rb +1 -1
- data/lib/enum_fields/enum_field.rb +9 -0
- data/lib/enum_fields/registry.rb +16 -0
- data/lib/enum_fields/version.rb +1 -1
- data/lib/enum_fields.rb +24 -11
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: bc607b206f6498ca75d71fc583206138080cf768b849ddbe690789281a21921e
|
|
4
|
+
data.tar.gz: da78b379dd988c85daa54c5f29f9562ee03cf86a143eb360f22ccae14547d8a7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e0494cb5c0d8d4afc5ed527fd5c69fd67c1074f631ddaac73896efd7a7553c02f01e6069300356b7a7fc62f960c56e44d8860563ef0847d1bca6588834b423c2
|
|
7
|
+
data.tar.gz: 2b9fef8b1ee2af4fd06a2c6fd36c86655fd694502b291968c836452473c61b3ee33f23849a43e4404b87578c4f87451a4081d369ae0cfe7cedf9c8a70b9fa248
|
data/README.md
CHANGED
|
@@ -12,7 +12,7 @@ Enhanced enum-like fields for ActiveRecord models with metadata support
|
|
|
12
12
|
Add this line to your application's Gemfile:
|
|
13
13
|
|
|
14
14
|
```ruby
|
|
15
|
-
gem
|
|
15
|
+
gem "enum_fields"
|
|
16
16
|
```
|
|
17
17
|
|
|
18
18
|
And then execute:
|
|
@@ -45,32 +45,32 @@ Now all models inheriting from `ApplicationRecord` can use `enum_field`.
|
|
|
45
45
|
class Campaign < ApplicationRecord
|
|
46
46
|
enum_field :stage, {
|
|
47
47
|
pending: {
|
|
48
|
-
value:
|
|
49
|
-
label:
|
|
50
|
-
icon:
|
|
51
|
-
color:
|
|
52
|
-
tooltip:
|
|
48
|
+
value: "pending",
|
|
49
|
+
label: "Pending",
|
|
50
|
+
icon: "clock",
|
|
51
|
+
color: "yellow",
|
|
52
|
+
tooltip: "Campaign is awaiting processing",
|
|
53
53
|
},
|
|
54
54
|
processing: {
|
|
55
|
-
value:
|
|
56
|
-
label:
|
|
57
|
-
icon:
|
|
58
|
-
color:
|
|
59
|
-
tooltip:
|
|
55
|
+
value: "processing",
|
|
56
|
+
label: "Processing",
|
|
57
|
+
icon: "cog",
|
|
58
|
+
color: "blue",
|
|
59
|
+
tooltip: "Campaign is being processed",
|
|
60
60
|
},
|
|
61
61
|
shipped: {
|
|
62
|
-
value:
|
|
63
|
-
label:
|
|
64
|
-
icon:
|
|
65
|
-
color:
|
|
66
|
-
tooltip:
|
|
62
|
+
value: "shipped",
|
|
63
|
+
label: "Shipped",
|
|
64
|
+
icon: "truck",
|
|
65
|
+
color: "green",
|
|
66
|
+
tooltip: "Campaign has been shipped",
|
|
67
67
|
},
|
|
68
68
|
delivered: {
|
|
69
|
-
value:
|
|
70
|
-
label:
|
|
71
|
-
icon:
|
|
72
|
-
color:
|
|
73
|
-
tooltip:
|
|
69
|
+
value: "delivered",
|
|
70
|
+
label: "Delivered",
|
|
71
|
+
icon: "check",
|
|
72
|
+
color: "green",
|
|
73
|
+
tooltip: "Campaign has been delivered",
|
|
74
74
|
},
|
|
75
75
|
}
|
|
76
76
|
end
|
|
@@ -80,7 +80,7 @@ end
|
|
|
80
80
|
|
|
81
81
|
```ruby
|
|
82
82
|
class Task < ApplicationRecord
|
|
83
|
-
enum_field :priority, [
|
|
83
|
+
enum_field :priority, ["low", "medium", "high"]
|
|
84
84
|
end
|
|
85
85
|
```
|
|
86
86
|
|
|
@@ -89,16 +89,16 @@ This automatically generates:
|
|
|
89
89
|
```ruby
|
|
90
90
|
{
|
|
91
91
|
low: {
|
|
92
|
-
value:
|
|
93
|
-
label:
|
|
92
|
+
value: "low",
|
|
93
|
+
label: "low",
|
|
94
94
|
},
|
|
95
95
|
medium: {
|
|
96
|
-
value:
|
|
97
|
-
label:
|
|
96
|
+
value: "medium",
|
|
97
|
+
label: "medium",
|
|
98
98
|
},
|
|
99
99
|
high: {
|
|
100
|
-
value:
|
|
101
|
-
label:
|
|
100
|
+
value: "high",
|
|
101
|
+
label: "high",
|
|
102
102
|
},
|
|
103
103
|
}
|
|
104
104
|
```
|
|
@@ -121,22 +121,22 @@ For an enum field defined as:
|
|
|
121
121
|
class Campaign < ApplicationRecord
|
|
122
122
|
enum_field :stage, {
|
|
123
123
|
draft: {
|
|
124
|
-
value:
|
|
125
|
-
label:
|
|
126
|
-
icon:
|
|
127
|
-
color:
|
|
124
|
+
value: "draft",
|
|
125
|
+
label: "Draft",
|
|
126
|
+
icon: "file",
|
|
127
|
+
color: "gray",
|
|
128
128
|
},
|
|
129
129
|
scheduled: {
|
|
130
|
-
value:
|
|
131
|
-
label:
|
|
132
|
-
icon:
|
|
133
|
-
color:
|
|
130
|
+
value: "scheduled",
|
|
131
|
+
label: "Scheduled",
|
|
132
|
+
icon: "calendar",
|
|
133
|
+
color: "blue",
|
|
134
134
|
},
|
|
135
135
|
completed: {
|
|
136
|
-
value:
|
|
137
|
-
label:
|
|
138
|
-
icon:
|
|
139
|
-
color:
|
|
136
|
+
value: "completed",
|
|
137
|
+
label: "Completed",
|
|
138
|
+
icon: "check",
|
|
139
|
+
color: "green",
|
|
140
140
|
},
|
|
141
141
|
}
|
|
142
142
|
end
|
|
@@ -152,15 +152,15 @@ Campaign.stages
|
|
|
152
152
|
Campaign.stages_count # 3
|
|
153
153
|
|
|
154
154
|
# Returns the values of the definitions
|
|
155
|
-
Campaign.stages_values # [
|
|
155
|
+
Campaign.stages_values # ["draft", "scheduled", "completed"]
|
|
156
156
|
|
|
157
157
|
# Returns the options for form helpers
|
|
158
|
-
Campaign.stages_options # [[
|
|
158
|
+
Campaign.stages_options # [["Draft", "draft"], ["Scheduled", "scheduled"], ["Completed", "completed"]]
|
|
159
159
|
|
|
160
160
|
# Returns the value for a specific key
|
|
161
|
-
Campaign.draft_stage_value #
|
|
162
|
-
Campaign.scheduled_stage_value #
|
|
163
|
-
Campaign.completed_stage_value #
|
|
161
|
+
Campaign.draft_stage_value # "draft"
|
|
162
|
+
Campaign.scheduled_stage_value # "scheduled"
|
|
163
|
+
Campaign.completed_stage_value # "completed"
|
|
164
164
|
```
|
|
165
165
|
|
|
166
166
|
#### Instance Getter/Setter
|
|
@@ -168,13 +168,13 @@ Campaign.completed_stage_value # 'completed'
|
|
|
168
168
|
If the accessor name differs from the column name, getter and setter methods are defined for the accessor.
|
|
169
169
|
|
|
170
170
|
```ruby
|
|
171
|
-
campaign.stage #
|
|
172
|
-
campaign.stage =
|
|
173
|
-
campaign.stage #
|
|
171
|
+
campaign.stage # "draft"
|
|
172
|
+
campaign.stage = "scheduled"
|
|
173
|
+
campaign.stage # "scheduled"
|
|
174
174
|
```
|
|
175
175
|
|
|
176
176
|
- `campaign.stage` - Get the current stage value
|
|
177
|
-
- `campaign.stage =
|
|
177
|
+
- `campaign.stage = "scheduled"` - Set the stage value
|
|
178
178
|
|
|
179
179
|
#### Metadata Methods
|
|
180
180
|
|
|
@@ -188,25 +188,25 @@ Any additional properties you define (like `icon`, `color`, `tooltip`, etc.) wil
|
|
|
188
188
|
```ruby
|
|
189
189
|
# Returns the full metadata hash for current value
|
|
190
190
|
campaign.stage_metadata
|
|
191
|
-
# => { value:
|
|
191
|
+
# => { value: "draft", label: "Draft", icon: "file", color: "gray" }
|
|
192
192
|
|
|
193
193
|
# Access individual properties
|
|
194
|
-
campaign.stage_value #
|
|
195
|
-
campaign.stage_label #
|
|
196
|
-
campaign.stage_icon #
|
|
197
|
-
campaign.stage_color #
|
|
194
|
+
campaign.stage_value # "draft"
|
|
195
|
+
campaign.stage_label # "Draft"
|
|
196
|
+
campaign.stage_icon # "file"
|
|
197
|
+
campaign.stage_color # "gray"
|
|
198
198
|
```
|
|
199
199
|
|
|
200
200
|
#### Inquiry Methods
|
|
201
201
|
|
|
202
202
|
```ruby
|
|
203
|
-
# Returns true if the current value is
|
|
203
|
+
# Returns true if the current value is "draft"
|
|
204
204
|
campaign.draft_stage?
|
|
205
205
|
|
|
206
|
-
# Returns true if the current value is
|
|
206
|
+
# Returns true if the current value is "scheduled"
|
|
207
207
|
campaign.scheduled_stage?
|
|
208
208
|
|
|
209
|
-
# Returns true if the current value is
|
|
209
|
+
# Returns true if the current value is "completed"
|
|
210
210
|
campaign.completed_stage?
|
|
211
211
|
```
|
|
212
212
|
|
|
@@ -235,14 +235,14 @@ You can add any custom properties to your definitions, and the gem will automati
|
|
|
235
235
|
class Ticket < ApplicationRecord
|
|
236
236
|
enum_field :priority, {
|
|
237
237
|
low: {
|
|
238
|
-
value:
|
|
239
|
-
label:
|
|
238
|
+
value: "low",
|
|
239
|
+
label: "Low Priority",
|
|
240
240
|
sla_hours: 72,
|
|
241
241
|
notify_manager: false,
|
|
242
242
|
},
|
|
243
243
|
high: {
|
|
244
|
-
value:
|
|
245
|
-
label:
|
|
244
|
+
value: "high",
|
|
245
|
+
label: "High Priority",
|
|
246
246
|
sla_hours: 4,
|
|
247
247
|
notify_manager: true,
|
|
248
248
|
},
|
|
@@ -11,7 +11,7 @@ module EnumFields
|
|
|
11
11
|
|
|
12
12
|
def initialize(data)
|
|
13
13
|
@data = build(data)
|
|
14
|
-
raise InvalidDefinitionsError,
|
|
14
|
+
raise InvalidDefinitionsError, "Definitions must be a Hash or Array" unless valid_hash?(@data)
|
|
15
15
|
|
|
16
16
|
@properties = Set.new(STANDARD_PROPERTIES)
|
|
17
17
|
@properties.merge(@data.flat_map { |_, metadata| metadata.keys })
|
|
@@ -15,6 +15,7 @@ module EnumFields
|
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
def define!
|
|
18
|
+
register!
|
|
18
19
|
store_definition!
|
|
19
20
|
define_class_methods!
|
|
20
21
|
define_class_value_accessors!
|
|
@@ -29,6 +30,14 @@ module EnumFields
|
|
|
29
30
|
|
|
30
31
|
private
|
|
31
32
|
|
|
33
|
+
def register!
|
|
34
|
+
EnumFields.register(
|
|
35
|
+
model_class: @model_class,
|
|
36
|
+
accessor: @accessor,
|
|
37
|
+
definition: @definition.data
|
|
38
|
+
)
|
|
39
|
+
end
|
|
40
|
+
|
|
32
41
|
def store_definition!
|
|
33
42
|
@model_class.enum_fields[@accessor] = @definition.data
|
|
34
43
|
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module EnumFields
|
|
4
|
+
class Registry < SimpleDelegator
|
|
5
|
+
def initialize
|
|
6
|
+
@store = {}.with_indifferent_access
|
|
7
|
+
super(@store)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def register(model_class:, accessor:, definition:)
|
|
11
|
+
key = model_class.name&.underscore || model_class.object_id.to_s
|
|
12
|
+
@store[key] ||= {}.with_indifferent_access
|
|
13
|
+
@store[key][accessor] = definition
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
data/lib/enum_fields/version.rb
CHANGED
data/lib/enum_fields.rb
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
4
|
-
require
|
|
5
|
-
require
|
|
6
|
-
require
|
|
7
|
-
require
|
|
8
|
-
require
|
|
9
|
-
require
|
|
10
|
-
|
|
11
|
-
require_relative
|
|
12
|
-
require_relative
|
|
13
|
-
require_relative
|
|
3
|
+
require "delegate"
|
|
4
|
+
require "active_support/concern"
|
|
5
|
+
require "active_support/dependencies/autoload"
|
|
6
|
+
require "active_support/core_ext/hash/indifferent_access"
|
|
7
|
+
require "active_support/core_ext/array/extract_options"
|
|
8
|
+
require "active_support/inflector"
|
|
9
|
+
require "active_record"
|
|
10
|
+
|
|
11
|
+
require_relative "enum_fields/errors"
|
|
12
|
+
require_relative "enum_fields/version"
|
|
13
|
+
require_relative "enum_fields/base"
|
|
14
14
|
|
|
15
15
|
module EnumFields
|
|
16
16
|
extend ActiveSupport::Concern
|
|
@@ -18,6 +18,19 @@ module EnumFields
|
|
|
18
18
|
|
|
19
19
|
autoload :Definition
|
|
20
20
|
autoload :EnumField
|
|
21
|
+
autoload :Registry
|
|
22
|
+
|
|
23
|
+
def self.registry
|
|
24
|
+
@registry ||= Registry.new
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def self.register(...)
|
|
28
|
+
registry.register(...)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def self.clear_registry!
|
|
32
|
+
@registry = nil
|
|
33
|
+
end
|
|
21
34
|
|
|
22
35
|
include Base
|
|
23
36
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: enum_fields
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Kinnell Shah
|
|
@@ -68,6 +68,7 @@ files:
|
|
|
68
68
|
- lib/enum_fields/definition.rb
|
|
69
69
|
- lib/enum_fields/enum_field.rb
|
|
70
70
|
- lib/enum_fields/errors.rb
|
|
71
|
+
- lib/enum_fields/registry.rb
|
|
71
72
|
- lib/enum_fields/version.rb
|
|
72
73
|
homepage: https://github.com/kinnell/enum_fields
|
|
73
74
|
licenses:
|