enum_ext 0.5.3 → 0.8.0
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/CHANGELOG.md +19 -0
- data/Dockerfile +22 -0
- data/Dockerfile_rails_7 +25 -0
- data/{Gemfile → Gemfile_rails_6} +1 -0
- data/{Gemfile.lock → Gemfile_rails_6.lock} +2 -2
- data/Gemfile_rails_7 +6 -0
- data/Gemfile_rails_7.lock +102 -0
- data/README.md +106 -25
- data/docker-compose.yml +24 -0
- data/img.png +0 -0
- data/img_1.png +0 -0
- data/img_2.png +0 -0
- data/img_3.png +0 -0
- data/lib/enum_ext/annotated.rb +172 -0
- data/lib/enum_ext/basic_helpers.rb +79 -0
- data/lib/enum_ext/enum_wrapper.rb +78 -0
- data/lib/enum_ext/humanize_helpers.rb +161 -0
- data/lib/enum_ext/superset_helpers.rb +77 -0
- data/lib/enum_ext/version.rb +1 -1
- data/lib/enum_ext.rb +43 -358
- metadata +19 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8d99346b1ae5f9458733025608cad6e8a1ab427091ce44d10a544e880722633c
|
4
|
+
data.tar.gz: 6a1943ddedaa37022bdfe421dbc4c5d063c63149a96aaa0f05cdab35f423de6b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ce540db8aca9002c6a46a5ae5eb7a45f7a296d4fd183493a8796e3b9dd007ebb04bc0793b1162b82e177de5b59ff06e8e553248a2aa4c725b17d3570d682444f
|
7
|
+
data.tar.gz: 7db85461bd47c03b3169259f699a29e7a1855fd791d53fb4fe84995b812739fe4968b8184665dfd04230b71e2d78152dc8868f78f89452f37d6b20ed00c77ecc
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,25 @@
|
|
1
|
+
# 0.8.0
|
2
|
+
* Methods annotations added:
|
3
|
+
* Full descriptions: Class.enum.describe(short=true) / Class.enum.describe_short / Class.enum.describe_long / Class.enum.describe_basic
|
4
|
+
* Per-methods: Class.enum.describe_enum_i, describe_mass_assign_enum, describe_multi_enum_scopes e.t.c.
|
5
|
+
* method Class.enum.supersets_raw added, it will return supersets decompositions to basic enum
|
6
|
+
* Class.enum.all method now returns basic enum_values + supersets_raw decomposition to basic enums ( previously was a supersets i.e. high level definition )
|
7
|
+
|
8
|
+
# 0.7.0
|
9
|
+
* rails 7 syntax support added
|
10
|
+
* massive test process refactoring docker with both rails 7 and earlier support
|
11
|
+
* simple translate_enum allowed via `ext: [:translate_enum]`
|
12
|
+
|
13
|
+
# 0.6.0 (BREAKING CHANGES)
|
14
|
+
* fixed issue with non array superset
|
15
|
+
* standard helpers are now private, you should use enum_ext / enum , ext: [] approach to the definitions
|
16
|
+
* class.superset_statuses moved to enum_obj -> class.statuses.superset_statuses
|
17
|
+
* massive ReadMe refactoring
|
18
|
+
|
1
19
|
# 0.5.3
|
2
20
|
* refactored tests
|
3
21
|
* multi_enum_scopes will be chainable with empty params (i.e. will not change the scope)
|
22
|
+
* some development dependencies were added
|
4
23
|
|
5
24
|
# 0.5.2
|
6
25
|
* removed unused require from the upcoming version
|
data/Dockerfile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
FROM ruby:3.0.3-bullseye
|
2
|
+
|
3
|
+
WORKDIR /app
|
4
|
+
#RUN apt-get update && apt-get -y install lsb-release
|
5
|
+
##
|
6
|
+
#RUN wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - && \
|
7
|
+
# sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' && \
|
8
|
+
# apt-get update && apt-get -y install postgresql postgresql-client-12
|
9
|
+
#
|
10
|
+
#RUN sh -c 'echo "local all all trust" > /etc/postgresql/14/main/pg_hba.conf' && \
|
11
|
+
# service postgresql start && \
|
12
|
+
# psql -U postgres -c 'CREATE DATABASE "niceql-test"'
|
13
|
+
|
14
|
+
RUN gem install bundler
|
15
|
+
|
16
|
+
COPY lib/enum_ext/version.rb /app/lib/enum_ext/version.rb
|
17
|
+
COPY enum_ext.gemspec /app/
|
18
|
+
COPY Gemfile_rails_6 /app/Gemfile
|
19
|
+
COPY Gemfile_rails_6.lock /app/Gemfile.lock
|
20
|
+
COPY Rakefile /app/
|
21
|
+
#
|
22
|
+
RUN bundle install
|
data/Dockerfile_rails_7
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
FROM ruby:3.0.3-bullseye
|
2
|
+
|
3
|
+
WORKDIR /app
|
4
|
+
#RUN apt-get update && apt-get -y install lsb-release
|
5
|
+
##
|
6
|
+
#RUN wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - && \
|
7
|
+
# sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' && \
|
8
|
+
# apt-get update && apt-get -y install postgresql postgresql-client-12
|
9
|
+
#
|
10
|
+
#RUN sh -c 'echo "local all all trust" > /etc/postgresql/14/main/pg_hba.conf' && \
|
11
|
+
# service postgresql start && \
|
12
|
+
# psql -U postgres -c 'CREATE DATABASE "niceql-test"'
|
13
|
+
|
14
|
+
RUN gem install bundler
|
15
|
+
|
16
|
+
|
17
|
+
RUN gem install bundler
|
18
|
+
|
19
|
+
COPY lib/enum_ext/version.rb /app/lib/enum_ext/version.rb
|
20
|
+
COPY enum_ext.gemspec /app/
|
21
|
+
COPY Gemfile_rails_7 /app/Gemfile
|
22
|
+
COPY Gemfile_rails_7.lock /app/Gemfile.lock
|
23
|
+
COPY Rakefile /app/
|
24
|
+
|
25
|
+
RUN bundle install
|
data/{Gemfile → Gemfile_rails_6}
RENAMED
data/Gemfile_rails_7
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
enum_ext (0.8.0)
|
5
|
+
activerecord (>= 5.2.4.3)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
actionpack (7.0.6)
|
11
|
+
actionview (= 7.0.6)
|
12
|
+
activesupport (= 7.0.6)
|
13
|
+
rack (~> 2.0, >= 2.2.4)
|
14
|
+
rack-test (>= 0.6.3)
|
15
|
+
rails-dom-testing (~> 2.0)
|
16
|
+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
17
|
+
actionview (7.0.6)
|
18
|
+
activesupport (= 7.0.6)
|
19
|
+
builder (~> 3.1)
|
20
|
+
erubi (~> 1.4)
|
21
|
+
rails-dom-testing (~> 2.0)
|
22
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
23
|
+
activemodel (7.0.6)
|
24
|
+
activesupport (= 7.0.6)
|
25
|
+
activerecord (7.0.6)
|
26
|
+
activemodel (= 7.0.6)
|
27
|
+
activesupport (= 7.0.6)
|
28
|
+
activesupport (7.0.6)
|
29
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
30
|
+
i18n (>= 1.6, < 2)
|
31
|
+
minitest (>= 5.1)
|
32
|
+
tzinfo (~> 2.0)
|
33
|
+
amazing_print (1.5.0)
|
34
|
+
builder (3.2.4)
|
35
|
+
byebug (11.1.3)
|
36
|
+
concurrent-ruby (1.2.2)
|
37
|
+
crass (1.0.6)
|
38
|
+
erubi (1.12.0)
|
39
|
+
i18n (1.14.1)
|
40
|
+
concurrent-ruby (~> 1.0)
|
41
|
+
loofah (2.21.3)
|
42
|
+
crass (~> 1.0.2)
|
43
|
+
nokogiri (>= 1.12.0)
|
44
|
+
method_source (1.0.0)
|
45
|
+
minitest (5.18.1)
|
46
|
+
niceql (0.6.1)
|
47
|
+
nokogiri (1.15.3-x86_64-darwin)
|
48
|
+
racc (~> 1.4)
|
49
|
+
nokogiri (1.15.3-x86_64-linux)
|
50
|
+
racc (~> 1.4)
|
51
|
+
racc (1.7.1)
|
52
|
+
rack (2.2.7)
|
53
|
+
rack-test (2.1.0)
|
54
|
+
rack (>= 1.3)
|
55
|
+
rails-dom-testing (2.1.1)
|
56
|
+
activesupport (>= 5.0.0)
|
57
|
+
minitest
|
58
|
+
nokogiri (>= 1.6)
|
59
|
+
rails-html-sanitizer (1.6.0)
|
60
|
+
loofah (~> 2.21)
|
61
|
+
nokogiri (~> 1.14)
|
62
|
+
rails-i18n (7.0.7)
|
63
|
+
i18n (>= 0.7, < 2)
|
64
|
+
railties (>= 6.0.0, < 8)
|
65
|
+
rails_sql_prettifier (7.0.4)
|
66
|
+
activerecord (>= 7)
|
67
|
+
niceql (~> 0.6)
|
68
|
+
railties (7.0.6)
|
69
|
+
actionpack (= 7.0.6)
|
70
|
+
activesupport (= 7.0.6)
|
71
|
+
method_source
|
72
|
+
rake (>= 12.2)
|
73
|
+
thor (~> 1.0)
|
74
|
+
zeitwerk (~> 2.5)
|
75
|
+
rake (13.0.6)
|
76
|
+
sqlite3 (1.6.3-x86_64-darwin)
|
77
|
+
sqlite3 (1.6.3-x86_64-linux)
|
78
|
+
stubberry (0.3.0)
|
79
|
+
thor (1.2.2)
|
80
|
+
tzinfo (2.0.6)
|
81
|
+
concurrent-ruby (~> 1.0)
|
82
|
+
zeitwerk (2.6.8)
|
83
|
+
|
84
|
+
PLATFORMS
|
85
|
+
x86_64-darwin-20
|
86
|
+
x86_64-linux
|
87
|
+
|
88
|
+
DEPENDENCIES
|
89
|
+
activerecord (>= 7)
|
90
|
+
amazing_print
|
91
|
+
bundler (>= 1.11)
|
92
|
+
byebug
|
93
|
+
enum_ext!
|
94
|
+
minitest
|
95
|
+
rails-i18n (>= 4)
|
96
|
+
rails_sql_prettifier
|
97
|
+
rake (>= 10.0)
|
98
|
+
sqlite3
|
99
|
+
stubberry
|
100
|
+
|
101
|
+
BUNDLED WITH
|
102
|
+
2.4.17
|
data/README.md
CHANGED
@@ -7,7 +7,7 @@ EnumExt extends rails enum with localization/translation and it's helpers, mass-
|
|
7
7
|
Add this line to your application's Gemfile:
|
8
8
|
|
9
9
|
```ruby
|
10
|
-
gem 'enum_ext', '~> 0.
|
10
|
+
gem 'enum_ext', '~> 0.7'
|
11
11
|
```
|
12
12
|
|
13
13
|
And then execute:
|
@@ -49,7 +49,41 @@ Or install it yourself as:
|
|
49
49
|
end
|
50
50
|
```
|
51
51
|
|
52
|
-
|
52
|
+
## Inline definitions. Starting 0.5.0!! BREAKING CHANGES!
|
53
|
+
|
54
|
+
Starting version 0.4.6, for concise and clearer code base, you can activate most of the extensions during enum definition, using `ext: [...]` notation.
|
55
|
+
This is a preferable way now, and old helpers are private starting version 0.6!:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
#Instead of three method calls:
|
59
|
+
enum kind: {}
|
60
|
+
enum_i :kind # will raise an private method error starting ver 0.6
|
61
|
+
enum_mass_assign :kind # will raise an private method error starting ver 0.6
|
62
|
+
|
63
|
+
#You should go with ext option instead:
|
64
|
+
enum kinds: {}, ext: [:enum_i, :enum_mass_assign]
|
65
|
+
|
66
|
+
# OR in case of standalone enum definition:
|
67
|
+
enum kinds: {} # somewhere where you can't or don't want reach
|
68
|
+
enum_ext :kinds, [:enum_i, :enum_mass_assign, enum_supersets: {} ]
|
69
|
+
```
|
70
|
+
Rem: enum_ext could be called multiple times and merge later definitions, though I can't imagine why would you split it to multiple calls.
|
71
|
+
|
72
|
+
Rem: The only exceptions for the new syntax are full translation/humanization helpers definitions.
|
73
|
+
Those will not add any clarity to code, and should be used via standalone helpers.
|
74
|
+
But standard translate_enum without definitions still welcome:
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
# GOOD:
|
78
|
+
enum_ext :kinds, [:enum_i, :enum_mass_assign, :translate_enum, enum_supersets: {}]
|
79
|
+
|
80
|
+
# BAD (even if correctly defines internationalization):
|
81
|
+
enum_ext :kinds, [:enum_i, :enum_mass_assign, :translate_enum, enum_supersets: {}] do
|
82
|
+
I18n.t("scope.#{kind}")
|
83
|
+
end
|
84
|
+
```
|
85
|
+
|
86
|
+
## Humanization and localization
|
53
87
|
|
54
88
|
### Humanization (humanize_enum)
|
55
89
|
|
@@ -81,7 +115,7 @@ Or install it yourself as:
|
|
81
115
|
|
82
116
|
```ruby
|
83
117
|
humanize_enum :status do
|
84
|
-
|
118
|
+
I18n.t("scope.#{status}")
|
85
119
|
end
|
86
120
|
```
|
87
121
|
|
@@ -95,7 +129,6 @@ Or install it yourself as:
|
|
95
129
|
```ruby
|
96
130
|
filter :status, as: :select, label: 'Status', collection: Request.t_statuses_options_i
|
97
131
|
```
|
98
|
-
|
99
132
|
|
100
133
|
**Rem:** select options may break when using lambda() or proc with instance method, but will survive with block
|
101
134
|
|
@@ -113,6 +146,7 @@ Or install it yourself as:
|
|
113
146
|
### Translate (translate_enum)
|
114
147
|
|
115
148
|
Enum is translated using scope 'active_record.attributes.class_name_underscore.enum_plural', or the given one:
|
149
|
+
|
116
150
|
```ruby
|
117
151
|
translate_enum :status, 'active_record.request.enum'
|
118
152
|
```
|
@@ -124,7 +158,10 @@ Or it can be done with block either with translate or humanize:
|
|
124
158
|
end
|
125
159
|
```
|
126
160
|
|
161
|
+
## Enum extended functionality
|
162
|
+
|
127
163
|
### Enum to_i shortcut ( enum_i )
|
164
|
+
|
128
165
|
Defines method enum_name_i shortcut for Model.enum_names[elem.enum_name] or enum_name_before_type_cast
|
129
166
|
|
130
167
|
**Ex**
|
@@ -135,22 +172,30 @@ Defines method enum_name_i shortcut for Model.enum_names[elem.enum_name] or enum
|
|
135
172
|
request.paid_i # 10
|
136
173
|
```
|
137
174
|
|
138
|
-
### Enum
|
175
|
+
### Enum SuperSets (enum_supersets)
|
139
176
|
|
140
|
-
**Use-case** whenever you need superset of enums to behave like a enum.
|
177
|
+
**Use-case** whenever you need superset of enums to behave like a super enum.
|
141
178
|
|
142
|
-
You can do this with method **
|
179
|
+
You can do this with method **enum_supersets** it creates:
|
143
180
|
- scopes for subsets,
|
144
181
|
- instance methods with `?`
|
145
|
-
- and some class methods helpers
|
146
182
|
|
147
183
|
For instance:
|
148
184
|
|
149
185
|
```ruby
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
186
|
+
enum status: [:in_cart, :waiting_for_payment, :paid, :packing, :ready_for_shipment, :on_delivery, :delivered],
|
187
|
+
ext: [enum_supersets: {
|
188
|
+
delivery_set: [:ready_for_shipment, :on_delivery], # for shipping department for example
|
189
|
+
in_warehouse: [:packing, :ready_for_shipment], # this scope is just for superposition example below
|
190
|
+
sold: [:paid, :delivery_set, :in_warehouse, :delivered] # also you can define any superposition of already defined supersets or enum values
|
191
|
+
}]
|
192
|
+
|
193
|
+
# supersets will be stored inside enum wrapper object, and can be de-referenced to basic enum values
|
194
|
+
# using wrapper defined methods: "superset_enum_plural", i.e. statuses.sold_statuses -> [:paid, :packing, :ready_for_shipment, :on_delivery, :delivered]
|
195
|
+
# so new supersets could be defined using Array operations against newly defined methods
|
196
|
+
enum_ext :status, enum_supersets: {
|
197
|
+
outside_warehouse: ( statuses.delivery_set_statuses - statuses.in_warehouse_statuses ) #... any other array operations like &, + and so can be used
|
198
|
+
}
|
154
199
|
```
|
155
200
|
|
156
201
|
it will generate:
|
@@ -162,13 +207,17 @@ instance:
|
|
162
207
|
class:
|
163
208
|
- named scopes: delivery_set, in_warehouse
|
164
209
|
- parametrized scopes: with_statuses, without_statuses ( available as a standalone extension now, and will not be included by default in a versionafter 0.5.0)
|
165
|
-
class helpers:
|
166
|
-
- delivery_set_statuses (=[:ready_for_shipment, :on_delivery, :delivered] ), in_warehouse_statuses
|
167
|
-
- delivery_set_statuses_i (= [3,4,5]), in_warehouse_statuses_i (=[3])
|
168
210
|
|
169
|
-
|
170
|
-
|
171
|
-
|
211
|
+
enum methods:
|
212
|
+
- Class.statuses.supersets -- will output superset definition hash
|
213
|
+
- Class.statuses.supersets_raw -- will output superset decompositions to basic enum types hash
|
214
|
+
|
215
|
+
- Class.statuses.delivery_superset (=[:ready_for_shipment, :on_delivery, :delivered] ), in_warehouse_statuses
|
216
|
+
- delivery_set_statuses_i (= [3,4,5]), in_warehouse_statuses_i (=[3])
|
217
|
+
|
218
|
+
translation helpers ( started with t_... ):
|
219
|
+
- Class.statuses.t_delivery_set_options (= [['translation or humanization', :ready_for_shipment] ...] ) for select inputs purposes
|
220
|
+
- Class.statuses.t_delivery_set_options_i (= [['translation or humanization', 3] ...]) same as above but with integer as value ( for example to use in Active admin filters )
|
172
221
|
```
|
173
222
|
|
174
223
|
```ruby
|
@@ -181,10 +230,10 @@ class:
|
|
181
230
|
Request.delivery_set_statuses # >> ["ready_for_shipment", "on_delivery", "delivered"]
|
182
231
|
```
|
183
232
|
Rem:
|
184
|
-
|
233
|
+
supersets creation could be called multiple times defining a superposition of already defined sets ( considering previous example ):
|
185
234
|
|
186
235
|
```ruby
|
187
|
-
|
236
|
+
enum_ext :status, enum_supersets: {
|
188
237
|
outside_wharehouse: ( delivery_set_statuses - in_warehouse_statuses )#... any other array operations like &, + and so can be used
|
189
238
|
}
|
190
239
|
```
|
@@ -192,7 +241,7 @@ ext_enum_sets can be called multiple times defining a superposition of already d
|
|
192
241
|
Rem: you can refer previously defined set as usual kind in the same method call:
|
193
242
|
|
194
243
|
```ruby
|
195
|
-
|
244
|
+
enum_ext :status, enum_supersets: {
|
196
245
|
delivery_set: [:ready_for_shipment, :on_delivery, :delivered],
|
197
246
|
not_in_cart: [:paid, :delivery_set] #
|
198
247
|
}
|
@@ -243,11 +292,43 @@ Rem: you can refer previously defined set as usual kind in the same method call:
|
|
243
292
|
order.requests.delivered.count # >> N + M
|
244
293
|
```
|
245
294
|
|
295
|
+
## Annotations helpers
|
296
|
+
Sometimes under irb console you need a quick tip on whats extension available on a enum, so some describe_* helpers added to enum wrapper
|
297
|
+
|
298
|
+
General description methods (describe/describe_short/describe_long)
|
299
|
+
```
|
300
|
+
Class.enum.describe
|
301
|
+
```
|
302
|
+

|
303
|
+
|
304
|
+
```
|
305
|
+
Class.enum.describe_short
|
306
|
+
```
|
307
|
+

|
308
|
+
|
309
|
+
```
|
310
|
+
Class.enum.describe_long
|
311
|
+
```
|
312
|
+

|
313
|
+
|
314
|
+
And per extension methods (describe_enum_i, e.t.c)
|
315
|
+
|
316
|
+
```ruby
|
317
|
+
EnumAnnotated.test_types.describe_mass_assign_enum
|
318
|
+
```
|
319
|
+

|
320
|
+
|
246
321
|
## Tests
|
247
|
-
|
248
|
-
|
322
|
+
Starting version 0.6 added support for rails 7+ enum definitions, that's making testing a little bit not that easy as running `rake test`.
|
323
|
+
Now testings are done via `docker-compose up`. Look closer to Dockerfiles and `docker-compose.yml`
|
324
|
+
to get the idea how they working simultaneously without interference with each other.
|
325
|
+
|
326
|
+
|
249
327
|
## Development
|
250
328
|
|
329
|
+
## TODO
|
330
|
+
[] better support for suffix/prefix as enum does
|
331
|
+
[] describe method to observe current extension
|
251
332
|
|
252
333
|
## Contributing
|
253
334
|
|
@@ -258,6 +339,6 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/alekse
|
|
258
339
|
|
259
340
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
260
341
|
|
261
|
-
### Thanks
|
262
342
|
|
263
|
-
Thanks
|
343
|
+
### Thanks
|
344
|
+
Thanks for the first star to vzamanillo, it inspires me to do mass refactor and gracefully cover code in this gem by tests.
|
data/docker-compose.yml
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
version: "3.7"
|
2
|
+
|
3
|
+
services:
|
4
|
+
test:
|
5
|
+
build: .
|
6
|
+
image: enum_ext
|
7
|
+
command: rake test
|
8
|
+
volumes:
|
9
|
+
- './lib:/app/lib'
|
10
|
+
- './test:/app/test'
|
11
|
+
- './Gemfile_rails_6:/app/Gemfile'
|
12
|
+
- './Gemfile_rails_6.lock:/app/Gemfile.lock'
|
13
|
+
|
14
|
+
test_7_0:
|
15
|
+
build:
|
16
|
+
context: .
|
17
|
+
dockerfile: Dockerfile_rails_7
|
18
|
+
image: enum_ext_rails_7
|
19
|
+
command: rake test
|
20
|
+
volumes:
|
21
|
+
- './lib:/app/lib'
|
22
|
+
- './test:/app/test'
|
23
|
+
- './Gemfile_rails_7:/app/Gemfile'
|
24
|
+
- './Gemfile_rails_7.lock:/app/Gemfile.lock'
|
data/img.png
ADDED
Binary file
|
data/img_1.png
ADDED
Binary file
|
data/img_2.png
ADDED
Binary file
|
data/img_3.png
ADDED
Binary file
|
@@ -0,0 +1,172 @@
|
|
1
|
+
# I wanted to add some quick live annotation to what's defined and how it could be used
|
2
|
+
# but have no idea how to do this in a super-neat way, so it a little bit chaotic and experimental
|
3
|
+
module EnumExt::Annotated
|
4
|
+
|
5
|
+
# call it to see what's your enum current opitons
|
6
|
+
def describe_basic
|
7
|
+
puts yellow( "Basic #{enum_name} definition: \n" )
|
8
|
+
print_hash(enum_values)
|
9
|
+
end
|
10
|
+
|
11
|
+
# call it to see all enum extensions defined.
|
12
|
+
def describe_long
|
13
|
+
puts yellow( "\nEnumExt extensions:" )
|
14
|
+
|
15
|
+
puts [
|
16
|
+
describe_enum_i(false),
|
17
|
+
describe_mass_assign_enum(false),
|
18
|
+
describe_multi_enum_scopes(false),
|
19
|
+
describe_supersets(false),
|
20
|
+
describe_translations(false),
|
21
|
+
describe_humanizations(false)
|
22
|
+
].join( "\n" + "-" * 100 + "\n" )
|
23
|
+
end
|
24
|
+
|
25
|
+
def describe(short = true)
|
26
|
+
describe_basic
|
27
|
+
short ? describe_short : describe_long
|
28
|
+
end
|
29
|
+
|
30
|
+
def describe_short
|
31
|
+
enabled, disabled = enabled_features.except(:key_sample).partition{!_2.blank?}.map{ |prt| prt.map(&:shift) }
|
32
|
+
puts <<~SHORT
|
33
|
+
#{yellow("EnumExt extensions:")}
|
34
|
+
#{cyan("Enabled")}: #{enabled.join(", ")}
|
35
|
+
#{red("Disabled")}: #{disabled.join(", ")}
|
36
|
+
SHORT
|
37
|
+
|
38
|
+
print_short(:supersets)
|
39
|
+
print_short(:translations)
|
40
|
+
print_short(:humanization)
|
41
|
+
end
|
42
|
+
|
43
|
+
# --------------------------------------------------------------------
|
44
|
+
# ------------- per helpers describers -------------------------------
|
45
|
+
# --------------------------------------------------------------------
|
46
|
+
def describe_enum_i(output = true)
|
47
|
+
description = basic_helpers_usage_header(:enum_i)
|
48
|
+
description << <<~ENUM_I if enabled_features[:enum_i]
|
49
|
+
#{black("instance")}.#{cyan( enabled_features[:enum_i] )}
|
50
|
+
# output will be same as #{base_class.to_s}.#{enum_name}[:#{enabled_features[:key_sample]}]
|
51
|
+
ENUM_I
|
52
|
+
|
53
|
+
output ? puts(description) : description
|
54
|
+
end
|
55
|
+
|
56
|
+
def describe_mass_assign_enum(output = true)
|
57
|
+
description = basic_helpers_usage_header(:mass_assign_enum)
|
58
|
+
description << <<~MASS_ASSIGN if enabled_features[:mass_assign_enum]
|
59
|
+
# To assign #{enabled_features[:key_sample]} to all elements of any_scope or relation call:
|
60
|
+
#{black(base_class.to_s)}.any_scope.#{cyan( enabled_features[:mass_assign_enum] )}
|
61
|
+
MASS_ASSIGN
|
62
|
+
|
63
|
+
output ? puts(description) : description
|
64
|
+
end
|
65
|
+
|
66
|
+
def describe_multi_enum_scopes(output = true)
|
67
|
+
description = basic_helpers_usage_header(:multi_enum_scopes)
|
68
|
+
description << <<~MULTI_SCOPES if enabled_features[:multi_enum_scopes]
|
69
|
+
# Two scopes: with_#{enum_name} and without_#{enum_name} are defined
|
70
|
+
# To get elements with a given enums or supersets values call:
|
71
|
+
#{black(base_class.to_s)}.#{cyan("with_#{enum_name}")}(:#{keys.sample(2).join(", :")})
|
72
|
+
\n# To get all elements except for the ones with enums or supersets values call:
|
73
|
+
#{black(base_class.to_s)}.#{cyan("without_#{enum_name}")}(:#{keys.sample(2).join(", :")})
|
74
|
+
MULTI_SCOPES
|
75
|
+
|
76
|
+
output ? puts(description) : description
|
77
|
+
end
|
78
|
+
|
79
|
+
def describe_supersets(output = true)
|
80
|
+
description = if enabled_features[:supersets].blank?
|
81
|
+
red( "\nSupersets not used!\n" )
|
82
|
+
else
|
83
|
+
red( "\nSupersets definitions:\n" ) << inspect_hash(enabled_features[:supersets]) << <<~SUPERSETS
|
84
|
+
# Instance methods added: #{enabled_features[:supersets].keys.join("?, ")}?
|
85
|
+
|
86
|
+
# Class level methods added: #{enabled_features[:supersets].keys.join(", ")}
|
87
|
+
SUPERSETS
|
88
|
+
end
|
89
|
+
|
90
|
+
output ? puts(description) : description
|
91
|
+
end
|
92
|
+
|
93
|
+
def describe_translations(output = true)
|
94
|
+
description = if enabled_features[:translations].blank?
|
95
|
+
red( "\nTranslations not used!\n" )
|
96
|
+
else
|
97
|
+
red( "\nTranslations definitions (will skip instance dependent translation)\n" ) <<
|
98
|
+
inspect_hash(enabled_features[:translations])
|
99
|
+
end
|
100
|
+
|
101
|
+
output ? puts(description) : description
|
102
|
+
end
|
103
|
+
|
104
|
+
def describe_humanizations(output = true)
|
105
|
+
description = if enabled_features[:humanization].blank?
|
106
|
+
red( "\nHumanization not used!\n" )
|
107
|
+
else
|
108
|
+
red( "\nHumanization definitions (will skip instance dependent humanization)\n" ) <<
|
109
|
+
inspect_hash(enabled_features[:humanization])
|
110
|
+
end
|
111
|
+
|
112
|
+
output ? puts(description) : description
|
113
|
+
end
|
114
|
+
|
115
|
+
private
|
116
|
+
|
117
|
+
def enabled_features
|
118
|
+
enum_sample = keys.first
|
119
|
+
{
|
120
|
+
key_sample: enum_sample,
|
121
|
+
enum_i: base_class.instance_methods.include?("#{enum_name}_i".to_sym) && "#{enum_name}_i",
|
122
|
+
mass_assign_enum: base_class.respond_to?("#{enum_sample}!") && "#{enum_sample}!",
|
123
|
+
multi_enum_scopes: base_class.respond_to?("with_#{enum_name.to_s.pluralize}") && "with_#{enum_name.to_s.pluralize}",
|
124
|
+
supersets: supersets_raw,
|
125
|
+
translations: try(:t_options),
|
126
|
+
humanization: try(:t_options)
|
127
|
+
}
|
128
|
+
end
|
129
|
+
|
130
|
+
def basic_helpers_usage_header(helper_name)
|
131
|
+
enabled_features[helper_name] ? "\n#{red(helper_name)} helpers enabled, usage:\n"
|
132
|
+
: "\n#{helper_name} wasn't used\n"
|
133
|
+
end
|
134
|
+
|
135
|
+
def print_hash(hsh)
|
136
|
+
defined?(ai) ? ap(hsh) : pp(hsh)
|
137
|
+
end
|
138
|
+
|
139
|
+
def inspect_hash(hsh)
|
140
|
+
defined?(ai) ? hsh.ai : hsh.inspect
|
141
|
+
end
|
142
|
+
|
143
|
+
def print_short(feature)
|
144
|
+
if enabled_features[feature].present?
|
145
|
+
puts black("#{feature.to_s.humanize}:")
|
146
|
+
print_hash(enabled_features[feature])
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
def yellow(str)
|
151
|
+
# yellow ANSI color
|
152
|
+
"\e[0;33;49m#{str}\e[0m"
|
153
|
+
end
|
154
|
+
|
155
|
+
def cyan(str)
|
156
|
+
# cyan ANSI color
|
157
|
+
"\e[0;36;49m#{str}\e[0m"
|
158
|
+
end
|
159
|
+
|
160
|
+
def red(str)
|
161
|
+
# red ANSI color
|
162
|
+
"\e[0;31;49m#{str}\e[0m"
|
163
|
+
end
|
164
|
+
|
165
|
+
def black(comment)
|
166
|
+
# bright black bold ANSI color
|
167
|
+
"\e[0;90;1;49m#{comment}\e[0m"
|
168
|
+
end
|
169
|
+
|
170
|
+
end
|
171
|
+
|
172
|
+
|