rabl 0.0.3 → 0.0.4
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/README.md +80 -19
- data/lib/rabl/engine.rb +11 -1
- data/lib/rabl/version.rb +1 -1
- metadata +3 -3
data/README.md
CHANGED
@@ -14,23 +14,53 @@ This general templating system solves all of those problems.
|
|
14
14
|
|
15
15
|
## Installation ##
|
16
16
|
|
17
|
+
Install as a gem:
|
18
|
+
|
17
19
|
gem install rabl
|
18
20
|
|
21
|
+
or add to your Gemfile:
|
22
|
+
|
23
|
+
# Gemfile
|
24
|
+
gem 'rabl'
|
25
|
+
|
26
|
+
and run `bundle install` to install the dependency.
|
27
|
+
|
19
28
|
## Usage ##
|
20
29
|
|
30
|
+
### Object Assignment ###
|
31
|
+
|
32
|
+
To declare the data object to use in the template:
|
33
|
+
|
34
|
+
# app/views/users/show.json.rabl
|
35
|
+
object @user
|
36
|
+
|
37
|
+
or a collection works:
|
38
|
+
|
39
|
+
object @users
|
40
|
+
|
41
|
+
and this will be used as the default data object for the rendering.
|
42
|
+
|
43
|
+
### Attributes ###
|
44
|
+
|
21
45
|
Basic usage of the templater:
|
22
46
|
|
23
47
|
# app/views/users/show.json.rabl
|
24
48
|
attributes :id, :foo, :bar
|
25
49
|
|
26
|
-
|
50
|
+
or with aliased attributes:
|
27
51
|
|
28
|
-
#
|
29
|
-
|
30
|
-
|
31
|
-
|
52
|
+
# Take the value of model attribute `foo` and name the node `bar`
|
53
|
+
# { bar : 5 }
|
54
|
+
attribute :foo => :bar
|
55
|
+
|
56
|
+
or multiple aliased attributes:
|
57
|
+
|
58
|
+
# { baz : <bar value>, animal : <dog value> }
|
59
|
+
attributes :bar => :baz, :dog => :animal
|
32
60
|
|
33
|
-
|
61
|
+
### Child Nodes ###
|
62
|
+
|
63
|
+
You can also add children nodes from an arbitrary object:
|
34
64
|
|
35
65
|
child @posts => :foobar do
|
36
66
|
attributes :id, :title
|
@@ -42,34 +72,65 @@ or use existing model associations:
|
|
42
72
|
attributes :id, :title
|
43
73
|
end
|
44
74
|
|
45
|
-
|
75
|
+
### Glued Attributes ###
|
76
|
+
|
77
|
+
You can also append attributes to the root node:
|
78
|
+
|
79
|
+
glue @post do
|
80
|
+
attributes :id => :post_id, :name => :post_name
|
81
|
+
end
|
82
|
+
|
83
|
+
Use glue to add additional attributes to the parent object.
|
84
|
+
|
85
|
+
### Custom Nodes ###
|
86
|
+
|
87
|
+
This will generate a json response with the attributes specified. You can also include arbitrary code:
|
46
88
|
|
47
89
|
# app/views/users/show.json.rabl
|
48
|
-
|
49
|
-
|
90
|
+
code :full_name do |u|
|
91
|
+
u.first_name + " " + u.last_name
|
50
92
|
end
|
51
93
|
|
52
|
-
|
94
|
+
You can use custom "code" nodes to create flexible representations of a value utilizing data from the model.
|
95
|
+
|
96
|
+
### Partials ###
|
97
|
+
|
98
|
+
Often you need to access sub-objects in order to construct your own custom nodes for more complex associations. You can get access to the hash representation of another object:
|
53
99
|
|
54
100
|
code :location do
|
55
|
-
{ :
|
101
|
+
{ :city => @city, :address => partial("web/users/address", :object => @address) }
|
56
102
|
end
|
57
103
|
|
58
|
-
|
104
|
+
or an object associated to the parent model:
|
59
105
|
|
60
|
-
|
61
|
-
|
106
|
+
code :location do |m|
|
107
|
+
{ :city => m.city, :address => partial("web/users/address", :object => m.address) }
|
62
108
|
end
|
63
109
|
|
64
|
-
|
110
|
+
You can use these to construct arbitrarily complex nodes for APIs.
|
111
|
+
|
112
|
+
### Inheritance ###
|
113
|
+
|
114
|
+
Another common limitation of many json builders is code redundancy. Typically every representation of an object across endpoints share common attributes or nodes. The nodes for a 'post' object are probably the same or similar in most references throughout the various endpoints.
|
65
115
|
|
66
|
-
|
116
|
+
RABL has the ability to extend other "base" rabl templates and additional attributes:
|
67
117
|
|
68
|
-
|
69
|
-
|
70
|
-
|
118
|
+
# app/views/users/advanced.json.rabl
|
119
|
+
extends "users/base" # another RABL template in "app/views/users/base.json.rabl"
|
120
|
+
|
121
|
+
code :can_drink do |m|
|
122
|
+
m.age > 21
|
71
123
|
end
|
72
124
|
|
125
|
+
You can also extend other rabl templates in constructing nodes to reduce duplication:
|
126
|
+
|
127
|
+
# app/views/users/show.json.rabl
|
128
|
+
child @address do
|
129
|
+
extends "address/item"
|
130
|
+
end
|
131
|
+
|
132
|
+
Using partials and inheritance can significantly reduce code duplication in your templates.
|
133
|
+
|
73
134
|
## Issues ##
|
74
135
|
|
75
136
|
* I am sloppy and once again failed to unit test this. Don't use it in production until I do obviously.
|
data/lib/rabl/engine.rb
CHANGED
@@ -6,7 +6,8 @@ module Rabl
|
|
6
6
|
@_handler = handler
|
7
7
|
@_options = { :handler => @_handler, :vars => @_vars, :engine => self }
|
8
8
|
self.copy_instance_variables_from(@_handler, [:@assigns, :@helpers]);
|
9
|
-
@_object = vars[:object] ||
|
9
|
+
@_object = vars[:object] || self.default_object
|
10
|
+
# raise @user.inspect + " - " + @_handler.instance_variable_get(:@options).inspect + " - " + @_handler.inspect
|
10
11
|
instance_eval(source_string) if source_string.present?
|
11
12
|
instance_eval(&block) if block_given?
|
12
13
|
end
|
@@ -89,5 +90,14 @@ module Rabl
|
|
89
90
|
return object unless object.is_a?(ActiveRecord::Base) || object.first.is_a?(ActiveRecord::Base)
|
90
91
|
self.class.new(@_vars.merge(:object => object), @_handler, source, &block).to_hash(:root => false)
|
91
92
|
end
|
93
|
+
|
94
|
+
protected
|
95
|
+
|
96
|
+
# Returns a guess at the default object for this template
|
97
|
+
def default_object
|
98
|
+
@_handler.respond_to?(:controller) ?
|
99
|
+
instance_variable_get("@#{@_handler.controller.controller_name}") :
|
100
|
+
nil
|
101
|
+
end
|
92
102
|
end
|
93
103
|
end
|
data/lib/rabl/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rabl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Nathan Esquenazi
|