functionable-json 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +71 -0
- data/lib/functionable/json.rb +15 -0
- data/lib/functionable/json/version.rb +7 -0
- data/lib/jsonable/function.rb +49 -0
- metadata +106 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a06b3fcc634432ab0c11c91e45ce0884be2d3b706df393d9c4e40e46511b3b84
|
4
|
+
data.tar.gz: f2e06cb7530133d2a70acd7adae804d3a81f4829a7077cbaca4eb4db70d67f35
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9f1535e895940ea58974e3a70515079b591b435fdc06c3f38e7a03a0a7730f586938bef63562dc6f2b5910f6bfe64a6eeee53725ae2065d5865b4ac12e2f98da
|
7
|
+
data.tar.gz: 2075fe63cf430bad24d76b6ca191a17f2b97849e5007d3569c0ba2cb78a24ea99aa514be35377ab7fb6bfb4743bc1f6b2c606dfd5cfad1c4018cf39c041ebdd7
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Adrian Setyadi
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
[![Gem Version](https://img.shields.io/gem/v/functionable-json.svg?label=functionable-json)](https://rubygems.org/gems/functionable-json)
|
2
|
+
[![Build Status](https://travis-ci.org/styd/functionable-json.svg?branch=master)](https://travis-ci.org/styd/functionable-json)
|
3
|
+
|
4
|
+
According to [RFC8259](https://tools.ietf.org/html/rfc8259):
|
5
|
+
|
6
|
+
> JSON can represent four primitive types (`strings`, `numbers`, `booleans`,
|
7
|
+
> and `null`) and two structured types (`objects` and `arrays`).
|
8
|
+
|
9
|
+
Now, with this gem, you can have the 7th one, `function`.
|
10
|
+
|
11
|
+
So, basically, what I'm saying is the json is no longer conform with the
|
12
|
+
**RFC8259**. The resulting function is not guaranteed to be executable in
|
13
|
+
JavaScript because it doesn't check what's inside the function. It also cannot
|
14
|
+
be parsed back into hash. So, use it at your own risk!
|
15
|
+
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
Add this line to your application's Gemfile:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
gem 'functionable-json'
|
23
|
+
```
|
24
|
+
|
25
|
+
And then execute:
|
26
|
+
|
27
|
+
$ bundle
|
28
|
+
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
{
|
34
|
+
speed: function(val) {
|
35
|
+
return parseFloat(val).toLocaleString();
|
36
|
+
}
|
37
|
+
}.to_json
|
38
|
+
#=> "{\"speed\":function(val) { return parseFloat(val).toLocaleString(); }}"
|
39
|
+
```
|
40
|
+
|
41
|
+
But, don't go wild just yet and try not to put multiple functions on the same
|
42
|
+
line.
|
43
|
+
|
44
|
+
If you try the above example on your REPL (e.g. `irb` or `pry`) it won't
|
45
|
+
work since the gem will try to find the file that stores the code. So, you
|
46
|
+
need to put it on a file and run it.
|
47
|
+
|
48
|
+
### How can a JavaScript function be called from Ruby?
|
49
|
+
|
50
|
+
Well, technically, it's not a JavaScript function. It's a Ruby method named
|
51
|
+
`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,
|
53
|
+
`val` needs to already exist before you call it, and it is. Both `function` and
|
54
|
+
`val` are already a method of an object. If you want to use arguments with
|
55
|
+
another name, you need to declare them as variable first.
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
arg1, arg2 = nil
|
59
|
+
{
|
60
|
+
equality: function(arg1, arg2) {
|
61
|
+
return arg1 + ' == ' + arg2
|
62
|
+
}
|
63
|
+
}.to_json
|
64
|
+
#=> "{\"equality\":function(arg1, arg2) { arg1 + ' == ' + arg2 }}"
|
65
|
+
```
|
66
|
+
|
67
|
+
|
68
|
+
## License
|
69
|
+
|
70
|
+
The gem is available as open source under the terms of the
|
71
|
+
[MIT License](https://opensource.org/licenses/MIT).
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../jsonable/function'
|
4
|
+
|
5
|
+
module Functionable
|
6
|
+
module JSON
|
7
|
+
def function(*args, &block)
|
8
|
+
JSONable::Function.new(&block)
|
9
|
+
end
|
10
|
+
|
11
|
+
def val; end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
Object.include(Functionable::JSON)
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json' unless defined? ::JSON
|
4
|
+
|
5
|
+
module JSONable
|
6
|
+
class Function < Numeric
|
7
|
+
attr_reader :file_name, :line_number, :function
|
8
|
+
|
9
|
+
def initialize(&block)
|
10
|
+
@file_name, @line_number = block.source_location
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_json(*)
|
14
|
+
scrape_function!
|
15
|
+
cleanup_function!
|
16
|
+
end
|
17
|
+
|
18
|
+
def scrape_function!
|
19
|
+
@function = +'function'
|
20
|
+
open_braces = 0
|
21
|
+
|
22
|
+
catch(:function_ends) do
|
23
|
+
File.foreach(file_name).with_index(1) do |line, index|
|
24
|
+
next if index < line_number
|
25
|
+
|
26
|
+
scanner = StringScanner.new(line)
|
27
|
+
scanner.scan_until(/function/)
|
28
|
+
while scanner.scan(/[^{}]+/)
|
29
|
+
@function << scanner.matched
|
30
|
+
while str = scanner.scan(/{|}/)
|
31
|
+
@function << str
|
32
|
+
case str
|
33
|
+
when '{'
|
34
|
+
open_braces += 1
|
35
|
+
when '}'
|
36
|
+
throw(:function_ends) if open_braces <= 1
|
37
|
+
open_braces -= 1
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def cleanup_function!
|
46
|
+
@function.tr("\n", '').squeeze(' ')
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: functionable-json
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Adrian Setyadi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-08-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.12.2
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.12.2
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description: Generate JSON with JavaScript functions from ruby hash
|
70
|
+
email:
|
71
|
+
- a.styd@yahoo.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- LICENSE.txt
|
77
|
+
- README.md
|
78
|
+
- lib/functionable/json.rb
|
79
|
+
- lib/functionable/json/version.rb
|
80
|
+
- lib/jsonable/function.rb
|
81
|
+
homepage: https://github.com/styd/functionable-json
|
82
|
+
licenses:
|
83
|
+
- MIT
|
84
|
+
metadata:
|
85
|
+
homepage_uri: https://github.com/styd/functionable-json
|
86
|
+
source_code_uri: https://github.com/styd/functionable-json
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubygems_version: 3.0.3
|
103
|
+
signing_key:
|
104
|
+
specification_version: 4
|
105
|
+
summary: Generate JSON with JavaScript functions from ruby hash
|
106
|
+
test_files: []
|