just_enum 0.1.0 → 0.1.1
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/.gitignore +1 -0
- data/Gemfile.lock +2 -2
- data/README.md +231 -5
- data/just_enum.gemspec +1 -1
- data/lib/just_enum/base.rb +1 -0
- data/lib/just_enum/enum.rb +5 -1
- data/lib/just_enum/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5cda13c8af65bfa092be174ccfff8f703623bd4572424961ffc439573d663950
|
4
|
+
data.tar.gz: 42e6ca5cc0d2fa7dcd07e863f45c113513bb0facbe8fc418e2ed67b64eb89233
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 230aa3583beb46aa8c2ae6e39c07f6306a3b11bb0aff1501674094beebef6bddf3798cda7e9857f1ad6c5fb210eae0f6c3ad19425e3f512f01d636524e7f767f
|
7
|
+
data.tar.gz: 73c066163a4afba2bba0a2d1ae28fa21087fd1436a5c6141c8ba9519acfe2e6c4257a4dead09fc606f29c212277f3dd64fdfa48c30ce169b09d29ddd06f4cccb
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# JustEnum
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
This gem has been created to mimic Typescript like enums and adding additional features to the POROs.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -22,7 +20,235 @@ Or install it yourself as:
|
|
22
20
|
|
23
21
|
## Usage
|
24
22
|
|
25
|
-
|
23
|
+
### Defining enums
|
24
|
+
|
25
|
+
After installing gem extend the class you want to make the enum for storing properties with `JustEnum::Base`.
|
26
|
+
|
27
|
+
For example:
|
28
|
+
```ruby
|
29
|
+
class ButtonType < JustEnum::Base
|
30
|
+
enum %i[primary secondary]
|
31
|
+
end
|
32
|
+
|
33
|
+
class Color < JustEnum::Base
|
34
|
+
enum %i[success danger]
|
35
|
+
end
|
36
|
+
|
37
|
+
class Labels < JustEnum::Base
|
38
|
+
enum({save: "Zapisz", cancel: "Anuluj"})
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
So that you can have enum classes as Ruby singletons. You can use:
|
43
|
+
```ruby
|
44
|
+
ButtonType.primary #=> 0
|
45
|
+
ButtonType.secondary #=> 1
|
46
|
+
Color.success #=> 0
|
47
|
+
Color.danger #=> 1
|
48
|
+
Labels.save #=> "Zapisz"
|
49
|
+
Labels.cancel #=> "Anuluj"
|
50
|
+
```
|
51
|
+
|
52
|
+
You can list all possible options in the enum by using `.options` static method
|
53
|
+
```ruby
|
54
|
+
ButtonType.options #=> [:primary, :secondary]
|
55
|
+
Color.options #=> {:success=>"success", :danger=>"danger"}
|
56
|
+
Labels.options #=> {:save=>"Zapisz", :cancel=>"Anuluj"}
|
57
|
+
```
|
58
|
+
|
59
|
+
### How enums are defined in JustEnum
|
60
|
+
#### Indexed enum
|
61
|
+
```ruby
|
62
|
+
class ButtonType < JustEnum::Base
|
63
|
+
enum %i[primary secondary]
|
64
|
+
end
|
65
|
+
```
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
enum %i[primary secondary]
|
69
|
+
|
70
|
+
# is the same as:
|
71
|
+
|
72
|
+
{ primary: 0, secondary: 1 }
|
73
|
+
```
|
74
|
+
|
75
|
+
#### Enum with custom values
|
76
|
+
You can define custom values for enums
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
class Labels < JustEnum::Enum
|
80
|
+
enum({ save: "Zapisz", cancel: "Anuluj" })
|
81
|
+
end
|
82
|
+
```
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
enum({ save: "Zapisz", cancel: "Anuluj" })
|
86
|
+
|
87
|
+
# is the same as:
|
88
|
+
|
89
|
+
{ save: "Zapisz", cancel: "Anuluj" }
|
90
|
+
```
|
91
|
+
|
92
|
+
#### Mirrored enum keys
|
93
|
+
You can mirror your keys as the values in enum.
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
class Color < JustEnum::Base
|
97
|
+
enum %i[success danger], mirror: true
|
98
|
+
end
|
99
|
+
```
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
enum %i[success danger], mirror: true
|
103
|
+
|
104
|
+
# is the same as:
|
105
|
+
|
106
|
+
{ success: "success", danger: "danger" }
|
107
|
+
```
|
108
|
+
|
109
|
+
### Use defined enums as values in your PORO classes.
|
110
|
+
|
111
|
+
Than You can use the enum to define values for fields in your PORO classes. Do it by extending your
|
112
|
+
class with `JustEnum::Enum`
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
class ButtonPrimary
|
116
|
+
extend JustEnum::Enum
|
117
|
+
enumerate :type, ButtonType, ButtonType.primary
|
118
|
+
enumerate :color, Color, Color.success
|
119
|
+
enumerate :label, Labels, Labels.save
|
120
|
+
end
|
121
|
+
```
|
122
|
+
|
123
|
+
You can also omit the `extend` directive and use it in traditional way in constructor or `attr_writer`.
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
class ButtonPrimary
|
127
|
+
def initialize(type, color, label)
|
128
|
+
@type = ButtonType.primary
|
129
|
+
@color = Color.success
|
130
|
+
@label = Labels.save
|
131
|
+
end
|
132
|
+
end
|
133
|
+
```
|
134
|
+
|
135
|
+
### Extend your class with `JustEnum::Enum`
|
136
|
+
When extending your PORO with `JustEnum::Enum`:
|
137
|
+
|
138
|
+
a) you can access the `enumarate` method which defines instance fields with values provided by enums
|
139
|
+
```ruby
|
140
|
+
class ButtonPrimary
|
141
|
+
extend JustEnum::Enum
|
142
|
+
enumerate :type, ButtonType, ButtonType.primary
|
143
|
+
enumerate :color, Color, Color.success
|
144
|
+
enumerate :label, Labels, Labels.save
|
145
|
+
end
|
146
|
+
```
|
147
|
+
|
148
|
+
b) you have public underscored accessors for defined fields which will return the Enum definition as
|
149
|
+
complex type
|
150
|
+
|
151
|
+
```ruby
|
152
|
+
button = ButtonPrimary.new
|
153
|
+
|
154
|
+
button._type #=> ButtonType.primary
|
155
|
+
button._color #=> Color.success
|
156
|
+
button._label #=> Labels.save
|
157
|
+
```
|
158
|
+
|
159
|
+
c) you have the string representation of defined enumerated fields with `str_` prefix
|
160
|
+
|
161
|
+
```ruby
|
162
|
+
button = ButtonPrimary.new
|
163
|
+
|
164
|
+
button.str_type #=> 'primary'
|
165
|
+
button.str_color #=> 'success'
|
166
|
+
button.str_label #=> 'Zapisz'
|
167
|
+
```
|
168
|
+
|
169
|
+
d) you have boolean methods allowing to check which enum value has been defined or in other words:
|
170
|
+
of what enum is the field in your PORO class
|
171
|
+
|
172
|
+
```ruby
|
173
|
+
button = ButtonPrimary.new
|
174
|
+
|
175
|
+
button.button_type_primary? #=> true
|
176
|
+
button.button_type_secondary? #=> false
|
177
|
+
button.color_success? #=> true
|
178
|
+
button.color_danger? #=> false
|
179
|
+
button.label_save? #=> true
|
180
|
+
button.label_cancel? #=> false
|
181
|
+
```
|
182
|
+
|
183
|
+
### Usage with ViewComponent
|
184
|
+
It's very convenient to use JustEnum with [ViewComponent gem from Github](https://github.com/github/view_component)
|
185
|
+
You can define your enum classes:
|
186
|
+
```ruby
|
187
|
+
module Enums
|
188
|
+
class ButtonType < JustEnum::Base
|
189
|
+
enum %i[primary secondary default success]
|
190
|
+
end
|
191
|
+
end
|
192
|
+
```
|
193
|
+
|
194
|
+
Then extend your component classes with `JustEnum::Enum`
|
195
|
+
```ruby
|
196
|
+
# frozen_string_literal: true
|
197
|
+
|
198
|
+
class ButtonPrimary < ViewComponent::Base
|
199
|
+
extend JustEnum::Enum
|
200
|
+
enumerate :type, ButtonType, ButtonType.primary
|
201
|
+
|
202
|
+
attr_reader :label
|
203
|
+
|
204
|
+
def initialize(label:)
|
205
|
+
@label = label
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
<%= render ButtonPrimary.new(label: "Start search") %>
|
210
|
+
```
|
211
|
+
|
212
|
+
Or you can go one step further and encapsulate enums in static methods
|
213
|
+
|
214
|
+
```ruby
|
215
|
+
# frozen_string_literal: true
|
216
|
+
|
217
|
+
class Button < ViewComponent::Base
|
218
|
+
include Enums::ButtonType::Enum
|
219
|
+
|
220
|
+
class << self
|
221
|
+
def primary(**args)
|
222
|
+
args[:type_class] = %w[border-transparent text-white bg-indigo-600 hover:bg-indigo-700]
|
223
|
+
new(**args, type: Enums::ButtonType.primary)
|
224
|
+
end
|
225
|
+
|
226
|
+
def secondary(**args)
|
227
|
+
new(**args, type: Enums::ButtonType.secondary)
|
228
|
+
end
|
229
|
+
|
230
|
+
def default(**args)
|
231
|
+
args[:type_class] = %w[bg-white border-gray-300 text-gray-700 hover:bg-gray-5]
|
232
|
+
new(**args, type: Enums::ButtonType.default)
|
233
|
+
end
|
234
|
+
|
235
|
+
def success(**args)
|
236
|
+
new(**args, type: Enums::ButtonType.success)
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
attr_reader :label,
|
241
|
+
|
242
|
+
def initialize(label:, type:)
|
243
|
+
@type = args[:type] || Enums::ButtonType.default
|
244
|
+
@label = args[:label]
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
<%= render Button.default(label: "Cancel") %>
|
249
|
+
<%= render Button.primary(label: "Start search") %>
|
250
|
+
|
251
|
+
```
|
26
252
|
|
27
253
|
## Development
|
28
254
|
|
@@ -32,4 +258,4 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
32
258
|
|
33
259
|
## Contributing
|
34
260
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
261
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/rafpiek/just_enum.
|
data/just_enum.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
|
11
11
|
spec.summary = "Simple enum module extending Ruby with TypeScript like enums"
|
12
12
|
spec.description = "Module extends PORO classes in enums and generate helpers for other POROS using those enums"
|
13
|
-
spec.homepage = "https://
|
13
|
+
spec.homepage = "https://github.com/rafpiek/just_enum"
|
14
14
|
spec.required_ruby_version = ">= 2.4.0"
|
15
15
|
|
16
16
|
|
data/lib/just_enum/base.rb
CHANGED
data/lib/just_enum/enum.rb
CHANGED
@@ -3,7 +3,11 @@ module JustEnum::Enum
|
|
3
3
|
def enumerate(field_key, enum_class, field_value)
|
4
4
|
has_hash_options = enum_class.options.is_a? Hash
|
5
5
|
if has_hash_options
|
6
|
-
|
6
|
+
if enum_class.mirrored?
|
7
|
+
define_method("str_#{field_key.to_s}") { enum_class.options[field_value.to_sym] }
|
8
|
+
else
|
9
|
+
define_method("str_#{field_key.to_s}") { enum_class.options.find { |k, v| v.to_s == field_value.to_s }[1] }
|
10
|
+
end
|
7
11
|
enum_class.options.each_pair do |key, value|
|
8
12
|
define_method("#{enum_class.name.snakecase}_#{key.to_s}?") { field_value == value }
|
9
13
|
end
|
data/lib/just_enum/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: just_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rafal Piekara
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-01-05 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Module extends PORO classes in enums and generate helpers for other POROS
|
14
14
|
using those enums
|
@@ -33,10 +33,10 @@ files:
|
|
33
33
|
- lib/just_enum/enum.rb
|
34
34
|
- lib/just_enum/string.rb
|
35
35
|
- lib/just_enum/version.rb
|
36
|
-
homepage: https://
|
36
|
+
homepage: https://github.com/rafpiek/just_enum
|
37
37
|
licenses: []
|
38
38
|
metadata:
|
39
|
-
homepage_uri: https://
|
39
|
+
homepage_uri: https://github.com/rafpiek/just_enum
|
40
40
|
source_code_uri: https://github.com/rafpiek/just_enum
|
41
41
|
changelog_uri: https://github.com/rafpiek/just_enum
|
42
42
|
post_install_message:
|