jsonbuilder 0.0.5 → 0.0.6

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 CHANGED
@@ -1,6 +1,7 @@
1
1
  = jsonbuilder
2
2
 
3
- by nov <nov@cerego.com>
3
+ by nov http://github.com/nov <nov@cerego.com> and
4
+ birkirb http://github.com/birkirb
4
5
 
5
6
  == Description
6
7
 
@@ -16,47 +17,49 @@
16
17
 
17
18
  === Gem Installation
18
19
 
19
- gem install jsonbuilder
20
+ gem install jsonbuilder --source http://gems.rubyforge.org
21
+
22
+ OR for the lastest development version
23
+
24
+ gem install nov-jsonbuilder --source http://gems.github.com
20
25
 
21
26
  == Features/Problems
22
27
 
23
- JsonMarkup returns Hash, not Json text.
24
- You need to call to_json for returned target.
28
+ The Hash builder will return a hash structured in a similar way as the corresponding xml
29
+ built by XmlMarkup. The Json builder will return the same kind of hash as a JSON string
25
30
 
26
31
  USAGE:
27
- def to_markup(markup, options = {})
28
- markup = case format
29
- when :xml
30
- Builder::XmlMarkup.new
31
- when :json
32
- Builder::JsonMarkup.new
33
- end
34
- markup.user(
32
+ def serialize(builder, options = {})
33
+ builder.user(
35
34
  :id => id,
36
35
  :url => url
37
36
  )
38
- markup.array_mode do
39
- markup.images do
37
+ builder.array_mode do
38
+ builder.images do
40
39
  package.images.each do |image|
41
- markup << image.to_markup(format, :only_url => true)
40
+ builder << image.builder(builder.class.new, :only_url => true)
42
41
  end
43
42
  end
44
43
  end
45
- markup.target!
44
+ builder.target!
46
45
  end
47
-
46
+
48
47
  def to_xml(options = {})
49
- self.to_markup(:xml, options)
48
+ self.serialize(Builder::XMmlMarkup.new, options)
50
49
  end
51
-
50
+
51
+ def to_hash(options = {})
52
+ self.serialize(Builder::Hash.new, options)
53
+ end
54
+
52
55
  def to_json(options = {})
53
- self.to_markup(:json, options).to_json
56
+ self.serialize(Builder::Json.new, options)
54
57
  end
55
58
 
56
59
  == Synopsis
57
60
 
58
61
  == Copyright
59
62
 
60
- Author:: nov <nov@matake.jp>
63
+ Author:: nov <nov@matake.jp> and birkirb
61
64
  Copyright:: Copyright (c) 2009 nov
62
65
  License:: MIT License
data/Rakefile CHANGED
@@ -60,6 +60,7 @@ spec = Gem::Specification.new do |s|
60
60
  s.files = %w(README ChangeLog Rakefile) +
61
61
  Dir.glob("{spec,lib}/**/*") +
62
62
  Dir.glob("examples/**/*.rb")
63
+ s.add_dependency('builder')
63
64
  end
64
65
 
65
66
  Rake::GemPackageTask.new(spec) do |p|
@@ -0,0 +1,23 @@
1
+ module Builder
2
+
3
+ class Abstract
4
+ def nil?
5
+ false
6
+ end
7
+
8
+ def target!
9
+ end
10
+
11
+ def comment!
12
+ end
13
+
14
+ def declare!
15
+ end
16
+
17
+ def instruct!
18
+ end
19
+
20
+ def comment!
21
+ end
22
+ end
23
+ end
@@ -1,13 +1,5 @@
1
1
  module Builder
2
-
3
- class XmlMarkup
4
- # Do nothing
5
- def array_mode(key = nil, &block)
6
- yield(self)
7
- end
8
- end
9
-
10
- class JsonMarkup
2
+ class Hash < Abstract
11
3
 
12
4
  def initialize(options = {})
13
5
  # @default_content_key is used in such case: markup.key(value, :attr_key => attr_value)
@@ -18,15 +10,11 @@ module Builder
18
10
  @array_mode = false
19
11
  end
20
12
 
21
- def nil?
22
- false
23
- end
24
-
25
13
  # NOTICE: you have to call this method to use array in json
26
14
  def array_mode(key = nil, &block)
27
15
  @array_mode = true
28
- if eval("#{_current}").is_a?(Hash)
29
- key ||= :entries
16
+ if eval("#{_current}").is_a?(::Hash)
17
+ key ||= :entry
30
18
  eval("#{_current}.merge!(key => [])")
31
19
  @path.push(key.to_sym)
32
20
  yield(self)
@@ -46,12 +34,6 @@ module Builder
46
34
  end
47
35
  end
48
36
 
49
- # Do nothing
50
- def comment!; end
51
- def declare!; end
52
- def instruct!; end
53
- def comment!; end
54
-
55
37
  def <<(_target)
56
38
  if @array_mode
57
39
  eval("#{_current} << _target")
@@ -62,7 +44,7 @@ module Builder
62
44
  end
63
45
 
64
46
  def text!(text)
65
- if eval("#{_current}").is_a?(Hash)
47
+ if eval("#{_current}").is_a?(::Hash)
66
48
  eval("#{_current}.merge!({@default_content_key => text})")
67
49
  else
68
50
  eval("#{_current} = text")
@@ -76,7 +58,7 @@ module Builder
76
58
 
77
59
  def method_missing(key, *args, &block)
78
60
  key = args.first.is_a?(Symbol) ? "#{key}:#{args.shift}".to_sym : key.to_sym
79
- args[0] = {@default_content_key => args[0]} if args.size > 1 && !args[0].is_a?(Hash)
61
+ args[0] = {@default_content_key => args[0]} if args.size > 1 && !args[0].is_a?(::Hash)
80
62
  unless @root
81
63
  _root(key, args, &block)
82
64
  else
@@ -105,7 +87,7 @@ module Builder
105
87
  def _set_args(args, &block)
106
88
  args.each do |arg|
107
89
  case arg
108
- when Hash
90
+ when ::Hash
109
91
  self << arg
110
92
  else
111
93
  eval("#{_current} = arg")
@@ -119,5 +101,4 @@ module Builder
119
101
  end
120
102
 
121
103
  end
122
-
123
- end
104
+ end
@@ -0,0 +1,25 @@
1
+ require 'json'
2
+
3
+ module Builder
4
+
5
+ class Json < Hash
6
+
7
+ def initialize(options = {})
8
+ # @default_content_key is used in such case: markup.key(value, :attr_key => attr_value)
9
+ # in this case, we need some key for value.
10
+ @default_content_key = (options[:default_content_key] || :content).to_sym
11
+ @include_root = options[:include_root]
12
+ @target = {}
13
+ @array_mode = false
14
+ end
15
+
16
+ def target!
17
+ if @include_root
18
+ @target.to_json
19
+ else
20
+ @target[@root].to_json
21
+ end
22
+ end
23
+ end
24
+
25
+ end
@@ -0,0 +1,9 @@
1
+ module Builder
2
+ class XmlMarkup
3
+
4
+ # Add this no-op
5
+ def array_mode(key = nil, &block)
6
+ yield(self)
7
+ end
8
+ end
9
+ end
data/lib/jsonbuilder.rb CHANGED
@@ -2,7 +2,7 @@ module JsonBuilder
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 0
5
- REVISION = 5
5
+ REVISION = 6
6
6
  class << self
7
7
  def to_version
8
8
  "#{MAJOR}.#{MINOR}.#{REVISION}"
@@ -15,4 +15,7 @@ module JsonBuilder
15
15
  end
16
16
  end
17
17
 
18
- require 'builder/jsonmarkup'
18
+ require 'builder/abstract'
19
+ require 'builder/hash'
20
+ require 'builder/xml_markup'
21
+ require 'builder/json'
@@ -0,0 +1,182 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+
3
+ describe Builder::Hash, ".new" do
4
+ it "should be accessible" do
5
+ Builder::Hash.should respond_to(:new)
6
+ end
7
+ end
8
+
9
+ describe Builder::Hash do
10
+
11
+ it "should remove the root tag" do
12
+ builder = Builder::Hash.new
13
+ # XML :: <root><tag>value</tag></root>
14
+ builder.root do
15
+ builder.tag "value"
16
+ end
17
+ builder.target!.should == {:tag => "value"}
18
+ end
19
+
20
+ it "should not remove the root tag when include_root is true" do
21
+ builder = Builder::Hash.new(:include_root => true)
22
+ # XML :: <root><tag>value</tag></root>
23
+ builder.root do
24
+ builder.tag "value"
25
+ end
26
+ builder.target!.should == {:root => {:tag => "value"}}
27
+ end
28
+
29
+ it "should use the default_content_key when both content and attributes exist" do
30
+ builder = Builder::Hash.new
31
+ # XML :: <root><tag id="1">value</tag></root>
32
+ builder.root do
33
+ builder.tag("value", :id => 1)
34
+ end
35
+ builder.target!.should == {:tag => {:id => 1, :content => "value"}}
36
+ end
37
+
38
+ it "should use the default_content_key when both cdata! and attributes exist" do
39
+ builder = Builder::Hash.new
40
+ # XML :: <root><tag id="1"><![CDATA[value]]></tag></root>
41
+ builder.root do
42
+ builder.tag(:id => 1) do
43
+ builder.cdata! "value"
44
+ end
45
+ end
46
+ builder.target!.should == {:tag => {:id => 1, :content => "value"}}
47
+ end
48
+
49
+ end
50
+
51
+ describe Builder::Hash, "#target!" do
52
+
53
+ it "should return a String when there is only a root value" do
54
+ builder = Builder::Hash.new
55
+ builder.root("value")
56
+ builder.target!.should be_a(String)
57
+ end
58
+
59
+ it "should return Hash when root has deeper structure" do
60
+ builder = Builder::Hash.new
61
+ builder.root do
62
+ builder.item("value")
63
+ end
64
+ builder.target!.should be_a(Hash)
65
+ end
66
+
67
+ end
68
+
69
+ describe Builder::Hash, "#array_mode" do
70
+
71
+ it "should support <<" do
72
+ builder = Builder::Hash.new
73
+ # XML ::
74
+ # <root>
75
+ # <items>
76
+ # <item>
77
+ # <text>hello world 0</text>
78
+ # </item>
79
+ # <item>
80
+ # <text>hello world 1</text>
81
+ # </item>
82
+ # </items>
83
+ # </root>
84
+ builder.root do
85
+ builder.items do
86
+ builder.array_mode do
87
+ 2.times do |i|
88
+ _builder = Builder::Hash.new
89
+ builder << _builder.item do
90
+ _builder.text "hello world #{i}"
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
96
+ builder.target!.should == {
97
+ :items => [{:text => "hello world 0"}, {:text => "hello world 1"}]
98
+ }
99
+ end
100
+
101
+ it "should generate a new key if needed" do
102
+ builder = Builder::Hash.new
103
+ # XML ::
104
+ # <root>
105
+ # <items site="smart.fm">
106
+ # <item>
107
+ # <text>hello world 0</text>
108
+ # </item>
109
+ # <item>
110
+ # <text>hello world 1</text>
111
+ # </item>
112
+ # </items>
113
+ # </root>
114
+ builder.root do
115
+ builder.items(:site => "smart.fm") do
116
+ builder.array_mode do
117
+ 2.times do |i|
118
+ _builder = Builder::Hash.new
119
+ builder << _builder.item do
120
+ _builder.text "hello world #{i}"
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
126
+ builder.target!.should == {
127
+ :items => {
128
+ :entry => [{:text => "hello world 0"}, {:text=>"hello world 1"}],
129
+ :site=>"smart.fm"
130
+ }
131
+ }
132
+ end
133
+
134
+ it "should treat an empty block as a blank Array" do
135
+ # XML ::
136
+ # <root>
137
+ # <items>
138
+ # </items>
139
+ # </root>
140
+ builder = Builder::Hash.new
141
+ builder.root do
142
+ builder.items do
143
+ builder.array_mode do
144
+ end
145
+ end
146
+ end
147
+ builder.target!.should == {:items => []}
148
+ end
149
+
150
+ it "should only support the << operator for insertion"do
151
+ builder = Builder::Hash.new
152
+ # XML ::
153
+ # <root>
154
+ # <items>
155
+ # <item>
156
+ # <text>hello world 0</text>
157
+ # </item>
158
+ # <item>
159
+ # <text>hello world 1</text>
160
+ # </item>
161
+ # </items>
162
+ # </root>
163
+ succeeded = true
164
+ builder.root do
165
+ builder.items do
166
+ begin
167
+ builder.array_mode do
168
+ 2.times do
169
+ builder.item do
170
+ builder.text "hello world"
171
+ end
172
+ end
173
+ end
174
+ rescue
175
+ succeeded = false
176
+ end
177
+ end
178
+ end
179
+ succeeded.should_not be_true
180
+ end
181
+
182
+ end
@@ -0,0 +1,26 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+
3
+ describe Builder::Json, ".new" do
4
+ it "should be accessible" do
5
+ Builder::Hash.should respond_to(:new)
6
+ end
7
+ end
8
+
9
+ describe Builder::Json, "#target!" do
10
+
11
+ it "should return a String when there is only a root value" do
12
+ builder = Builder::Hash.new
13
+ builder.root("value")
14
+ builder.target!.should be_a(String)
15
+ end
16
+
17
+ it "should return a JSON string when root has deeper structure" do
18
+ builder = Builder::Json.new
19
+ builder.root do
20
+ builder.item("value")
21
+ end
22
+ builder.target!.should be_a(String)
23
+ builder.target!.should =="{\"item\":\"value\"}"
24
+ end
25
+
26
+ end
@@ -0,0 +1,19 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+
3
+ describe Builder::XmlMarkup, "#array_mode" do
4
+
5
+ it "should do nothing" do
6
+ builder = Builder::XmlMarkup.new
7
+ builder.root do
8
+ builder.array_mode do
9
+ builder.item("value")
10
+ end
11
+ end
12
+ builder2 = Builder::XmlMarkup.new
13
+ builder2.root do
14
+ builder2.item("value")
15
+ end
16
+ builder.target!.should == builder2.target!
17
+ end
18
+
19
+ end
data/spec/spec_helper.rb CHANGED
@@ -6,7 +6,8 @@ rescue LoadError
6
6
  require 'spec'
7
7
  end
8
8
  $LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")))
9
- require File.join(File.dirname(__FILE__), '..', 'lib', 'jsonbuilder')
9
+ require 'builder'
10
+ require 'jsonbuilder'
10
11
 
11
12
  def be_a(klass)
12
13
  be_is_a(klass)
@@ -15,4 +16,4 @@ end
15
16
  def rand_string(length = 100)
16
17
  chars = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
17
18
  Array.new(length){ chars[rand(chars.size)] }.join
18
- end
19
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonbuilder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - nov
@@ -9,10 +9,19 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-06 00:00:00 +09:00
12
+ date: 2009-06-08 00:00:00 +09:00
13
13
  default_executable:
14
- dependencies: []
15
-
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: builder
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
16
25
  description: Builder::XmlMarkup like JsonBuilder (Builder::JsonMarkup)
17
26
  email: nov@matake.jp
18
27
  executables: []
@@ -27,11 +36,16 @@ files:
27
36
  - ChangeLog
28
37
  - Rakefile
29
38
  - spec/builder
30
- - spec/builder/jsonmarkup_spec.rb
39
+ - spec/builder/hash_spec.rb
40
+ - spec/builder/json_spec.rb
41
+ - spec/builder/xml_markup_spec.rb
31
42
  - spec/jsonbuilder_spec.rb
32
43
  - spec/spec_helper.rb
33
44
  - lib/builder
34
- - lib/builder/jsonmarkup.rb
45
+ - lib/builder/abstract.rb
46
+ - lib/builder/hash.rb
47
+ - lib/builder/json.rb
48
+ - lib/builder/xml_markup.rb
35
49
  - lib/jsonbuilder.rb
36
50
  has_rdoc: true
37
51
  homepage: http://jsonbuilder.rubyforge.org
@@ -1,164 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
-
3
- describe Builder::JsonMarkup, ".new" do
4
- it "should be accessible" do
5
- Builder::JsonMarkup.should respond_to(:new)
6
- end
7
- end
8
-
9
- describe Builder::JsonMarkup do
10
-
11
- it "should remove root tag" do
12
- markup = Builder::JsonMarkup.new
13
- # XML :: <root><tag>value</tag></root>
14
- markup.root do
15
- markup.tag "value"
16
- end
17
- markup.target!.should == {:tag => "value"}
18
- end
19
-
20
- it "should not remove root tag when include_root is true" do
21
- markup = Builder::JsonMarkup.new(:include_root => true)
22
- # XML :: <root><tag>value</tag></root>
23
- markup.root do
24
- markup.tag "value"
25
- end
26
- markup.target!.should == {:root => {:tag => "value"}}
27
- end
28
-
29
- it "should use default_content_key when both content and attributes are exist" do
30
- markup = Builder::JsonMarkup.new
31
- # XML :: <root><tag id="1">value</tag></root>
32
- markup.root do
33
- markup.tag("value", :id => 1)
34
- end
35
- markup.target!.should == {:tag => {:id => 1, :content => "value"}}
36
- end
37
-
38
- it "should use default_content_key when both cdata! and attributes are exist" do
39
- markup = Builder::JsonMarkup.new
40
- # XML :: <root><tag id="1"><![CDATA[value]]></tag></root>
41
- markup.root do
42
- markup.tag(:id => 1) do
43
- markup.cdata! "value"
44
- end
45
- end
46
- markup.target!.should == {:tag => {:id => 1, :content => "value"}}
47
- end
48
-
49
- end
50
-
51
- describe Builder::JsonMarkup, "#array_mode" do
52
-
53
- it "should support <<" do
54
- markup = Builder::JsonMarkup.new
55
- # XML ::
56
- # <root>
57
- # <items>
58
- # <item>
59
- # <text>hello world 0</text>
60
- # </item>
61
- # <item>
62
- # <text>hello world 1</text>
63
- # </item>
64
- # </items>
65
- # </root>
66
- markup.root do
67
- markup.items do
68
- markup.array_mode do
69
- 2.times do |i|
70
- _markup = Builder::JsonMarkup.new
71
- markup << _markup.item do
72
- _markup.text "hello world #{i}"
73
- end
74
- end
75
- end
76
- end
77
- end
78
- markup.target!.should == {
79
- :items => [{:text => "hello world 0"}, {:text => "hello world 1"}]
80
- }
81
- end
82
-
83
- it "should generate new key if needed" do
84
- markup = Builder::JsonMarkup.new
85
- # XML ::
86
- # <root>
87
- # <items site="smart.fm">
88
- # <item>
89
- # <text>hello world 0</text>
90
- # </item>
91
- # <item>
92
- # <text>hello world 1</text>
93
- # </item>
94
- # </items>
95
- # </root>
96
- markup.root do
97
- markup.items(:site => "smart.fm") do
98
- markup.array_mode do
99
- 2.times do |i|
100
- _markup = Builder::JsonMarkup.new
101
- markup << _markup.item do
102
- _markup.text "hello world #{i}"
103
- end
104
- end
105
- end
106
- end
107
- end
108
- markup.target!.should == {
109
- :items => {
110
- :entries => [{:text => "hello world 0"}, {:text=>"hello world 1"}],
111
- :site=>"smart.fm"
112
- }
113
- }
114
- end
115
-
116
- it "should treat blank block as blank Array" do
117
- # XML ::
118
- # <root>
119
- # <items>
120
- # </items>
121
- # </root>
122
- markup = Builder::JsonMarkup.new
123
- markup.root do
124
- markup.items do
125
- markup.array_mode do
126
- end
127
- end
128
- end
129
- markup.target!.should == {:items => []}
130
- end
131
-
132
- it "should not support all methods except <<"do
133
- markup = Builder::JsonMarkup.new
134
- # XML ::
135
- # <root>
136
- # <items>
137
- # <item>
138
- # <text>hello world 0</text>
139
- # </item>
140
- # <item>
141
- # <text>hello world 1</text>
142
- # </item>
143
- # </items>
144
- # </root>
145
- succeeded = true
146
- markup.root do
147
- markup.items do
148
- begin
149
- markup.array_mode do
150
- 2.times do
151
- markup.item do
152
- markup.text "hello world"
153
- end
154
- end
155
- end
156
- rescue
157
- succeeded = false
158
- end
159
- end
160
- end
161
- succeeded.should_not be_true
162
- end
163
-
164
- end