enu 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +56 -0
- data/lib/enu.rb +4 -0
- data/lib/enu/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: 9fadd105115f24dc921adfe7185cfc686fe0d81ec048c581a9d194f992b42627
|
4
|
+
data.tar.gz: 37c92078a937aa1ec6383f2f89ae6aef0cfc00b4b640134d76bdeb168f6fa4c9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7e74d81cdeb09deb48d6bcedbfc38c8baeffd26dd5bcbec32ec3e6c855ca304d13b85e3c027e7e320be330e2e6c2ee455f71baaeb8d48491b8faa41f9bdcaa96
|
7
|
+
data.tar.gz: 78146fc63abc61689c8c4b9f0b3927d7c3a1915c8da76d270acf6a516945b5091c8129cc1baf5428d465e2234bea3d7f7b88b47c82f7827b926d582cb75301c8
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -256,6 +256,62 @@ There is a known issue with using custom directories in a Rails application. If
|
|
256
256
|
Spring stopped.
|
257
257
|
```
|
258
258
|
|
259
|
+
### Export enum definition to JavaScript
|
260
|
+
|
261
|
+
Sometimes it is necessary to use same enum values on the client side, for instance, when you receive data object serializations from API. In this case it is possible to share your enum type definition with JavaScript code.
|
262
|
+
|
263
|
+
#### Webpack
|
264
|
+
|
265
|
+
Make sure you have [rails-erb-loader](https://github.com/usabilityhub/rails-erb-loader) installed and enabled so your Webpack configuration will process ERB properly. If you use [Webpacker](https://github.com/rails/webpacker) gem, run `bundle exec rails webpacker:install:erb`.
|
266
|
+
|
267
|
+
This example will export `PostStatus` enum to a JavaScript object:
|
268
|
+
|
269
|
+
```javascript
|
270
|
+
// app/javascript/src/enums.js.erb
|
271
|
+
export const postStatus = Object.freeze(<%= PostStatus.to_json %>)
|
272
|
+
```
|
273
|
+
|
274
|
+
`to_json` method generates JSON replresentation for the enum class options:
|
275
|
+
|
276
|
+
```json
|
277
|
+
{
|
278
|
+
"draft": "draft",
|
279
|
+
"published": "published",
|
280
|
+
"moderated": "moderated",
|
281
|
+
"deleted": "deleted"
|
282
|
+
}
|
283
|
+
|
284
|
+
```
|
285
|
+
|
286
|
+
Use generated object to reference enum values:
|
287
|
+
|
288
|
+
```javascript
|
289
|
+
import { postStatus } from 'enums'
|
290
|
+
|
291
|
+
postStatus.draft // => "draft"
|
292
|
+
```
|
293
|
+
|
294
|
+
`Object.freeze()` call in the example above will prevent accidental change of the `postStatus` values. It is optional, though.
|
295
|
+
|
296
|
+
#### Rails Assets Pipeline
|
297
|
+
|
298
|
+
Same idea:
|
299
|
+
|
300
|
+
```javascript
|
301
|
+
// app/assets/javascripts/application.js
|
302
|
+
|
303
|
+
//= require_self
|
304
|
+
//= require_tree ./shared
|
305
|
+
|
306
|
+
window.App || (window.App = {});
|
307
|
+
```
|
308
|
+
|
309
|
+
```javascript
|
310
|
+
// app/assets/javascripts/shared/enums.js.erb
|
311
|
+
|
312
|
+
App.postStatus = Object.freeze(<%= PostStatus.to_json %>)
|
313
|
+
```
|
314
|
+
|
259
315
|
## Development
|
260
316
|
|
261
317
|
After checking out the repository, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/enu.rb
CHANGED
data/lib/enu/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enu
|
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
|
- Alex Musayev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-08-
|
11
|
+
date: 2019-08-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|