rabl 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -0
- data/lib/rabl/builder.rb +3 -1
- data/lib/rabl/engine.rb +18 -7
- data/lib/rabl/helpers.rb +1 -1
- data/lib/rabl/version.rb +1 -1
- data/test/builder_test.rb +11 -2
- metadata +3 -3
data/README.md
CHANGED
@@ -209,6 +209,7 @@ Thanks to [Miso](http://gomiso.com) for allowing me to create this for our appli
|
|
209
209
|
* [Nathan Esquenazi](https://github.com/nesquena) - Creator of the project
|
210
210
|
* [Arthur Chiu](https://github.com/achiu) - Core Maintainer, Riot Testing Guru
|
211
211
|
* [Tim Lee](https://github.com/timothy1ee) - RABL is an awesome name and was chosen by the Miso CTO.
|
212
|
+
* [Rick Thomas](https://github.com/rickthomasjr) - Added options passing for extends and Sinatra testing
|
212
213
|
|
213
214
|
More to come hopefully! Please fork and contribute, any help is appreciated!
|
214
215
|
|
data/lib/rabl/builder.rb
CHANGED
@@ -3,6 +3,8 @@ module Rabl
|
|
3
3
|
include Rabl::Helpers
|
4
4
|
|
5
5
|
# Constructs a new ejs hash based on given object and options
|
6
|
+
# options = { :format => "json", :attributes, :root => true,
|
7
|
+
# :child_root => true, :code, :child, :glue, :extends }
|
6
8
|
def initialize(data, options={}, &block)
|
7
9
|
@options = options
|
8
10
|
@_scope = options[:scope]
|
@@ -70,7 +72,7 @@ module Rabl
|
|
70
72
|
def child(data, options={}, &block)
|
71
73
|
return false unless data.present?
|
72
74
|
name, object = data_name(data), data_object(data)
|
73
|
-
include_root = object.respond_to?(:each) # child @users
|
75
|
+
include_root = object.respond_to?(:each) && @options[:child_root] # child @users
|
74
76
|
object = { object => name } if data.respond_to?(:each_pair) && object # child :users => :people
|
75
77
|
@_result[name] = self.object_to_hash(object, :root => include_root, &block) if resolve_condition(options)
|
76
78
|
end
|
data/lib/rabl/engine.rb
CHANGED
@@ -6,7 +6,7 @@ module Rabl
|
|
6
6
|
# Rabl::Engine.new("...source...", { :format => "xml", :root => true, :view_path => "/path/to/views" })
|
7
7
|
def initialize(source, options={})
|
8
8
|
@_source = source
|
9
|
-
@_options = options
|
9
|
+
@_options = options
|
10
10
|
end
|
11
11
|
|
12
12
|
# Renders the representation based on source, object, scope and locals
|
@@ -15,6 +15,7 @@ module Rabl
|
|
15
15
|
@_locals, @_scope = locals, scope
|
16
16
|
self.copy_instance_variables_from(@_scope, [:@assigns, :@helpers])
|
17
17
|
@_options[:scope] = @_scope
|
18
|
+
@_options[:format] ||= default_format
|
18
19
|
@_data = locals[:object] || self.default_object
|
19
20
|
instance_eval(@_source) if @_source.present?
|
20
21
|
instance_eval(&block) if block_given?
|
@@ -22,29 +23,30 @@ module Rabl
|
|
22
23
|
end
|
23
24
|
|
24
25
|
# Returns a hash representation of the data object
|
25
|
-
# to_hash(:root => true)
|
26
|
+
# to_hash(:root => true, :child_root => true)
|
26
27
|
def to_hash(options={})
|
28
|
+
options = options.reverse_merge(@_options)
|
27
29
|
data = data_object(@_data)
|
28
30
|
if is_record?(data) || !data # object @user
|
29
|
-
Rabl::Builder.new(@_data,
|
31
|
+
Rabl::Builder.new(@_data, options).to_hash(options)
|
30
32
|
elsif data.respond_to?(:each) # collection @users
|
31
33
|
object_name = data_name(@_data).to_s.singularize # @users => :users
|
32
|
-
data.map { |object| Rabl::Builder.new({ object => object_name },
|
34
|
+
data.map { |object| Rabl::Builder.new({ object => object_name }, options).to_hash(options) }
|
33
35
|
end
|
34
36
|
end
|
35
37
|
|
36
38
|
# Returns a json representation of the data object
|
37
39
|
# to_json(:root => true)
|
38
40
|
def to_json(options={})
|
39
|
-
options = options.reverse_merge(:root => true)
|
41
|
+
options = options.reverse_merge(:root => true, :child_root => true)
|
40
42
|
result = @_collection_name ? { @_collection_name => to_hash(options) } : to_hash(options)
|
41
43
|
result.to_json
|
42
44
|
end
|
43
45
|
|
44
|
-
# Returns
|
46
|
+
# Returns an xml representation of the data object
|
45
47
|
# to_xml(:root => true)
|
46
48
|
def to_xml(options={})
|
47
|
-
options = options.reverse_merge(:root => false)
|
49
|
+
options = options.reverse_merge(:root => false, :child_root => false)
|
48
50
|
to_hash(options).to_xml(:root => data_name(@_data))
|
49
51
|
end
|
50
52
|
|
@@ -124,5 +126,14 @@ module Rabl
|
|
124
126
|
instance_variable_get("@#{@_scope.controller.controller_name}") :
|
125
127
|
nil
|
126
128
|
end
|
129
|
+
|
130
|
+
# Returns a guess at the format in this scope
|
131
|
+
# default_format => "xml"
|
132
|
+
def default_format
|
133
|
+
format = @_scope.respond_to?(:params) && @_scope.params.has_key?(:format) ?
|
134
|
+
@_scope.params[:format] :
|
135
|
+
nil
|
136
|
+
format || "json"
|
137
|
+
end
|
127
138
|
end
|
128
139
|
end
|
data/lib/rabl/helpers.rb
CHANGED
@@ -29,7 +29,7 @@ module Rabl
|
|
29
29
|
# Renders a partial hash based on another rabl template
|
30
30
|
# partial("users/show", :object => @user)
|
31
31
|
def partial(file, options={}, &block)
|
32
|
-
source = self.fetch_source(file)
|
32
|
+
source = self.fetch_source(file, options)
|
33
33
|
self.object_to_hash(options[:object], :source => source, &block)
|
34
34
|
end
|
35
35
|
|
data/lib/rabl/version.rb
CHANGED
data/test/builder_test.rb
CHANGED
@@ -128,14 +128,23 @@ context "Rabl::Builder" do
|
|
128
128
|
get_result(b)
|
129
129
|
end.equivalent_to({ :user => 'xyz'})
|
130
130
|
|
131
|
-
asserts "that it generates with an collection" do
|
132
|
-
b = builder @user, {}
|
131
|
+
asserts "that it generates with an collection and child_root" do
|
132
|
+
b = builder @user, { :child_root => true }
|
133
133
|
mock(b).data_name(@users) { :users }
|
134
134
|
mock(b).object_to_hash(@users,{ :root => true }).returns('xyz').subject
|
135
135
|
|
136
136
|
b.child(@users) { attribute :name }
|
137
137
|
get_result(b)
|
138
138
|
end.equivalent_to({ :users => 'xyz'})
|
139
|
+
|
140
|
+
asserts "that it generates with an collection and no child root" do
|
141
|
+
b = builder @user, { :child_root => false }
|
142
|
+
mock(b).data_name(@users) { :users }
|
143
|
+
mock(b).object_to_hash(@users,{ :root => false }).returns('xyz').subject
|
144
|
+
|
145
|
+
b.child(@users) { attribute :name }
|
146
|
+
get_result(b)
|
147
|
+
end.equivalent_to({ :users => 'xyz'})
|
139
148
|
end
|
140
149
|
|
141
150
|
context "#glue" do
|
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.5
|
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-27 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.5.2
|
117
117
|
signing_key:
|
118
118
|
specification_version: 3
|
119
119
|
summary: General ruby templating for json or xml
|