jason 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
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 YAML and Ember to make the simplest thing that could possibly work.
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 plain YAML and Ember. Jason will take care of the
18
- (ultra-simple) conversion to JSON. You can use the Ember shorthand syntax, leave
19
- off the `end` of blocks, and use natural indentation, since Ember will
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
- test:
26
- % if true
27
- - foo
28
- - bar
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
- foo: bar
40
- baz:
41
- % unless @we_started_the_fire
42
- - quz
43
- - quuz
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
 
@@ -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 'ember'
22
+ s.add_dependency 'erubis'
23
23
 
24
24
  s.add_development_dependency 'minitest', '~> 2.0'
25
25
  s.add_development_dependency 'mocha'
@@ -1,6 +1,6 @@
1
- require 'ember'
1
+ require 'erubis'
2
2
  require 'json'
3
- require 'yaml'
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 = ember_template(template).render(binding)
17
+ yaml = eruby_template(template).result(binding)
18
18
  else
19
- yaml = ember_template(template).render
19
+ yaml = eruby_template(template).result
20
20
  end
21
21
 
22
- YAML::load(yaml).to_json
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
- "YAML::load(#{ember_template(template).program}).to_json"
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.ember_template(template)
38
- Ember::Template.new(template,
39
- :unindent => true,
40
- :infer_end => true,:shorthand => true
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'
@@ -0,0 +1,14 @@
1
+ require 'erubis'
2
+
3
+ module Jason
4
+ class JSONEruby < Erubis::Eruby
5
+ def add_expr(src, code, indicator)
6
+ case indicator
7
+ when '='
8
+ add_expr_literal(src, "(#{code}).to_json")
9
+ when '=='
10
+ add_expr_literal(src, code)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -1,3 +1,3 @@
1
1
  module Jason
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -1,2 +1,4 @@
1
- instance: <%= @instance_var %>
2
- local: <%= local_var %>
1
+ {
2
+ "instance": <%= @instance_var %>,
3
+ "local": <%= local_var %>,
4
+ }
@@ -1,41 +1,51 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class TestJason < MiniTest::Unit::TestCase
4
- test '#render without binding' do
4
+ test '.render without binding' do
5
5
  template = <<-EOF
6
- foo: bar
7
- baz:
8
- % if true
9
- - quz
10
- - quuz
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 '#render with binding' do
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 '#compile' do
29
+ test '.compile' do
26
30
  test_string = 'bar'
27
31
  template = <<-EOF
28
- foo: <%= test_string %>
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
- end
34
-
35
- puts Jason.render(<<-EOS
36
- test:
37
- % if true
38
- - foo
39
- - bar
40
- EOS
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.2.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-29 00:00:00 -07:00
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: ember
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