jason 0.3.1 → 0.4.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 CHANGED
@@ -39,7 +39,13 @@ like to use regular interpolation, use the `<%== expr %>` delimiter instead.
39
39
  Jason.render('<%= 'test' %>') # => '"test"'
40
40
  Jason.render('"<%== 'test' %>"') # => '"test"'
41
41
 
42
- That's it.
42
+ You can configure the output format of the JSON:
43
+
44
+ Jason.output_format = :compact # This is the default.
45
+ Jason.output_format = :pretty
46
+
47
+ `:compact` will remove any unnecessary whitespace in the JSON while `:pretty`
48
+ will indent the result JSON so that it looks, well, pretty.
43
49
 
44
50
  ## Usage with Rails ##
45
51
 
@@ -10,7 +10,7 @@ Gem::Specification.new do |s|
10
10
  s.email = ['alex@kernul.com']
11
11
  s.homepage = 'https://github.com/CapnKernul/jason'
12
12
  s.summary = %q{Insanely simple JSON templates}
13
- s.description = %q{Create JSON templates using YAML and Ember}
13
+ s.description = %q{Create JSON templates using Erubis}
14
14
 
15
15
  s.rubyforge_project = 'jason'
16
16
 
@@ -4,6 +4,20 @@ require 'strscan'
4
4
 
5
5
  # Renders and compiles Jason templates.
6
6
  module Jason
7
+ class << self
8
+ attr_writer :output_format
9
+
10
+ # The output format of the JSON.
11
+ #
12
+ # I suggest using `:pretty` for development and testing and `:compact` for
13
+ # production.
14
+ #
15
+ # @returns [:pretty, :compact]
16
+ def output_format
17
+ @output_format ||= :compact
18
+ end
19
+ end
20
+
7
21
  # Render a template.
8
22
  #
9
23
  # @example
@@ -40,7 +54,13 @@ module Jason
40
54
  #
41
55
  # @param [String] buffer
42
56
  def self.process(buffer)
43
- JSON.load(remove_trailing_commas(buffer)).to_json
57
+ obj = JSON.load(remove_trailing_commas(buffer))
58
+
59
+ if output_format == :pretty
60
+ JSON.pretty_generate(obj)
61
+ else
62
+ JSON.generate(obj)
63
+ end
44
64
  end
45
65
 
46
66
  private
@@ -1,3 +1,3 @@
1
1
  module Jason
2
- VERSION = '0.3.1'
2
+ VERSION = '0.4.0'
3
3
  end
@@ -76,4 +76,13 @@ EOF
76
76
 
77
77
  assert_equal({ 'foo' => 'bar', 'baz' => ['quz', 'quuz'] }, JSON.load(Jason.process(template)))
78
78
  end
79
+
80
+ test '.output_format' do
81
+ assert_equal :compact, Jason.output_format
82
+ assert_equal '{"foo":["bar"]}', Jason.process('{"foo":["bar"]}')
83
+
84
+ Jason.output_format = :pretty
85
+ assert_equal :pretty, Jason.output_format
86
+ assert_equal "{\n \"foo\": [\n \"bar\"\n ]\n}", Jason.process('{"foo":["bar"]}')
87
+ end
79
88
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: jason
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.1
5
+ version: 0.4.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Alexander Kern
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-06-01 00:00:00 -07:00
13
+ date: 2011-06-06 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -112,7 +112,7 @@ dependencies:
112
112
  version: "0"
113
113
  type: :development
114
114
  version_requirements: *id009
115
- description: Create JSON templates using YAML and Ember
115
+ description: Create JSON templates using Erubis
116
116
  email:
117
117
  - alex@kernul.com
118
118
  executables: []