json-response 0.0.1 → 0.0.2
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/lib/json_response.rb +28 -5
- data/spec/json_response_spec.rb +9 -3
- metadata +6 -5
data/lib/json_response.rb
CHANGED
@@ -2,19 +2,42 @@ require "ostruct"
|
|
2
2
|
require "json"
|
3
3
|
|
4
4
|
module JSONResponse
|
5
|
-
VERSION = "0.0.
|
5
|
+
VERSION = "0.0.2"
|
6
6
|
extend self
|
7
7
|
|
8
|
-
def
|
9
|
-
|
8
|
+
def counters
|
9
|
+
@counters ||= Hash.new(0)
|
10
|
+
end
|
11
|
+
|
12
|
+
def next(name)
|
13
|
+
counters[name] += 1
|
14
|
+
end
|
15
|
+
|
16
|
+
class JSONStruct < OpenStruct
|
17
|
+
def initialize(&block)
|
18
|
+
super
|
19
|
+
@block = block
|
20
|
+
end
|
21
|
+
|
22
|
+
def next(name)
|
23
|
+
JSONResponse.next(name)
|
24
|
+
end
|
25
|
+
|
26
|
+
def build
|
27
|
+
self.tap { |s| @block.call(s) }.marshal_dump
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def define(name, &block)
|
32
|
+
registry[name] = JSONStruct.new(&block)
|
10
33
|
end
|
11
34
|
|
12
35
|
def build(name, options={})
|
13
|
-
JSON.generate(registry[name].
|
36
|
+
JSON.generate(registry[name].build.merge(options))
|
14
37
|
end
|
15
38
|
|
16
39
|
def attributes_for(name, options={})
|
17
|
-
registry[name].
|
40
|
+
registry[name].build.merge(options)
|
18
41
|
end
|
19
42
|
|
20
43
|
private
|
data/spec/json_response_spec.rb
CHANGED
@@ -2,14 +2,20 @@ require File.dirname(__FILE__) + '/spec_helper'
|
|
2
2
|
|
3
3
|
describe JSONResponse, ".build" do
|
4
4
|
before do
|
5
|
-
JSONResponse.define(:person) { |p| p.name = "Dave" }
|
5
|
+
JSONResponse.define(:person) { |p| p.id = JSONResponse.next(:id); p.name = "Dave" }
|
6
6
|
end
|
7
7
|
|
8
8
|
it "builds json strings from definitions" do
|
9
|
-
JSONResponse.build(:person).should ==
|
9
|
+
JSONResponse.build(:person).should == '{"id":1,"name":"Dave"}'
|
10
10
|
end
|
11
11
|
|
12
12
|
it "builds a hash of attributes from definitions" do
|
13
|
-
JSONResponse.attributes_for(:person).should == {name: "Dave"}
|
13
|
+
JSONResponse.attributes_for(:person).should == {id: 2, name: "Dave"}
|
14
|
+
end
|
15
|
+
|
16
|
+
it "supports counters" do
|
17
|
+
count = JSONResponse.counters[:id]
|
18
|
+
JSONResponse.attributes_for(:person)[:id].should == count + 1
|
19
|
+
JSONResponse.attributes_for(:person)[:id].should == count + 2
|
14
20
|
end
|
15
21
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json-response
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-
|
12
|
+
date: 2011-11-29 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &2157080820 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: 2.6.0
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2157080820
|
25
25
|
description: Define factories to be built as JSON.
|
26
26
|
email:
|
27
27
|
- me@mattenoble.com
|
@@ -56,10 +56,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
56
56
|
version: '0'
|
57
57
|
requirements: []
|
58
58
|
rubyforge_project:
|
59
|
-
rubygems_version: 1.8.
|
59
|
+
rubygems_version: 1.8.10
|
60
60
|
signing_key:
|
61
61
|
specification_version: 3
|
62
62
|
summary: Define factories to be built as JSON.
|
63
63
|
test_files:
|
64
64
|
- spec/json_response_spec.rb
|
65
65
|
- spec/spec_helper.rb
|
66
|
+
has_rdoc:
|