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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2b1f886bc7124afb3308e97ca245f525de0c314ffd01a64978fcfdd039e161ca
4
- data.tar.gz: eba9554f9f8e1c4431b9b5caee7f97259b58c428d50ed0413ce96a3729ca0bc1
3
+ metadata.gz: 9fadd105115f24dc921adfe7185cfc686fe0d81ec048c581a9d194f992b42627
4
+ data.tar.gz: 37c92078a937aa1ec6383f2f89ae6aef0cfc00b4b640134d76bdeb168f6fa4c9
5
5
  SHA512:
6
- metadata.gz: 55af3d8c3e08f270259a66a42193187441eed608013d0e9e5a340aac1ebde36ebcb56371ca40e7b9fee31c00a2f4e3a1019fe057efa0f0f54013093bc5d41e1f
7
- data.tar.gz: d6c15d8edac06b856c57d4487dce5f6206c084ec3e32f4deb326ff52fe5dda3139f65b4d5527fba860bc6aeba7113cc8f1a0112ed19e514bbe7c9a2395f4b6cc
6
+ metadata.gz: 7e74d81cdeb09deb48d6bcedbfc38c8baeffd26dd5bcbec32ec3e6c855ca304d13b85e3c027e7e320be330e2e6c2ee455f71baaeb8d48491b8faa41f9bdcaa96
7
+ data.tar.gz: 78146fc63abc61689c8c4b9f0b3927d7c3a1915c8da76d270acf6a516945b5091c8129cc1baf5428d465e2234bea3d7f7b88b47c82f7827b926d582cb75301c8
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- enu (0.1.0)
4
+ enu (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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
@@ -48,6 +48,10 @@ class Enu
48
48
  raise "empty enum, sad enum" unless options&.any?
49
49
  keys.first
50
50
  end
51
+
52
+ def to_json
53
+ keys.map { |key| [key, key] }.to_h.to_json
54
+ end
51
55
  end
52
56
 
53
57
  private_class_method :new, :option, :options=
data/lib/enu/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Enu
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
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.0
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-01 00:00:00.000000000 Z
11
+ date: 2019-08-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler