js_render 0.4.0 → 0.5.0

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
  SHA1:
3
- metadata.gz: 3537448b722ff236b75da2d4f60f5c69fbfa6290
4
- data.tar.gz: 177de4414b1c645c485fbc6ef1b3bebe5b4a968f
3
+ metadata.gz: 3efa8f3b5071ab1391bc0f0884c7125ddfc875bc
4
+ data.tar.gz: eddc754107e5f99f70b9cb48855781023379735b
5
5
  SHA512:
6
- metadata.gz: 97646f57c0c1428117e0ca0d2bb849f0cf04d6c5cdff34a3fd0f03cdb225f3668ff528b309b7ede336ec335493a7c377e61f46f6f2f651acac2ca89a5f6f14b3
7
- data.tar.gz: 6869b116f9fdeb9842164f57727ad5878752f67da1e90976e7ca76c85e669c54f67dea3430b304d8c5231d13c3f699c8d50ff2c0aca478f49e2fe3ddb943f312
6
+ metadata.gz: f1a34fc4f3b80733f4b3b5335902a1ed43b40db6442cbc73e32f78b847bb263fdfaa9928dfcd5fa671910c95fb3340b6145b08039a0758170c8b969dd8426f84
7
+ data.tar.gz: 5b4011ffccaae946dfac1903abee032074e0cda6392b27e6fcd5af4c73675d55c88de7ebac4a96718e9ef102fbdb1b692972a55b58adb0727e041324f4ec3c5f
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  # JsRender
5
5
 
6
- JsRender is an unopinionated Ruby library for rendering JavaScript "components" on the server side. This approach works with [React](https://facebook.github.io/react/), [Angular](ihttps://angular.io/), [Ember](http://emberjs.com/), or any other library of your choice. The only requirement is that there is a JavaScript function that returns HTML for the component or view such that it can be properly rendered on the server side (e.g. [`ReactDOMServer.renderToString`](https://facebook.github.io/react/docs/top-level-api.html#reactdomserver.rendertostring) in React).
6
+ JsRender is an unopinionated Ruby library for rendering JavaScript "components" on the server side. This approach works with [React](https://facebook.github.io/react/), [Angular](https://angular.io/), [Ember](http://emberjs.com/), or any other library of your choice. The only requirement is that there is a JavaScript function that returns HTML for the component or view such that it can be properly rendered on the server side (e.g. [`ReactDOMServer.renderToString`](https://facebook.github.io/react/docs/top-level-api.html#reactdomserver.rendertostring) in React).
7
7
 
8
8
  The library works in two essential parts:
9
9
  - Calls a JavaScript function (defined by the user) that returns HTML. This allows us to render our JS component/view when the page initially loads, rather than having the delay of doing it solely on the client side.
@@ -150,7 +150,29 @@ Methods you can override if you subclass `JsRender::AssetFinder::Base`:
150
150
 
151
151
  > Defaults to `nil`
152
152
 
153
- ** should_server_render **
153
+ **key_transforms**
154
+
155
+ An array of lambdas (or singletons that implement a `call` method) that can transform the keys of the data being passed into the component. The lambdas in the array are called in order, and the result of a lambda is passed to the next transform (as a string) for each key.
156
+
157
+ `JsRender::Utils::Camelize` is a provided utility that will transform keys to camel case.
158
+
159
+ Note: This only works if the component data is passed in as a hash, if it is already passed in as JSON, then the transforms will be ignored.
160
+
161
+ Example:
162
+
163
+ ```ruby
164
+ JsRender.config.key_transforms = [
165
+ JsRender::Utils::Camelize,
166
+ -> (key) { key + '_SomeEnding'}
167
+ ]
168
+
169
+ # data => { camel_case: 2, something_else: 3 }
170
+ # would be transformed to => { camelCase_SomeEnding: 2, somethingElse_SomeEnding: 3 }
171
+ ```
172
+
173
+ > Defaults to `[]`
174
+
175
+ **should_server_render**
154
176
 
155
177
  This config option is a boolean that specifies if the server render function and associated JS should be executed and run. When it is false, it only returns the span with the unique ID that the client side render function relies upon. This is meant for development purposes and enables things like console logging etc. that would normally cause errors in the `ExecJS` runtime.
156
178
 
@@ -1,6 +1,7 @@
1
1
  require 'js_render/version'
2
2
  require 'js_render/configuration'
3
3
  require 'js_render/errors'
4
+ require 'js_render/utils'
4
5
  require 'js_render/asset_finder'
5
6
  require 'js_render/renderer'
6
7
 
@@ -23,6 +23,7 @@ module JsRender
23
23
  :client_render_function,
24
24
  :use_asset_pipeline,
25
25
  :asset_finder_class,
26
+ :key_transforms,
26
27
  :should_server_render
27
28
 
28
29
  def initialize
@@ -33,6 +34,7 @@ module JsRender
33
34
  @client_render_function = 'window.render*Client'
34
35
  @use_asset_pipeline = false
35
36
  @asset_finder_class = nil
37
+ @key_transforms = []
36
38
  @should_server_render = true
37
39
  end
38
40
  end
@@ -13,7 +13,10 @@ module JsRender
13
13
 
14
14
  def initialize(component_name, data)
15
15
  @component_name = component_name
16
- data = data.to_json if !data.is_a?(String)
16
+ unless data.is_a?(String)
17
+ transform_keys(data)
18
+ data = data.to_json
19
+ end
17
20
  @json_data = data
18
21
  @uuid = SecureRandom.uuid
19
22
  end
@@ -64,5 +67,13 @@ module JsRender
64
67
  AssetFinder::Base.new
65
68
  end
66
69
  end
70
+
71
+ def transform_keys(data)
72
+ JsRender.config.key_transforms.reduce(data) do |transformed_data, transform|
73
+ transformed_data.tap do |d|
74
+ d.keys.each { |k| d[transform.call(k.to_s)] = d.delete(k) }
75
+ end
76
+ end
77
+ end
67
78
  end
68
79
  end
@@ -0,0 +1,6 @@
1
+ require 'js_render/utils/camelize'
2
+
3
+ module JsRender
4
+ module Utils
5
+ end
6
+ end
@@ -0,0 +1,9 @@
1
+ module JsRender
2
+ module Utils
3
+ # based on rails camelize
4
+ Camelize = ->(key) do
5
+ key = key.sub(/^(?:(?=\b|[A-Z_])|\w)/) { $&.downcase }
6
+ key.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }
7
+ end
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module JsRender
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: js_render
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Lehman
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-01 00:00:00.000000000 Z
11
+ date: 2016-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -219,6 +219,8 @@ files:
219
219
  - lib/js_render/rails/railtie.rb
220
220
  - lib/js_render/rails/view_helpers.rb
221
221
  - lib/js_render/renderer.rb
222
+ - lib/js_render/utils.rb
223
+ - lib/js_render/utils/camelize.rb
222
224
  - lib/js_render/version.rb
223
225
  homepage: http://github.com/jdlehman/js_render
224
226
  licenses: