jsonbuilder 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -34,10 +34,19 @@ RDOC_OPTS = [
34
34
  task :default => [:spec]
35
35
  task :package => [:clean]
36
36
 
37
- Rake::TestTask.new("spec") do |t|
38
- t.libs << "spec"
39
- t.pattern = "spec/**/*_spec.rb"
40
- t.verbose = true
37
+ begin
38
+ require 'spec/rake/spectask'
39
+
40
+ Spec::Rake::SpecTask.new('spec') do |t|
41
+ t.spec_opts = ["-f specdoc", "-c"]
42
+ t.spec_files = FileList['spec/*_spec.rb', 'spec/builder/*_spec.rb']
43
+ end
44
+
45
+ rescue LoadError
46
+ desc 'Spec rake task not available'
47
+ task :spec do
48
+ abort 'Spec rake task is not available. Be sure to install rspec as a gem or plugin'
49
+ end
41
50
  end
42
51
 
43
52
  spec = Gem::Specification.new do |s|
@@ -1,5 +1,4 @@
1
1
  module Builder
2
-
3
2
  class Abstract
4
3
  def nil?
5
4
  false
@@ -1,5 +1,5 @@
1
1
  module Builder
2
- class Hash < Abstract
2
+ class HashStructure < Abstract
3
3
 
4
4
  def initialize(options = {})
5
5
  # @default_content_key is used in such case: markup.key(value, :attr_key => attr_value)
@@ -12,6 +12,7 @@ module Builder
12
12
 
13
13
  # NOTICE: you have to call this method to use array in json
14
14
  def array_mode(key = nil, &block)
15
+ raise RuntimeError.new("cannot call inside array_mode block") if @array_mode
15
16
  @array_mode = true
16
17
  if eval("#{_current}").is_a?(::Hash)
17
18
  key ||= :entry
@@ -44,6 +45,7 @@ module Builder
44
45
  end
45
46
 
46
47
  def text!(text)
48
+ raise RuntimeError.new("cannot call inside array_mode block") if @array_mode
47
49
  if eval("#{_current}").is_a?(::Hash)
48
50
  eval("#{_current}.merge!({@default_content_key => text})")
49
51
  else
@@ -1,8 +1,7 @@
1
1
  require 'json'
2
2
 
3
3
  module Builder
4
-
5
- class Json < Hash
4
+ class JsonFormat < HashStructure
6
5
 
7
6
  def initialize(options = {})
8
7
  # @default_content_key is used in such case: markup.key(value, :attr_key => attr_value)
@@ -21,5 +20,4 @@ module Builder
21
20
  end
22
21
  end
23
22
  end
24
-
25
23
  end
@@ -1,3 +1,5 @@
1
+ require 'builder/xmlmarkup'
2
+
1
3
  module Builder
2
4
  class XmlMarkup
3
5
 
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 = 6
5
+ REVISION = 7
6
6
  class << self
7
7
  def to_version
8
8
  "#{MAJOR}.#{MINOR}.#{REVISION}"
@@ -16,6 +16,6 @@ module JsonBuilder
16
16
  end
17
17
 
18
18
  require 'builder/abstract'
19
- require 'builder/hash'
19
+ require 'builder/hash_structure'
20
20
  require 'builder/xml_markup'
21
- require 'builder/json'
21
+ require 'builder/json_format'
@@ -1,15 +1,15 @@
1
1
  require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
2
 
3
- describe Builder::Hash, ".new" do
3
+ describe Builder::HashStructure, ".new" do
4
4
  it "should be accessible" do
5
- Builder::Hash.should respond_to(:new)
5
+ Builder::HashStructure.should respond_to(:new)
6
6
  end
7
7
  end
8
8
 
9
- describe Builder::Hash do
9
+ describe Builder::HashStructure do
10
10
 
11
11
  it "should remove the root tag" do
12
- builder = Builder::Hash.new
12
+ builder = Builder::HashStructure.new
13
13
  # XML :: <root><tag>value</tag></root>
14
14
  builder.root do
15
15
  builder.tag "value"
@@ -18,7 +18,7 @@ describe Builder::Hash do
18
18
  end
19
19
 
20
20
  it "should not remove the root tag when include_root is true" do
21
- builder = Builder::Hash.new(:include_root => true)
21
+ builder = Builder::HashStructure.new(:include_root => true)
22
22
  # XML :: <root><tag>value</tag></root>
23
23
  builder.root do
24
24
  builder.tag "value"
@@ -27,7 +27,7 @@ describe Builder::Hash do
27
27
  end
28
28
 
29
29
  it "should use the default_content_key when both content and attributes exist" do
30
- builder = Builder::Hash.new
30
+ builder = Builder::HashStructure.new
31
31
  # XML :: <root><tag id="1">value</tag></root>
32
32
  builder.root do
33
33
  builder.tag("value", :id => 1)
@@ -36,7 +36,7 @@ describe Builder::Hash do
36
36
  end
37
37
 
38
38
  it "should use the default_content_key when both cdata! and attributes exist" do
39
- builder = Builder::Hash.new
39
+ builder = Builder::HashStructure.new
40
40
  # XML :: <root><tag id="1"><![CDATA[value]]></tag></root>
41
41
  builder.root do
42
42
  builder.tag(:id => 1) do
@@ -48,28 +48,36 @@ describe Builder::Hash do
48
48
 
49
49
  end
50
50
 
51
- describe Builder::Hash, "#target!" do
51
+ describe Builder::HashStructure, "#target!" do
52
52
 
53
53
  it "should return a String when there is only a root value" do
54
- builder = Builder::Hash.new
54
+ builder = Builder::HashStructure.new
55
55
  builder.root("value")
56
- builder.target!.should be_a(String)
56
+ builder.target!.should == "value"
57
57
  end
58
58
 
59
- it "should return Hash when root has deeper structure" do
60
- builder = Builder::Hash.new
59
+ it "should return a HashStructure when there is only a root value and include_root option is true" do
60
+ builder = Builder::HashStructure.new(:include_root => true)
61
+ # XML :: <root>value</root>
62
+ builder.root "value"
63
+ builder.target!.should == {:root => "value"}
64
+ end
65
+
66
+
67
+ it "should return a HashStructure when root has deeper structure" do
68
+ builder = Builder::HashStructure.new
61
69
  builder.root do
62
70
  builder.item("value")
63
71
  end
64
- builder.target!.should be_a(Hash)
72
+ builder.target!.should == {:item => 'value'}
65
73
  end
66
74
 
67
75
  end
68
76
 
69
- describe Builder::Hash, "#array_mode" do
77
+ describe Builder::HashStructure, "#array_mode" do
70
78
 
71
- it "should support <<" do
72
- builder = Builder::Hash.new
79
+ it "should support <<(hash)" do
80
+ builder = Builder::HashStructure.new
73
81
  # XML ::
74
82
  # <root>
75
83
  # <items>
@@ -85,7 +93,7 @@ describe Builder::Hash, "#array_mode" do
85
93
  builder.items do
86
94
  builder.array_mode do
87
95
  2.times do |i|
88
- _builder = Builder::Hash.new
96
+ _builder = Builder::HashStructure.new
89
97
  builder << _builder.item do
90
98
  _builder.text "hello world #{i}"
91
99
  end
@@ -99,7 +107,7 @@ describe Builder::Hash, "#array_mode" do
99
107
  end
100
108
 
101
109
  it "should generate a new key if needed" do
102
- builder = Builder::Hash.new
110
+ builder = Builder::HashStructure.new
103
111
  # XML ::
104
112
  # <root>
105
113
  # <items site="smart.fm">
@@ -115,7 +123,7 @@ describe Builder::Hash, "#array_mode" do
115
123
  builder.items(:site => "smart.fm") do
116
124
  builder.array_mode do
117
125
  2.times do |i|
118
- _builder = Builder::Hash.new
126
+ _builder = Builder::HashStructure.new
119
127
  builder << _builder.item do
120
128
  _builder.text "hello world #{i}"
121
129
  end
@@ -137,7 +145,7 @@ describe Builder::Hash, "#array_mode" do
137
145
  # <items>
138
146
  # </items>
139
147
  # </root>
140
- builder = Builder::Hash.new
148
+ builder = Builder::HashStructure.new
141
149
  builder.root do
142
150
  builder.items do
143
151
  builder.array_mode do
@@ -147,36 +155,64 @@ describe Builder::Hash, "#array_mode" do
147
155
  builder.target!.should == {:items => []}
148
156
  end
149
157
 
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
158
+ it "should raise error if tag methods (method_missing) is used inside block"do
159
+ builder = Builder::HashStructure.new
164
160
  builder.root do
165
161
  builder.items do
166
- begin
162
+ lambda do
163
+ builder.array_mode do
164
+ builder.item("hello world")
165
+ end
166
+ end.should raise_error
167
+ lambda do
167
168
  builder.array_mode do
168
- 2.times do
169
- builder.item do
170
- builder.text "hello world"
171
- end
169
+ builder.item do
170
+ builder.text("hello world")
172
171
  end
173
172
  end
174
- rescue
175
- succeeded = false
176
- end
173
+ end.should raise_error
174
+ end
175
+ end
176
+ end
177
+
178
+ it "should raise error if tag! is used inside block"do
179
+ builder = Builder::HashStructure.new
180
+ builder.root do
181
+ builder.items do
182
+ lambda do
183
+ builder.array_mode do
184
+ builder.tag!("item", "item1")
185
+ end
186
+ end.should raise_error
187
+ end
188
+ end
189
+ end
190
+
191
+ it "should raise error if cdata! (or text!) is used inside block"do
192
+ builder = Builder::HashStructure.new
193
+ builder.root do
194
+ builder.items do
195
+ lambda do
196
+ builder.array_mode do
197
+ builder.cdata!("text")
198
+ end
199
+ end.should raise_error
200
+ end
201
+ end
202
+ end
203
+
204
+ it "should raise error if array_mode is used inside block"do
205
+ builder = Builder::HashStructure.new
206
+ builder.root do
207
+ builder.items do
208
+ lambda do
209
+ builder.array_mode do
210
+ builder.array_mode do
211
+ end
212
+ end
213
+ end.should raise_error
177
214
  end
178
215
  end
179
- succeeded.should_not be_true
180
216
  end
181
217
 
182
218
  end
@@ -1,21 +1,21 @@
1
1
  require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
2
 
3
- describe Builder::Json, ".new" do
3
+ describe Builder::JsonFormat, ".new" do
4
4
  it "should be accessible" do
5
- Builder::Hash.should respond_to(:new)
5
+ Builder::JsonFormat.should respond_to(:new)
6
6
  end
7
7
  end
8
8
 
9
- describe Builder::Json, "#target!" do
9
+ describe Builder::JsonFormat, "#target!" do
10
10
 
11
11
  it "should return a String when there is only a root value" do
12
- builder = Builder::Hash.new
12
+ builder = Builder::JsonFormat.new
13
13
  builder.root("value")
14
14
  builder.target!.should be_a(String)
15
15
  end
16
16
 
17
17
  it "should return a JSON string when root has deeper structure" do
18
- builder = Builder::Json.new
18
+ builder = Builder::JsonFormat.new
19
19
  builder.root do
20
20
  builder.item("value")
21
21
  end
data/spec/spec_helper.rb CHANGED
@@ -1,19 +1,5 @@
1
- begin
2
- require 'spec'
3
- rescue LoadError
4
- require 'rubygems'
5
- gem 'rspec'
6
- require 'spec'
7
- end
1
+ require 'rubygems'
2
+ require 'spec'
3
+
8
4
  $LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")))
9
- require 'builder'
10
5
  require 'jsonbuilder'
11
-
12
- def be_a(klass)
13
- be_is_a(klass)
14
- end
15
-
16
- def rand_string(length = 100)
17
- chars = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
18
- Array.new(length){ chars[rand(chars.size)] }.join
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.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - nov
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-08 00:00:00 +09:00
12
+ date: 2009-06-11 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -36,15 +36,15 @@ files:
36
36
  - ChangeLog
37
37
  - Rakefile
38
38
  - spec/builder
39
- - spec/builder/hash_spec.rb
40
- - spec/builder/json_spec.rb
39
+ - spec/builder/hash_structure_spec.rb
40
+ - spec/builder/json_format_spec.rb
41
41
  - spec/builder/xml_markup_spec.rb
42
42
  - spec/jsonbuilder_spec.rb
43
43
  - spec/spec_helper.rb
44
44
  - lib/builder
45
45
  - lib/builder/abstract.rb
46
- - lib/builder/hash.rb
47
- - lib/builder/json.rb
46
+ - lib/builder/hash_structure.rb
47
+ - lib/builder/json_format.rb
48
48
  - lib/builder/xml_markup.rb
49
49
  - lib/jsonbuilder.rb
50
50
  has_rdoc: true