functionable-json 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: a06b3fcc634432ab0c11c91e45ce0884be2d3b706df393d9c4e40e46511b3b84
4
- data.tar.gz: f2e06cb7530133d2a70acd7adae804d3a81f4829a7077cbaca4eb4db70d67f35
3
+ metadata.gz: 0c87b1ebb532e8595d7ae4371872784a0bfe7ff9e8ef3efc03baf62d256a4258
4
+ data.tar.gz: 1f22e55fdbbedc465bc0f95d64a4c76294e4bfb8b4bf79a147b67767eae955cd
5
5
  SHA512:
6
- metadata.gz: 9f1535e895940ea58974e3a70515079b591b435fdc06c3f38e7a03a0a7730f586938bef63562dc6f2b5910f6bfe64a6eeee53725ae2065d5865b4ac12e2f98da
7
- data.tar.gz: 2075fe63cf430bad24d76b6ca191a17f2b97849e5007d3569c0ba2cb78a24ea99aa514be35377ab7fb6bfb4743bc1f6b2c606dfd5cfad1c4018cf39c041ebdd7
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 and try not to put multiple functions on the same
42
- line.
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 method invocation. So, in that case,
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
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Functionable
4
4
  module JSON
5
- VERSION = '0.1.0'
5
+ VERSION = '0.1.1'
6
6
  end
7
7
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'json' unless defined? ::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
- @function.tr("\n", '').squeeze(' ')
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.0
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-08-30 00:00:00.000000000 Z
11
+ date: 2019-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler