activejson 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +47 -2
- data/lib/activejson/activejson_core.rb +0 -1
- data/lib/activejson/version.rb +1 -1
- metadata +4 -4
data/README.md
CHANGED
@@ -8,7 +8,7 @@ Uses yajl to encode the JSON to text for extra speed.
|
|
8
8
|
|
9
9
|
##Installation
|
10
10
|
|
11
|
-
Installation is easy, just add `gem activejson` to your Gemfile and let bundle do the rest. Alternatively `gem install activejson` will install the gem globally on your system.
|
11
|
+
Installation is easy, just add `gem "activejson"` to your Gemfile and let bundle do the rest. Alternatively `gem install activejson` will install the gem globally on your system.
|
12
12
|
|
13
13
|
##Example
|
14
14
|
|
@@ -37,7 +37,7 @@ And, assuming your `@user` object has a name, an age and an array of pets, this
|
|
37
37
|
name : "frank",
|
38
38
|
age : {
|
39
39
|
human_years : 14,
|
40
|
-
animal_years 2
|
40
|
+
animal_years : 2
|
41
41
|
}
|
42
42
|
},
|
43
43
|
{
|
@@ -52,3 +52,48 @@ And, assuming your `@user` object has a name, an age and an array of pets, this
|
|
52
52
|
}
|
53
53
|
|
54
54
|
...except not-pretty printed (for speed).
|
55
|
+
|
56
|
+
##Usage
|
57
|
+
|
58
|
+
###Introduction
|
59
|
+
|
60
|
+
In its simplest use case, ActiveJSON allows the view code to create JSON labels using (almost) any name, just by calling the label name as a method on an ActiveJSON object.
|
61
|
+
|
62
|
+
All data that you want to be added to your JSON should be added to the top-level `json` object, which is created for you by the ActiveJSON framework when the view code is run.
|
63
|
+
|
64
|
+
For example, to give a name and a date, use the following:
|
65
|
+
|
66
|
+
json.name "Steve"
|
67
|
+
json.date "12 Oct 2011"
|
68
|
+
|
69
|
+
This is sufficient for simple, flat JSON data blocks but will never create arrays or JSON objects within other objects.
|
70
|
+
|
71
|
+
###Sub-Objects
|
72
|
+
|
73
|
+
To mark a JSON label as being a JSON object, any function called on an ActiveJSON object can be called with a block, taking one parameter. Within the block, the parameter can be used to add labels to the JSON sub-object.
|
74
|
+
|
75
|
+
For example:
|
76
|
+
|
77
|
+
json.object do |obj|
|
78
|
+
obj.name "Widget 1"
|
79
|
+
obj.flavour "Lemon"
|
80
|
+
end
|
81
|
+
json.title "Objects"
|
82
|
+
|
83
|
+
Will render:
|
84
|
+
|
85
|
+
{ object : { name : "Widget 1", flavour : "Lemon" }, title : "Objects" }
|
86
|
+
|
87
|
+
###Arrays
|
88
|
+
|
89
|
+
To mark a JSON label as containing an array, pass any object that responds to `map` as an argument to the label's function and supply a block taking two parameters, the first of which will be the ActiveJSON object for the array element and the second of which will be the corresponding element from the argument.
|
90
|
+
|
91
|
+
For example, if `@users` contains Steve and Phil, then:
|
92
|
+
|
93
|
+
json.users(@users) do |user_j, user|
|
94
|
+
user_j.name user.name
|
95
|
+
end
|
96
|
+
|
97
|
+
Will render to:
|
98
|
+
|
99
|
+
{ users : [ { name : "Steve" }, { name : "Phil" } ] }
|
data/lib/activejson/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activejson
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Andy Caldwell
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-09-
|
18
|
+
date: 2011-09-27 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: multi_json
|