jsonit 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
- Copyright (C) 2011 by Klaas Speller
1
+ Copyright (C) 2011 by Voormedia B.V.
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -4,8 +4,13 @@ Jsonit provides a way to quickly construct handcrafted json documents.
4
4
  Jsonit requires objects to respond to `#to_json`.
5
5
  It will attempt to load the json gem if this is not the case.
6
6
 
7
+ ## Installation
8
+
9
+ `gem install jsonit`
10
+
7
11
  ## Usage
8
12
 
13
+
9
14
  ``` ruby
10
15
  require 'json' # Jsonit expects a #to_json method on object.
11
16
  require 'jsonit'
@@ -150,7 +155,7 @@ MyApp.controllers :photos do
150
155
  end
151
156
 
152
157
  get :show, :with => :photo_id, :provides => :json do
153
- render :'photo/show'
158
+ render :'photos/show'
154
159
  end
155
160
  end
156
161
  ```
@@ -162,6 +167,6 @@ Jsonit is currently under active development.
162
167
 
163
168
  ## LICENSE
164
169
 
165
- Json is Copyright (c) 2011 Klaas Speller and Voormedia and distributed under the MIT license. See the LICENCE file for more info.
170
+ Json is Copyright (c) 2011 Voormedia B.V. and distributed under the MIT license. See the LICENSE file for more info.
166
171
 
167
172
 
data/jsonit.gemspec CHANGED
@@ -17,6 +17,7 @@ Gem::Specification.new do |s|
17
17
  s.add_development_dependency "json"
18
18
  s.add_development_dependency "tilt"
19
19
  s.add_development_dependency "rspec"
20
+ s.add_development_dependency "mocha"
20
21
 
21
22
  s.files = `git ls-files`.split("\n")
22
23
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -1,51 +1,58 @@
1
1
  module Jsonit
2
2
  class Builder
3
3
 
4
- instance_methods.each { |m| undef_method(m) unless m.to_s =~ /^__|object_id/ }
4
+ instance_methods.each { |m| undef_method(m) unless m.to_s =~ /^__|object_id|\?$|!$/ }
5
5
 
6
- def initialize
7
- @document = scoped! do
8
- yield self if block_given?
9
- end
6
+ def initialize(scope={})
7
+ @document = @current_scope = scope
8
+ yield self if block_given?
10
9
  end
11
10
 
12
11
  def to_s
13
- @document.to_json
12
+ to_json
14
13
  end
15
14
 
16
- def to_json(*)
17
- to_s
15
+ def to_json(*args)
16
+ @document.to_json(*args)
18
17
  end
19
18
 
20
- def set!(key, value=nil, &blk)
21
- @current_scope[key] = if !block_given?
22
- value
23
- elsif value
19
+ def set!(key=nil, value=nil, &blk)
20
+ if !block_given?
21
+ assign!(key, value)
22
+ elsif value.is_a?(Enumerable)
24
23
  array!(key, value, &blk)
25
24
  else
26
- object!(key, &blk)
25
+ object!(key, value, &blk)
27
26
  end
27
+ self
28
28
  end
29
- alias_method :method_missing, :set!
30
-
31
- private
32
29
 
33
- def scoped!
30
+ def object!(key=nil, value=nil, &blk)
34
31
  @current_scope, previous_scope = {}, @current_scope
35
- yield
36
- @current_scope
37
- ensure
38
- @current_scope = previous_scope
32
+ yield value
33
+ @current_scope, new_scope = previous_scope, @current_scope
34
+ !key.nil? ? assign!(key, new_scope) : Builder.new(new_scope)
39
35
  end
40
-
41
- def object!(key, &blk)
42
- scoped!(&blk)
36
+
37
+ def array!(key, collection=[], &blk)
38
+ collection.map! { |itm| object!(nil, itm, &blk) } if block_given?
39
+ assign!(key, collection)
43
40
  end
44
41
 
45
- def array!(key, collection, &blk)
46
- collection.map do |itm|
47
- scoped! { blk.call(itm) }
42
+ def assign!(key, value=nil)
43
+ if value.is_a?(Hash) && @current_scope[key].is_a?(Hash)
44
+ @current_scope[key].merge!(value)
45
+ else
46
+ @current_scope[key] = value
47
+ end
48
+ self
49
+ end
50
+
51
+ def method_missing(meth, *args, &blk)
52
+ if (meth.to_s =~ /\?$|!$/).nil?
53
+ return set!(meth, *args, &blk)
48
54
  end
55
+ super
49
56
  end
50
57
  end
51
58
  end
@@ -1,3 +1,3 @@
1
1
  module Jsonit
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,64 +1,165 @@
1
1
  require "spec_helper"
2
2
  require "jsonit/builder"
3
3
 
4
- describe Jsonit::Builder, "Building json" do
5
- specify "a simple object" do
6
- Jsonit::Builder.new.to_json.should == %|{}|
7
- end
4
+ describe Jsonit::Builder do
5
+
6
+ describe ".new" do
7
+ it 'represents an empty object' do
8
+ Jsonit::Builder.new.to_json.should == %|{}|
9
+ end
10
+
11
+ it 'sets the root scope if provided' do
12
+ scope = { :foo => "bar" }
13
+ Jsonit::Builder.new(scope).to_json.should == %|{"foo":"bar"}|
14
+ end
8
15
 
9
- specify "a simple object with a string value" do
10
- Jsonit::Builder.new do |json|
11
- json.foo "bar"
12
- end.to_json.should == %|{"foo":"bar"}|
16
+ it 'evaluates the passed block' do
17
+ obj = mock()
18
+ obj.expects(:ping).once
19
+ Jsonit::Builder.new do |json|
20
+ obj.ping
21
+ end.to_json
22
+ end
13
23
  end
14
24
 
15
- specify "#to_json" do
16
- expect { Jsonit::Builder.new.to_json }.to_not raise_error
25
+ describe "#set!" do
26
+ let(:json) { Jsonit::Builder.new }
27
+
28
+ it 'returns an instance of self' do
29
+ json.set!.should be_a Jsonit::Builder
30
+ end
31
+
32
+ it 'sets a key value pair' do
33
+ json.set!(:foo, "bar").to_json.should == %|{"foo":"bar"}|
34
+ end
35
+
36
+ it 'sets value to null if not provided' do
37
+ json.set!(:foo).to_json.should == %|{"foo":null}|
38
+ end
39
+
40
+ it 'sets value to an object if block is provided' do
41
+ json.set!(:foo) { }.to_json.should == %|{"foo":{}}|
42
+ end
43
+
44
+ it 'sets values on nested object within block' do
45
+ json.set!(:foo) { json.set!(:a, "b") }.should == %|{"foo":{"a":"b"}}|
46
+ end
17
47
  end
18
48
 
19
- specify "a nested object" do
20
- Jsonit::Builder.new do |json|
21
- json.foo do
22
- json.bar "baz"
23
- end
49
+ describe "#object!" do
50
+ let(:json) { Jsonit::Builder.new }
24
51
 
25
- json.alpha do
26
- json.bravo "charlie"
27
- json.delta "echo"
52
+ it 'creates an object' do
53
+ json.object!(:foo) { }.to_json.should == %|{"foo":{}}|
54
+ end
28
55
 
29
- json.foxtrot do
30
- json.golf "hotel"
31
- end
32
- end
33
- end.to_json.should == %|{"foo":{"bar":"baz"},"alpha":{"bravo":"charlie","delta":"echo","foxtrot":{"golf":"hotel"}}}|
56
+ it 'passes value to the block' do
57
+ value = mock()
58
+ value.expects(:ping)
59
+ json.object!(:foo, value) { |val| val.ping }
60
+ end
61
+
62
+ it 'sets scope to the object' do
63
+ json.object!(:foo) { json.set!(:bar, "baz") }.to_json.should == %|{"foo":{"bar":"baz"}}|
64
+ end
65
+
66
+ it 'continues an object with same key' do
67
+ json.object!(:foo) { json.set!(:a, "b") }
68
+ json.object!(:foo) { json.set!(:c, "d") }
69
+ json.to_json.should == %|{"foo":{"a":"b","c":"d"}}|
70
+ end
34
71
  end
35
72
 
36
- specify "an array" do
37
- Jsonit::Builder.new do |json|
38
- json.foo [1, "bar", false, true]
39
- end.to_json.should == %|{"foo":[1,"bar",false,true]}|
73
+ describe "#array!" do
74
+ let(:json) { Jsonit::Builder.new }
75
+
76
+ it 'creates an array' do
77
+ json.array!(:foo).to_json.should == %|{"foo":[]}|
78
+ end
79
+
80
+ it 'sets value to array if no block is given' do
81
+ json.array!(:foo, ["a", 1, false]).to_json.should == %|{"foo":["a",1,false]}|
82
+ end
83
+
84
+ it 'executes block for each entry in the collection' do
85
+ obj1, obj2 = mock(), mock()
86
+ obj1.expects(:ping).once
87
+ obj2.expects(:ping).once
88
+
89
+ json.array!(:foo, [obj1, obj2]) { |obj| obj.ping }.to_json
90
+ end
91
+
92
+ it 'creates an object for each entry' do
93
+ json.array!(:foo, [1,"a"]) { |val| json.set!(:val, val) }.to_json.should == %|{"foo":[{"val":1},{"val":"a"}]}|
94
+ end
40
95
  end
41
96
 
42
- specify "explicit keys" do
43
- Jsonit::Builder.new do |json|
44
- json.set! :foo, "bar"
45
- end.to_json.should == %|{"foo":"bar"}|
97
+
98
+ describe "#assign!" do
99
+ let(:json) { Jsonit::Builder.new }
100
+
101
+ it 'returns an instance of self' do
102
+ json.assign!(:foo).should be_a Jsonit::Builder
103
+ end
104
+
105
+ it 'creates a key value pair' do
106
+ json.assign!(:foo, "bar").to_json.should == %|{"foo":"bar"}|
107
+ end
108
+
109
+ it 'can assign an array' do
110
+ json.assign!(:foo, ["a", "b", "c"]).to_json.should == %|{"foo":["a","b","c"]}|
111
+ end
112
+
113
+ it 'can assign an object' do
114
+ json.assign!(:foo, {"a" => "b"}).to_json.should == %|{"foo":{"a":"b"}}|
115
+ end
116
+
117
+ it 'continues an object with same key' do
118
+ json.assign!(:foo, {"a" => "b"})
119
+ json.assign!(:foo, {"c" => "d"})
120
+ json.to_json.should == %|{"foo":{"a":"b","c":"d"}}|
121
+ end
46
122
  end
47
-
48
- specify "explicit with block" do
49
- Jsonit::Builder.new do |json|
50
- json.set! :foo do
51
- json.set! :bar, "baz"
123
+
124
+ describe "ghost methods" do
125
+ let(:json) { Jsonit::Builder.new }
126
+
127
+ it 'does not raise method missing for method with format /^[a-z][a-z0-9]+$/' do
128
+ [:a, :b, :c].each do |m|
129
+ expect { json.send(m) }.to_not raise_error
52
130
  end
53
- end.to_json.should == %|{"foo":{"bar":"baz"}}|
54
- end
131
+ end
55
132
 
56
- specify "collections with a block" do
57
- Jsonit::Builder.new do |json|
58
- json.foos ["bar", "baz"] do |item|
59
- json.value item
133
+
134
+ it 'raises method missing for ending with a "?"' do
135
+ [:a?, :b?, :c?].each do |m|
136
+ expect { eval("json.#{m.to_s}") }.to raise_error(NoMethodError)
137
+ end
138
+ end
139
+
140
+ it 'raises method missing for ending with a "!"' do
141
+ [:a!, :b!, :c!].each do |m|
142
+ expect { eval("json.#{m.to_s}") }.to raise_error(NoMethodError)
60
143
  end
61
- json.foo "bar"
62
- end.to_json.should == %|{"foos":[{"value":"bar"},{"value":"baz"}],"foo":"bar"}|
144
+ end
145
+ end
146
+
147
+ describe "DSL" do
148
+ it "can nest multiple levels" do
149
+ Jsonit::Builder.new do |json|
150
+ json.foo do
151
+ json.bar "baz"
152
+ end
153
+
154
+ json.alpha do
155
+ json.bravo "charlie"
156
+ json.delta "echo"
157
+
158
+ json.foxtrot do
159
+ json.golf "hotel"
160
+ end
161
+ end
162
+ end.to_json.should == %|{"foo":{"bar":"baz"},"alpha":{"bravo":"charlie","delta":"echo","foxtrot":{"golf":"hotel"}}}|
163
+ end
63
164
  end
64
165
  end
data/spec/spec_helper.rb CHANGED
@@ -3,3 +3,8 @@ $:.unshift File.dirname(__FILE__)
3
3
 
4
4
  require "json"
5
5
 
6
+ RSpec.configure do |config|
7
+ config.mock_with :mocha
8
+ end
9
+
10
+
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: jsonit
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Klaas Speller
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-04-21 00:00:00 +02:00
13
+ date: 2011-04-26 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -46,6 +46,17 @@ dependencies:
46
46
  version: "0"
47
47
  type: :development
48
48
  version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: mocha
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ type: :development
59
+ version_requirements: *id004
49
60
  description: Create JSON documents with ease
50
61
  email:
51
62
  - k.speller@voormedia.com
@@ -58,7 +69,7 @@ extra_rdoc_files: []
58
69
  files:
59
70
  - .gitignore
60
71
  - Gemfile
61
- - LICENCE
72
+ - LICENSE
62
73
  - README.md
63
74
  - Rakefile
64
75
  - jsonit.gemspec