rabl 0.1.1 → 0.1.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/README.md +16 -2
- data/lib/rabl/builder.rb +3 -1
- data/lib/rabl/engine.rb +2 -1
- data/lib/rabl/helpers.rb +10 -10
- data/lib/rabl/version.rb +1 -1
- data/test/builder_test.rb +1 -1
- metadata +3 -3
data/README.md
CHANGED
@@ -60,6 +60,14 @@ or even specify a root node label for the collection:
|
|
60
60
|
|
61
61
|
and this will be used as the default data for the rendering.
|
62
62
|
|
63
|
+
There can also be odd cases where the root-level of the response doesn't map directly to any object:
|
64
|
+
|
65
|
+
object false
|
66
|
+
code(:some_count) { |m| @user.posts.count }
|
67
|
+
child(@user) { attribute :name }
|
68
|
+
|
69
|
+
In those cases, object can be assigned to 'false' and child nodes can be constructed independently.
|
70
|
+
|
63
71
|
### Attributes ###
|
64
72
|
|
65
73
|
Basic usage of the templater to define a few simple attributes for the response:
|
@@ -80,13 +88,19 @@ or even multiple aliased attributes:
|
|
80
88
|
|
81
89
|
### Child Nodes ###
|
82
90
|
|
83
|
-
|
91
|
+
Often a response requires including nested information from data associated with the parent model:
|
92
|
+
|
93
|
+
child :address do
|
94
|
+
attributes :street, :city, :zip, :state
|
95
|
+
end
|
96
|
+
|
97
|
+
You can also add child nodes from an arbitrary data source:
|
84
98
|
|
85
99
|
child @posts => :foobar do
|
86
100
|
attributes :id, :title
|
87
101
|
end
|
88
102
|
|
89
|
-
or
|
103
|
+
or use model associations with an alias:
|
90
104
|
|
91
105
|
# Renders all the 'posts' association
|
92
106
|
# from the model into a node called 'foobar'
|
data/lib/rabl/builder.rb
CHANGED
@@ -66,10 +66,12 @@ module Rabl
|
|
66
66
|
# Creates a child node that is included in json output
|
67
67
|
# child(@user) { attribute :full_name }
|
68
68
|
# child(@user => :person) { ... }
|
69
|
+
# child(@users => :people) { ... }
|
69
70
|
def child(data, options={}, &block)
|
70
71
|
return false unless data.present?
|
71
72
|
name, object = data_name(data), data_object(data)
|
72
|
-
include_root = object.respond_to?(:each) # @users
|
73
|
+
include_root = object.respond_to?(:each) # child @users
|
74
|
+
object = { data_object(data) => data_name(data) } if data.respond_to?(:each_pair) # child :users => :people
|
73
75
|
@_result[name] = self.object_to_hash(object, :root => include_root, &block) if resolve_condition(options)
|
74
76
|
end
|
75
77
|
|
data/lib/rabl/engine.rb
CHANGED
@@ -28,7 +28,8 @@ module Rabl
|
|
28
28
|
if is_record?(data) || !data # object @user
|
29
29
|
Rabl::Builder.new(@_data, @_options).to_hash(options)
|
30
30
|
elsif data.respond_to?(:each) # collection @users
|
31
|
-
|
31
|
+
object_name = data_name(@_data).to_s.singularize # @users => :users
|
32
|
+
data.map { |object| Rabl::Builder.new({ object => object_name }, @_options).to_hash(options) }
|
32
33
|
end
|
33
34
|
end
|
34
35
|
|
data/lib/rabl/helpers.rb
CHANGED
@@ -26,11 +26,11 @@ module Rabl
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
#
|
30
|
-
#
|
31
|
-
|
32
|
-
|
33
|
-
|
29
|
+
# Renders a partial hash based on another rabl template
|
30
|
+
# partial("users/show", :object => @user)
|
31
|
+
def partial(file, options={}, &block)
|
32
|
+
source = self.fetch_source(file)
|
33
|
+
self.object_to_hash(options[:object], :source => source, &block)
|
34
34
|
end
|
35
35
|
|
36
36
|
# Returns a hash based representation of any data object given ejs template block
|
@@ -52,11 +52,11 @@ module Rabl
|
|
52
52
|
result
|
53
53
|
end
|
54
54
|
|
55
|
-
#
|
56
|
-
#
|
57
|
-
|
58
|
-
|
59
|
-
|
55
|
+
# Returns true if item is a ORM record; false otherwise
|
56
|
+
# is_record?(@user) => true
|
57
|
+
# is_record?([]) => false
|
58
|
+
def is_record?(obj)
|
59
|
+
obj && data_object(obj).respond_to?(:valid?)
|
60
60
|
end
|
61
61
|
|
62
62
|
# Returns source for a given relative file
|
data/lib/rabl/version.rb
CHANGED
data/test/builder_test.rb
CHANGED
@@ -106,7 +106,7 @@ context "Rabl::Builder" do
|
|
106
106
|
|
107
107
|
asserts "that it generates with a hash" do
|
108
108
|
b = builder @user, {}
|
109
|
-
mock(b).object_to_hash(@user,{ :root => false }).returns('xyz').subject
|
109
|
+
mock(b).object_to_hash({ @user => :user },{ :root => false }).returns('xyz').subject
|
110
110
|
|
111
111
|
b.child(@user => :user) { attribute :name }
|
112
112
|
get_result(b)
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: rabl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.1.
|
5
|
+
version: 0.1.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Nathan Esquenazi
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-04-
|
13
|
+
date: 2011-04-19 00:00:00 -07:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -113,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
113
113
|
requirements: []
|
114
114
|
|
115
115
|
rubyforge_project: rabl
|
116
|
-
rubygems_version: 1.
|
116
|
+
rubygems_version: 1.6.2
|
117
117
|
signing_key:
|
118
118
|
specification_version: 3
|
119
119
|
summary: General ruby templating for json or xml
|