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 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" } ] }
@@ -6,7 +6,6 @@ MultiJson.engine = :yajl
6
6
  module ActiveJson
7
7
  class Core
8
8
  instance_methods.each {|m| undef_method(m) unless %w(__id__ __send__ to_json instance_eval nil? is_a? class).include?(m.to_s)}
9
- attr_accessor :hash
10
9
 
11
10
  def initialize()
12
11
  @hash = {}
@@ -1,3 +1,3 @@
1
1
  module Activejson
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
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: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
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-23 00:00:00 Z
18
+ date: 2011-09-27 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: multi_json