jason 0.2.0 → 0.3.0
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.
- data/README.md +29 -15
- data/jason.gemspec +1 -1
- data/lib/jason.rb +40 -11
- data/lib/jason/json_eruby.rb +14 -0
- data/lib/jason/version.rb +1 -1
- data/test/rails/app/views/test/index.jason +4 -2
- data/test/test_jason.rb +32 -22
- data/test/test_json_eruby.rb +12 -0
- metadata +6 -3
data/README.md
CHANGED
@@ -6,7 +6,7 @@ There's no easy way to create JSON templates in Ruby.
|
|
6
6
|
|
7
7
|
## Solution ##
|
8
8
|
|
9
|
-
Use
|
9
|
+
Use Erubis to make the simplest thing that could possibly work.
|
10
10
|
|
11
11
|
## Installation ##
|
12
12
|
|
@@ -14,21 +14,31 @@ Use YAML and Ember to make the simplest thing that could possibly work.
|
|
14
14
|
|
15
15
|
## Usage ##
|
16
16
|
|
17
|
-
You write jason templates in
|
18
|
-
|
19
|
-
|
20
|
-
automatically unindent blocks for you.
|
17
|
+
You write jason templates in a fashion similar to regular old JSON with a
|
18
|
+
notable exception: trailing commas in arrays and objects are automatically
|
19
|
+
removed. This allows you to easily create JSON templates using iterators.
|
21
20
|
|
22
|
-
Jason.render('foo: bar') # => '{"foo":"bar"}'
|
21
|
+
Jason.render('{ "foo": "bar" }') # => '{"foo":"bar"}'
|
23
22
|
|
24
23
|
Jason.render(<<-EOS
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
24
|
+
{
|
25
|
+
"test": [
|
26
|
+
<% if true %>
|
27
|
+
"foo",
|
28
|
+
<% end %>
|
29
|
+
"bar", <%# Notice how this trailing comma is perfectly valid. %>
|
30
|
+
]
|
31
|
+
}
|
29
32
|
EOS
|
30
33
|
) # => '{"test":["foo","bar"]}'
|
31
34
|
|
35
|
+
Jason also redefines the `<%= expr %>` Erubis delimiter so that it converts the
|
36
|
+
expression to a JSON value by calling `#to_json` on the expression. If you'd
|
37
|
+
like to use regular interpolation, use the `<%== expr %>` delimiter instead.
|
38
|
+
|
39
|
+
Jason.render('<%= 'test' %>') # => '"test"'
|
40
|
+
Jason.render('"<%== 'test' %>"') # => '"test"'
|
41
|
+
|
32
42
|
That's it.
|
33
43
|
|
34
44
|
## Usage with Rails ##
|
@@ -36,11 +46,15 @@ That's it.
|
|
36
46
|
Name your view template with the extension `jason`. Everything else is the same.
|
37
47
|
|
38
48
|
# in view_name.jason
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
49
|
+
{
|
50
|
+
"foo": "bar",
|
51
|
+
"baz": [
|
52
|
+
<% unless @we_started_the_fire %>
|
53
|
+
"quz",
|
54
|
+
"quuz",
|
55
|
+
<% end %>
|
56
|
+
]
|
57
|
+
}
|
44
58
|
|
45
59
|
# Renders: {"foo":"bar","baz":["quz","quuz"]}
|
46
60
|
|
data/jason.gemspec
CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
20
|
s.require_paths = ['lib']
|
21
21
|
|
22
|
-
s.add_dependency '
|
22
|
+
s.add_dependency 'erubis'
|
23
23
|
|
24
24
|
s.add_development_dependency 'minitest', '~> 2.0'
|
25
25
|
s.add_development_dependency 'mocha'
|
data/lib/jason.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
require '
|
1
|
+
require 'erubis'
|
2
2
|
require 'json'
|
3
|
-
require '
|
3
|
+
require 'strscan'
|
4
4
|
|
5
5
|
# Renders and compiles Jason templates.
|
6
6
|
module Jason
|
@@ -14,12 +14,12 @@ module Jason
|
|
14
14
|
# @return [String] the rendered template
|
15
15
|
def self.render(template, binding = nil)
|
16
16
|
if binding
|
17
|
-
yaml =
|
17
|
+
yaml = eruby_template(template).result(binding)
|
18
18
|
else
|
19
|
-
yaml =
|
19
|
+
yaml = eruby_template(template).result
|
20
20
|
end
|
21
21
|
|
22
|
-
|
22
|
+
process(yaml)
|
23
23
|
end
|
24
24
|
|
25
25
|
# Compile a template.
|
@@ -29,20 +29,49 @@ module Jason
|
|
29
29
|
# @param [String] template the template to compile
|
30
30
|
# @return [String] the compiled template
|
31
31
|
def self.compile(template)
|
32
|
-
"
|
32
|
+
"#{eruby_template(template).src}; Jason.process(_buf)"
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.process(buffer)
|
36
|
+
JSON.load(remove_trailing_commas(buffer)).to_json
|
33
37
|
end
|
34
38
|
|
35
39
|
private
|
36
40
|
|
37
|
-
def self.
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
41
|
+
def self.eruby_template(template)
|
42
|
+
JSONEruby.new(template)
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.remove_trailing_commas(json)
|
46
|
+
comma_position = nil
|
47
|
+
quoting = nil
|
48
|
+
scanner = StringScanner.new(json)
|
49
|
+
|
50
|
+
while scanner.scan_until(/(\\['"]|['",\]\}])/)
|
51
|
+
case char = scanner[1]
|
52
|
+
when '"', "'"
|
53
|
+
if !quoting
|
54
|
+
quoting = char
|
55
|
+
elsif quoting == char
|
56
|
+
quoting = nil
|
57
|
+
end
|
58
|
+
when ']', '}'
|
59
|
+
if comma_position && json[comma_position + 1...scanner.pos - 1] =~ /^\s*$/
|
60
|
+
json[comma_position] = ''
|
61
|
+
scanner.pos -= 1
|
62
|
+
comma_position = nil
|
63
|
+
end
|
64
|
+
when ','
|
65
|
+
comma_position = scanner.pos - 1 unless quoting
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
json
|
42
70
|
end
|
43
71
|
end
|
44
72
|
|
45
73
|
require 'jason/version'
|
74
|
+
require 'jason/json_eruby'
|
46
75
|
|
47
76
|
if defined? ActionView::Template
|
48
77
|
require 'jason/rails_template_handler'
|
data/lib/jason/version.rb
CHANGED
data/test/test_jason.rb
CHANGED
@@ -1,41 +1,51 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class TestJason < MiniTest::Unit::TestCase
|
4
|
-
test '
|
4
|
+
test '.render without binding' do
|
5
5
|
template = <<-EOF
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
{
|
7
|
+
"foo": "bar",
|
8
|
+
"baz": [
|
9
|
+
<% if true %>
|
10
|
+
"quz",
|
11
|
+
<% end %>
|
12
|
+
"quuz",
|
13
|
+
]
|
14
|
+
}
|
11
15
|
EOF
|
12
16
|
|
13
|
-
assert_equal({'foo' => 'bar', 'baz' => ['quz', 'quuz']}, JSON.load(Jason.render(template)))
|
17
|
+
assert_equal({ 'foo' => 'bar', 'baz' => ['quz', 'quuz'] }, JSON.load(Jason.render(template)))
|
14
18
|
end
|
15
19
|
|
16
|
-
test '
|
20
|
+
test '.render with binding' do
|
17
21
|
test_string = 'bar'
|
18
22
|
template = <<-EOF
|
19
|
-
foo: <%= test_string
|
23
|
+
{ "foo": <%= test_string %>, }
|
20
24
|
EOF
|
21
25
|
|
22
|
-
assert_equal({'foo' => 'bar' }, JSON.load(Jason.render(template, binding)))
|
26
|
+
assert_equal({ 'foo' => 'bar' }, JSON.load(Jason.render(template, binding)))
|
23
27
|
end
|
24
28
|
|
25
|
-
test '
|
29
|
+
test '.compile' do
|
26
30
|
test_string = 'bar'
|
27
31
|
template = <<-EOF
|
28
|
-
foo:
|
32
|
+
{ "foo": "<%== test_string %>", }
|
29
33
|
EOF
|
30
34
|
|
31
|
-
assert_equal({'foo' => 'bar' }, JSON.load(eval(Jason.compile(template))))
|
35
|
+
assert_equal({ 'foo' => 'bar' }, JSON.load(eval(Jason.compile(template))))
|
32
36
|
end
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
37
|
+
|
38
|
+
test '.process' do
|
39
|
+
template = <<-EOF
|
40
|
+
{
|
41
|
+
"foo": "bar",
|
42
|
+
"baz": [
|
43
|
+
"quz",
|
44
|
+
"quuz",
|
45
|
+
],
|
46
|
+
}
|
47
|
+
EOF
|
48
|
+
|
49
|
+
assert_equal({ 'foo' => 'bar', 'baz' => ['quz', 'quuz'] }, JSON.load(Jason.process(template)))
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestJSONEruby < MiniTest::Unit::TestCase
|
4
|
+
test '= and == indicators' do
|
5
|
+
template = <<-EOS
|
6
|
+
<%== 'blah' %>
|
7
|
+
<%= 'blah' %>
|
8
|
+
EOS
|
9
|
+
eruby = Jason::JSONEruby.new(template)
|
10
|
+
assert_equal "blah\n\"blah\"\n", eruby.result
|
11
|
+
end
|
12
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: jason
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.3.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Alexander Kern
|
@@ -10,11 +10,11 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-05-
|
13
|
+
date: 2011-05-31 00:00:00 -07:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
|
-
name:
|
17
|
+
name: erubis
|
18
18
|
prerelease: false
|
19
19
|
requirement: &id001 !ruby/object:Gem::Requirement
|
20
20
|
none: false
|
@@ -131,6 +131,7 @@ files:
|
|
131
131
|
- Rakefile
|
132
132
|
- jason.gemspec
|
133
133
|
- lib/jason.rb
|
134
|
+
- lib/jason/json_eruby.rb
|
134
135
|
- lib/jason/rails_template_handler.rb
|
135
136
|
- lib/jason/version.rb
|
136
137
|
- test/rails/.gitignore
|
@@ -161,6 +162,7 @@ files:
|
|
161
162
|
- test/rails/test/test_helper.rb
|
162
163
|
- test/test_helper.rb
|
163
164
|
- test/test_jason.rb
|
165
|
+
- test/test_json_eruby.rb
|
164
166
|
has_rdoc: true
|
165
167
|
homepage: https://github.com/CapnKernul/jason
|
166
168
|
licenses: []
|
@@ -218,3 +220,4 @@ test_files:
|
|
218
220
|
- test/rails/test/test_helper.rb
|
219
221
|
- test/test_helper.rb
|
220
222
|
- test/test_jason.rb
|
223
|
+
- test/test_json_eruby.rb
|