rabl 0.8.3 → 0.8.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +4 -0
- data/CHANGELOG.md +4 -0
- data/README.md +3 -0
- data/lib/rabl/builder.rb +8 -0
- data/lib/rabl/configuration.rb +22 -20
- data/lib/rabl/version.rb +1 -1
- data/test/builder_test.rb +13 -0
- data/test/configuration_test.rb +11 -0
- data/test/models/user.rb +4 -6
- metadata +4 -4
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -137,6 +137,7 @@ Rabl.configure do |config|
|
|
137
137
|
# config.xml_options = { :dasherize => true, :skip_types => false }
|
138
138
|
# config.view_paths = []
|
139
139
|
# config.raise_on_missing_attribute = true # Defaults to false
|
140
|
+
# config.replace_nil_values_with_empty_strings = true # Defaults to false
|
140
141
|
end
|
141
142
|
```
|
142
143
|
|
@@ -171,6 +172,8 @@ attempts to render an attribute that does not exist. Otherwise, the attribute wi
|
|
171
172
|
Setting this to true during development may help increase the robustness of your code, but using `true` in
|
172
173
|
production code is not recommended.
|
173
174
|
|
175
|
+
If `replace_nil_values_with_empty_strings` is set to `true`, all values that are `nil` and would normally be displayed as `null` in the response are converted to empty strings.
|
176
|
+
|
174
177
|
If you wish to use [oj](https://github.com/ohler55/oj) as
|
175
178
|
the primary JSON encoding engine simply add that to your Gemfile:
|
176
179
|
|
data/lib/rabl/builder.rb
CHANGED
@@ -57,6 +57,14 @@ module Rabl
|
|
57
57
|
@_root_name = nil
|
58
58
|
end
|
59
59
|
|
60
|
+
# Replace nil values with empty strings if configured
|
61
|
+
if Rabl.configuration.replace_nil_values_with_empty_strings
|
62
|
+
@_result = @_result.inject({}) do |hash, (k, v)|
|
63
|
+
hash[k] = v.nil? ? '' : v
|
64
|
+
hash
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
60
68
|
# Return Results
|
61
69
|
@_root_name ? { @_root_name => @_result } : @_result
|
62
70
|
end
|
data/lib/rabl/configuration.rb
CHANGED
@@ -47,30 +47,32 @@ module Rabl
|
|
47
47
|
attr_accessor :cache_engine
|
48
48
|
attr_accessor :raise_on_missing_attribute
|
49
49
|
attr_accessor :perform_caching
|
50
|
+
attr_accessor :replace_nil_values_with_empty_strings
|
50
51
|
|
51
52
|
DEFAULT_XML_OPTIONS = { :dasherize => true, :skip_types => false }
|
52
53
|
|
53
54
|
def initialize
|
54
|
-
@include_json_root
|
55
|
-
@include_child_root
|
56
|
-
@include_msgpack_root
|
57
|
-
@include_plist_root
|
58
|
-
@include_xml_root
|
59
|
-
@include_bson_root
|
60
|
-
@enable_json_callbacks
|
61
|
-
@bson_check_keys
|
62
|
-
@bson_move_id
|
63
|
-
@json_engine
|
64
|
-
@msgpack_engine
|
65
|
-
@bson_engine
|
66
|
-
@plist_engine
|
67
|
-
@xml_options
|
68
|
-
@cache_sources
|
69
|
-
@cache_all_output
|
70
|
-
@escape_all_output
|
71
|
-
@view_paths
|
72
|
-
@cache_engine
|
73
|
-
@perform_caching
|
55
|
+
@include_json_root = true
|
56
|
+
@include_child_root = true
|
57
|
+
@include_msgpack_root = true
|
58
|
+
@include_plist_root = true
|
59
|
+
@include_xml_root = false
|
60
|
+
@include_bson_root = true
|
61
|
+
@enable_json_callbacks = false
|
62
|
+
@bson_check_keys = false
|
63
|
+
@bson_move_id = false
|
64
|
+
@json_engine = nil
|
65
|
+
@msgpack_engine = nil
|
66
|
+
@bson_engine = nil
|
67
|
+
@plist_engine = nil
|
68
|
+
@xml_options = {}
|
69
|
+
@cache_sources = false
|
70
|
+
@cache_all_output = false
|
71
|
+
@escape_all_output = false
|
72
|
+
@view_paths = []
|
73
|
+
@cache_engine = Rabl::CacheEngine.new
|
74
|
+
@perform_caching = false
|
75
|
+
@replace_nil_values_with_empty_strings = false
|
74
76
|
end
|
75
77
|
|
76
78
|
# @return The JSON engine used to encode Rabl templates into JSON
|
data/lib/rabl/version.rb
CHANGED
data/test/builder_test.rb
CHANGED
@@ -42,6 +42,19 @@ context "Rabl::Builder" do
|
|
42
42
|
topic.build(User.new, :root => false)
|
43
43
|
end.equivalent_to({ :name => "rabl" })
|
44
44
|
end
|
45
|
+
|
46
|
+
context "when nil values are replaced with empty strings" do
|
47
|
+
setup do
|
48
|
+
Rabl.configuration.replace_nil_values_with_empty_strings = true
|
49
|
+
builder({ :attributes => { :name => {} } })
|
50
|
+
end
|
51
|
+
asserts "that an empty string is returned as the value" do
|
52
|
+
topic.build(User.new(:name => nil))
|
53
|
+
end.equivalent_to({ :name => "" })
|
54
|
+
teardown do
|
55
|
+
Rabl.configuration.replace_nil_values_with_empty_strings = false
|
56
|
+
end
|
57
|
+
end
|
45
58
|
end
|
46
59
|
|
47
60
|
context "#attribute" do
|
data/test/configuration_test.rb
CHANGED
@@ -13,6 +13,7 @@ context 'Rabl::Configuration' do
|
|
13
13
|
asserts(:view_paths).equals []
|
14
14
|
asserts(:json_engine).equals { json_engine }
|
15
15
|
asserts(:cache_engine).is_a?(Rabl::CacheEngine)
|
16
|
+
asserts(:replace_nil_values_with_empty_strings).equals false
|
16
17
|
end
|
17
18
|
|
18
19
|
context 'custom JSON engine configured as Symbol' do
|
@@ -44,4 +45,14 @@ context 'Rabl::Configuration' do
|
|
44
45
|
|
45
46
|
asserts(:raise_on_missing_attribute).equals true
|
46
47
|
end # raise on missing
|
48
|
+
|
49
|
+
context 'replace nil values with empty strings' do
|
50
|
+
setup do
|
51
|
+
Rabl.configure do |c|
|
52
|
+
c.replace_nil_values_with_empty_strings = true
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
asserts(:replace_nil_values_with_empty_strings).equals true
|
57
|
+
end # replace nil values with empty strings
|
47
58
|
end
|
data/test/models/user.rb
CHANGED
@@ -10,12 +10,10 @@ unless defined?(User)
|
|
10
10
|
DEFAULT_HOBBIES = ['Photography']
|
11
11
|
|
12
12
|
def initialize(attributes={})
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
self.
|
17
|
-
self.float = attributes[:float] || DEFAULT_FLOAT
|
18
|
-
self.hobbies = (attributes[:hobbies] || DEFAULT_HOBBIES).map { |h| Hobby.new(h) }
|
13
|
+
%w(age city name first float hobbies).each do |attr|
|
14
|
+
self.send "#{attr}=", (attributes.has_key?(attr.to_sym) ? attributes[attr.to_sym] : self.class.const_get("DEFAULT_#{attr.upcase}"))
|
15
|
+
end
|
16
|
+
self.hobbies = self.hobbies.map { |h| Hobby.new(h) }
|
19
17
|
end
|
20
18
|
end
|
21
19
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rabl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-04-
|
12
|
+
date: 2013-04-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -421,7 +421,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
421
421
|
version: '0'
|
422
422
|
segments:
|
423
423
|
- 0
|
424
|
-
hash:
|
424
|
+
hash: 501966820991429896
|
425
425
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
426
426
|
none: false
|
427
427
|
requirements:
|
@@ -430,7 +430,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
430
430
|
version: '0'
|
431
431
|
segments:
|
432
432
|
- 0
|
433
|
-
hash:
|
433
|
+
hash: 501966820991429896
|
434
434
|
requirements: []
|
435
435
|
rubyforge_project: rabl
|
436
436
|
rubygems_version: 1.8.25
|