rabl 0.5.5.h → 0.5.5.i
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +3 -1
- data/README.md +1 -1
- data/lib/rabl/builder.rb +2 -2
- data/lib/rabl/helpers.rb +3 -3
- data/lib/rabl/partials.rb +2 -1
- data/lib/rabl/version.rb +1 -1
- data/test/helpers_test.rb +24 -3
- data/test/models/user.rb +9 -7
- metadata +3 -3
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
-
## 0.5.5.a-
|
3
|
+
## 0.5.5.a-i
|
4
4
|
|
5
5
|
* Change engine to only instantiate one builder when rendering a collection
|
6
6
|
* Alias to\_msgpack to to\_mpac
|
@@ -10,6 +10,8 @@
|
|
10
10
|
* Adds a 'object_root' option to collection (thanks @blakewatters)
|
11
11
|
* Adds a 'root_name' option to collection
|
12
12
|
* Adds PList format support (thanks @alzeih)
|
13
|
+
* Fixes infinite recursion in edge case calculating object root name
|
14
|
+
* Fixes issue with nameless node that has an array result
|
13
15
|
|
14
16
|
## 0.5.4
|
15
17
|
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# RABL #
|
2
2
|
|
3
|
-
RABL (Ruby API Builder Language) is a Rails and [Padrino](http://padrinorb.com) ruby templating system for generating JSON, XML, MessagePack and BSON. When using the ActiveRecord 'to_json' method, I tend to quickly find myself wanting a more expressive and powerful solution for generating APIs.
|
3
|
+
RABL (Ruby API Builder Language) is a Rails and [Padrino](http://padrinorb.com) ruby templating system for generating JSON, XML, MessagePack, PList and BSON. When using the ActiveRecord 'to_json' method, I tend to quickly find myself wanting a more expressive and powerful solution for generating APIs.
|
4
4
|
This is especially frustrating when the JSON representation is complex or doesn't match the exact schema defined in the database.
|
5
5
|
|
6
6
|
I wanted a simple and flexible system for generating my APIs. In particular, I wanted to easily:
|
data/lib/rabl/builder.rb
CHANGED
@@ -73,8 +73,8 @@ module Rabl
|
|
73
73
|
result = block.call(@_object)
|
74
74
|
if name.present?
|
75
75
|
@_result[name] = result
|
76
|
-
|
77
|
-
@_result.merge!(result)
|
76
|
+
elsif result.respond_to?(:each_pair) # merge hash into root hash
|
77
|
+
@_result.merge!(result)
|
78
78
|
end
|
79
79
|
end
|
80
80
|
alias_method :code, :node
|
data/lib/rabl/helpers.rb
CHANGED
@@ -14,15 +14,15 @@ module Rabl
|
|
14
14
|
# data_name(data) => "user"
|
15
15
|
# data_name(@user => :person) => :person
|
16
16
|
# data_name(@users) => :user
|
17
|
-
# data_name([@user]) => "
|
17
|
+
# data_name([@user]) => "users"
|
18
18
|
# data_name([]) => "array"
|
19
19
|
def data_name(data)
|
20
20
|
return nil unless data # nil or false
|
21
21
|
return data.values.first if data.is_a?(Hash) # @user => :user
|
22
22
|
data = @_object.send(data) if data.is_a?(Symbol) && @_object # :address
|
23
|
-
if data.respond_to?(:first)
|
23
|
+
if is_collection?(data) && data.respond_to?(:first) # data collection
|
24
24
|
data_name(data.first).to_s.pluralize if data.first.present?
|
25
|
-
|
25
|
+
elsif is_object?(data) # actual data object
|
26
26
|
object_name = object_root_name if object_root_name
|
27
27
|
object_name ||= collection_root_name.to_s.singularize if collection_root_name
|
28
28
|
object_name ||= data.class.respond_to?(:model_name) ? data.class.model_name.element : data.class.to_s.downcase
|
data/lib/rabl/partials.rb
CHANGED
@@ -9,7 +9,6 @@ module Rabl
|
|
9
9
|
def partial(file, options={}, &block)
|
10
10
|
raise ArgumentError, "Must provide an :object option to render a partial" unless options.has_key?(:object)
|
11
11
|
object, view_path = options.delete(:object), options.delete(:view_path)
|
12
|
-
return if object.blank?
|
13
12
|
source, location = self.fetch_source(file, :view_path => view_path)
|
14
13
|
engine_options = options.merge(:source => source, :source_location => location)
|
15
14
|
self.object_to_hash(object, engine_options, &block)
|
@@ -18,10 +17,12 @@ module Rabl
|
|
18
17
|
# Returns a hash based representation of any data object given ejs template block
|
19
18
|
# object_to_hash(@user) { attribute :full_name } => { ... }
|
20
19
|
# object_to_hash(@user, :source => "...") { attribute :full_name } => { ... }
|
20
|
+
# object_to_hash([@user], :source => "...") { attribute :full_name } => { ... }
|
21
21
|
# options must have :source (rabl file contents)
|
22
22
|
# options can have :source_location (source filename)
|
23
23
|
def object_to_hash(object, options={}, &block)
|
24
24
|
return object unless is_object?(object) || is_collection?(object)
|
25
|
+
return [] if is_collection?(object) && object.blank? # empty collection
|
25
26
|
engine_options = options.merge(:format => "hash", :root => (options[:root] || false))
|
26
27
|
Rabl::Engine.new(options[:source], engine_options).render(@_scope, :object => object, &block)
|
27
28
|
end
|
data/lib/rabl/version.rb
CHANGED
data/test/helpers_test.rb
CHANGED
@@ -2,10 +2,31 @@ require 'tmpdir'
|
|
2
2
|
require 'pathname'
|
3
3
|
require File.expand_path('../../lib/rabl', __FILE__)
|
4
4
|
|
5
|
-
class
|
5
|
+
class TestHelperMethods
|
6
6
|
include Rabl::Helpers
|
7
7
|
end
|
8
8
|
|
9
9
|
context "Rabl::Helpers" do
|
10
|
-
|
11
|
-
|
10
|
+
setup do
|
11
|
+
@helper_class = TestHelperMethods.new
|
12
|
+
@user = User.new
|
13
|
+
end
|
14
|
+
|
15
|
+
context "for data_name method" do
|
16
|
+
asserts "returns nil if no data" do
|
17
|
+
@helper_class.data_name(nil)
|
18
|
+
end.equals(nil)
|
19
|
+
|
20
|
+
asserts "returns alias if hash with symbol is passed" do
|
21
|
+
@helper_class.data_name(@user => :user)
|
22
|
+
end.equals(:user)
|
23
|
+
|
24
|
+
asserts "returns name of first object of a collection" do
|
25
|
+
@helper_class.data_name([@user, @user])
|
26
|
+
end.equals('users')
|
27
|
+
|
28
|
+
asserts "returns name of an object" do
|
29
|
+
@helper_class.data_name(@user)
|
30
|
+
end.equals('user')
|
31
|
+
end
|
32
|
+
end
|
data/test/models/user.rb
CHANGED
@@ -1,14 +1,16 @@
|
|
1
1
|
class User
|
2
|
-
attr_accessor :age, :city, :name
|
2
|
+
attr_accessor :age, :city, :name, :first
|
3
3
|
|
4
|
-
DEFAULT_AGE
|
5
|
-
DEFAULT_CITY
|
6
|
-
DEFAULT_NAME
|
4
|
+
DEFAULT_AGE = 24
|
5
|
+
DEFAULT_CITY = 'irvine'
|
6
|
+
DEFAULT_NAME = 'rabl'
|
7
|
+
DEFAULT_FIRST = 'bob'
|
7
8
|
|
8
9
|
def initialize(attributes={})
|
9
|
-
self.age
|
10
|
-
self.city
|
11
|
-
self.name
|
10
|
+
self.age = attributes[:age] || DEFAULT_AGE
|
11
|
+
self.city = attributes[:city] || DEFAULT_CITY
|
12
|
+
self.name = attributes[:name] || DEFAULT_NAME
|
13
|
+
self.first = attributes[:first] || DEFAULT_FIRST
|
12
14
|
end
|
13
15
|
end
|
14
16
|
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: rabl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease: 6
|
5
|
-
version: 0.5.5.
|
5
|
+
version: 0.5.5.i
|
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: 2012-02-
|
13
|
+
date: 2012-02-26 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: multi_json
|
@@ -312,7 +312,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
312
312
|
requirements: []
|
313
313
|
|
314
314
|
rubyforge_project: rabl
|
315
|
-
rubygems_version: 1.8.
|
315
|
+
rubygems_version: 1.8.15
|
316
316
|
signing_key:
|
317
317
|
specification_version: 3
|
318
318
|
summary: General ruby templating with json, bson, xml and msgpack support
|