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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4407bd54833c58ac81c23c2067b06e49fe7eb055978ee9132e2f590784db3bb6
4
- data.tar.gz: 6aa6c654c9303f5e3852adb86f25d45d375ff897570164cd42dc9f98c3f82485
3
+ metadata.gz: 5cda13c8af65bfa092be174ccfff8f703623bd4572424961ffc439573d663950
4
+ data.tar.gz: 42e6ca5cc0d2fa7dcd07e863f45c113513bb0facbe8fc418e2ed67b64eb89233
5
5
  SHA512:
6
- metadata.gz: 169b80283833e53040ba83c4dd67061c6f377544334c5f32fd941a6327e80995a4e0e419589ccbd65aea3c28e2638f911b0df12fa3306b6cea2a9a02d28dc10e
7
- data.tar.gz: 89783ddc98bce0c488b3b39966e51298c9179fed58401560fac5fc40eb5ab2934a5be7f7c3eeda6e33887f3f7262fcf86ba30408573aaa4115f90f63da5fafcf
6
+ metadata.gz: 230aa3583beb46aa8c2ae6e39c07f6306a3b11bb0aff1501674094beebef6bddf3798cda7e9857f1ad6c5fb210eae0f6c3ad19425e3f512f01d636524e7f767f
7
+ data.tar.gz: 73c066163a4afba2bba0a2d1ae28fa21087fd1436a5c6141c8ba9519acfe2e6c4257a4dead09fc606f29c212277f3dd64fdfa48c30ce169b09d29ddd06f4cccb
data/.gitignore CHANGED
@@ -10,3 +10,4 @@
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
12
  .idea
13
+ just_enum-*.gem
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- just_enum (0.1.0)
4
+ just_enum (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -52,4 +52,4 @@ DEPENDENCIES
52
52
  rubocop (~> 1.7)
53
53
 
54
54
  BUNDLED WITH
55
- 2.2.22
55
+ 2.2.32
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # JustEnum
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/just_enum`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- TODO: Write usage instructions here
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/[USERNAME]/just_enum.
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://grubykodzi.pl"
13
+ spec.homepage = "https://github.com/rafpiek/just_enum"
14
14
  spec.required_ruby_version = ">= 2.4.0"
15
15
 
16
16
 
@@ -7,6 +7,7 @@ module JustEnum
7
7
  hash[i] = i.to_s
8
8
  end
9
9
  end
10
+ define_singleton_method(:mirrored?) { mirror }
10
11
  define_singleton_method(:options) { options }
11
12
  case options
12
13
  when Array
@@ -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
- define_method("str_#{field_key.to_s}") { enum_class.options[field_value.to_sym] }
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JustEnum
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
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.0
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: 2021-12-24 00:00:00.000000000 Z
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://grubykodzi.pl
36
+ homepage: https://github.com/rafpiek/just_enum
37
37
  licenses: []
38
38
  metadata:
39
- homepage_uri: https://grubykodzi.pl
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: