functionable-json 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 +4 -4
- data/README.md +21 -6
- data/lib/functionable/json/version.rb +1 -1
- data/lib/jsonable/function.rb +3 -2
- 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: 0c87b1ebb532e8595d7ae4371872784a0bfe7ff9e8ef3efc03baf62d256a4258
|
4
|
+
data.tar.gz: 1f22e55fdbbedc465bc0f95d64a4c76294e4bfb8b4bf79a147b67767eae955cd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 435365c9cf093a989b039360140796859ccce5557bd4caac192f067f24055e83a4a9c6b3624b28ebbb253a47252ed3730c9d9e662dd47f3ae92f209745e5c247
|
7
|
+
data.tar.gz: 3050e24bba5c90944ff52f8e3aa360b46cf93e87a8ff90315c21442b275b3d2381db7068f0eca607afea01ca979eb00b6ea76789519e3a5817af17872666fccd
|
data/README.md
CHANGED
@@ -32,14 +32,19 @@ And then execute:
|
|
32
32
|
```ruby
|
33
33
|
{
|
34
34
|
speed: function(val) {
|
35
|
-
return parseFloat(val).toLocaleString();
|
35
|
+
return '$' + parseFloat(val).toLocaleString();
|
36
36
|
}
|
37
37
|
}.to_json
|
38
|
-
#=> "{\"speed\":function(val) { return parseFloat(val).toLocaleString(); }}"
|
38
|
+
#=> "{\"speed\":function(val) { return '$' + parseFloat(val).toLocaleString(); }}"
|
39
39
|
```
|
40
40
|
|
41
|
-
But, don't go wild just yet
|
42
|
-
|
41
|
+
But, don't go wild just yet because there are limitations:
|
42
|
+
- You cannot use `if` keyword because the way JavaScript uses it is different from
|
43
|
+
ruby. It is meant to be used for a simple function. One use case I'm targetting
|
44
|
+
is for formatting options to be used by JavaScript libraries.
|
45
|
+
- Also, don't put multiple functions on the same line. You'll get unexpected result
|
46
|
+
if you do.
|
47
|
+
- You should end statement with `;`.
|
43
48
|
|
44
49
|
If you try the above example on your REPL (e.g. `irb` or `pry`) it won't
|
45
50
|
work since the gem will try to find the file that stores the code. So, you
|
@@ -49,7 +54,7 @@ need to put it on a file and run it.
|
|
49
54
|
|
50
55
|
Well, technically, it's not a JavaScript function. It's a Ruby method named
|
51
56
|
`function` that's available in any object. In JavaScript, a form like that is
|
52
|
-
a _function declaration_, but in Ruby it's a
|
57
|
+
a _function declaration_, but in Ruby it's a _method invocation_. So, in that case,
|
53
58
|
`val` needs to already exist before you call it, and it is. Both `function` and
|
54
59
|
`val` are already a method of an object. If you want to use arguments with
|
55
60
|
another name, you need to declare them as variable first.
|
@@ -61,7 +66,17 @@ arg1, arg2 = nil
|
|
61
66
|
return arg1 + ' == ' + arg2
|
62
67
|
}
|
63
68
|
}.to_json
|
64
|
-
#=> "{\"equality\":function(arg1, arg2) { arg1 + ' == ' + arg2 }}"
|
69
|
+
#=> "{\"equality\":function(arg1, arg2) { return arg1 + ' == ' + arg2 }}"
|
70
|
+
```
|
71
|
+
|
72
|
+
### Use Case
|
73
|
+
|
74
|
+
One example use case is [ApexCharts.RB](https://github.com/styd/apexcharts.rb)
|
75
|
+
formatter. You can add tooltip formatter like this:
|
76
|
+
|
77
|
+
```erb
|
78
|
+
<%= line_chart data, tooltip: {y: {formatter: function(val) { return '$' + parseFloat(val).toLocaleString(); }}} %>
|
79
|
+
|
65
80
|
```
|
66
81
|
|
67
82
|
|
data/lib/jsonable/function.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'json'
|
3
|
+
require 'json'
|
4
|
+
require 'strscan'
|
4
5
|
|
5
6
|
module JSONable
|
6
7
|
class Function < Numeric
|
@@ -43,7 +44,7 @@ module JSONable
|
|
43
44
|
end
|
44
45
|
|
45
46
|
def cleanup_function!
|
46
|
-
|
47
|
+
function.tr("\n", '').squeeze(' ')
|
47
48
|
end
|
48
49
|
end
|
49
50
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: functionable-json
|
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
|
- Adrian Setyadi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-09-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|