rabl 0.6.12 → 0.6.13

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.6.13
4
+
5
+ * Small tweak to is_collection detection (look for each and map)
6
+ * Adds `include_child_root` configuration option (Thanks @yoon)
7
+
3
8
  ## 0.6.12
4
9
 
5
10
  * Fix view_path options for renderer (Thanks @ivanvanderbyl and @route)
data/README.md CHANGED
@@ -110,7 +110,8 @@ Rabl.configure do |config|
110
110
  # config.include_msgpack_root = true
111
111
  # config.include_bson_root = true
112
112
  # config.include_plist_root = true
113
- # config.include_xml_root = false
113
+ # config.include_xml_root = false
114
+ # config.include_child_root = true
114
115
  # config.enable_json_callbacks = false
115
116
  # config.xml_options = { :dasherize => true, :skip_types => false }
116
117
  # config.view_paths = []
@@ -118,8 +119,11 @@ end
118
119
  ```
119
120
 
120
121
  Each option specifies behavior related to RABL's output. If `include_json_root` is disabled that removes the
121
- root node for each child in the output, and `enable_json_callbacks` enables support for 'jsonp' style callback
122
- output if the incoming request has a 'callback' parameter.
122
+ root node for each root object in the output, and `enable_json_callbacks` enables support for 'jsonp' style callback
123
+ output if the incoming request has a 'callback' parameter.
124
+
125
+ If `include_child_root` is set to false then child objects in the response will not include
126
+ a root node by default. This allows you to further fine-tune your desired response structure.
123
127
 
124
128
  If `cache_sources` is set to `true`, template lookups will be cached for improved performance.
125
129
  The cache can be reset manually by running `Rabl.reset_source_cache!` within your application.
@@ -23,6 +23,7 @@ module Rabl
23
23
  # Rabl.host
24
24
  class Configuration
25
25
  attr_accessor :include_json_root
26
+ attr_accessor :include_child_root
26
27
  attr_accessor :include_msgpack_root
27
28
  attr_accessor :include_plist_root
28
29
  attr_accessor :include_xml_root
@@ -43,6 +44,7 @@ module Rabl
43
44
 
44
45
  def initialize
45
46
  @include_json_root = true
47
+ @include_child_root = true
46
48
  @include_msgpack_root = true
47
49
  @include_plist_root = true
48
50
  @include_xml_root = false
@@ -54,7 +54,8 @@ module Rabl
54
54
  # to_json(:root => true)
55
55
  def to_json(options={})
56
56
  include_root = Rabl.configuration.include_json_root
57
- options = options.reverse_merge(:root => include_root, :child_root => include_root)
57
+ include_child_root = Rabl.configuration.include_child_root
58
+ options = options.reverse_merge(:root => include_root, :child_root => include_child_root)
58
59
  result = collection_root_name ? { collection_root_name => to_hash(options) } : to_hash(options)
59
60
  format_json(result)
60
61
  end
@@ -63,7 +64,8 @@ module Rabl
63
64
  # to_msgpack(:root => true)
64
65
  def to_msgpack(options={})
65
66
  include_root = Rabl.configuration.include_msgpack_root
66
- options = options.reverse_merge(:root => include_root, :child_root => include_root)
67
+ include_child_root = Rabl.configuration.include_child_root
68
+ options = options.reverse_merge(:root => include_root, :child_root => include_child_root)
67
69
  result = collection_root_name ? { collection_root_name => to_hash(options) } : to_hash(options)
68
70
  Rabl.configuration.msgpack_engine.pack result
69
71
  end
@@ -73,7 +75,8 @@ module Rabl
73
75
  # to_plist(:root => true)
74
76
  def to_plist(options={})
75
77
  include_root = Rabl.configuration.include_plist_root
76
- options = options.reverse_merge(:root => include_root, :child_root => include_root)
78
+ include_child_root = Rabl.configuration.include_child_root
79
+ options = options.reverse_merge(:root => include_root, :child_root => include_child_root)
77
80
  result = defined?(@_collection_name) ? { @_collection_name => to_hash(options) } : to_hash(options)
78
81
  Rabl.configuration.plist_engine.dump(result)
79
82
  end
@@ -82,7 +85,8 @@ module Rabl
82
85
  # to_xml(:root => true)
83
86
  def to_xml(options={})
84
87
  include_root = Rabl.configuration.include_xml_root
85
- options = options.reverse_merge(:root => include_root, :child_root => include_root)
88
+ include_child_root = include_root && Rabl.configuration.include_child_root
89
+ options = options.reverse_merge(:root => include_root, :child_root => include_child_root)
86
90
  xml_options = Rabl.configuration.default_xml_options.merge(:root => data_name(@_data))
87
91
  to_hash(options).to_xml(xml_options)
88
92
  end
@@ -91,7 +95,8 @@ module Rabl
91
95
  # to_bson(:root => true)
92
96
  def to_bson(options={})
93
97
  include_root = Rabl.configuration.include_bson_root
94
- options = options.reverse_merge(:root => include_root, :child_root => include_root)
98
+ include_child_root = Rabl.configuration.include_child_root
99
+ options = options.reverse_merge(:root => include_root, :child_root => include_child_root)
95
100
  result = if collection_root_name
96
101
  { collection_root_name => to_hash(options) }
97
102
  elsif is_collection?(@_data) && @_data.is_a?(Array)
@@ -50,17 +50,17 @@ module Rabl
50
50
  end
51
51
  end
52
52
 
53
- # Returns true if obj is not enumerable
53
+ # Returns true if obj is not a collection
54
54
  # is_object?(@user) => true
55
55
  # is_object?([]) => false
56
56
  # is_object?({}) => false
57
57
  def is_object?(obj)
58
- obj && !data_object(obj).respond_to?(:map)
58
+ obj && (!data_object(obj).respond_to?(:map) || !data_object(obj).respond_to?(:each))
59
59
  end
60
60
 
61
61
  # Returns true if the obj is a collection of items
62
62
  def is_collection?(obj)
63
- obj && data_object(obj).respond_to?(:map)
63
+ obj && data_object(obj).respond_to?(:map) && data_object(obj).respond_to?(:each)
64
64
  end
65
65
 
66
66
  # Returns the scope wrapping this engine, used for retrieving data, invoking methods, etc
@@ -1,3 +1,3 @@
1
1
  module Rabl
2
- VERSION = "0.6.12"
2
+ VERSION = "0.6.13"
3
3
  end
@@ -8,6 +8,7 @@ context 'Rabl::Configuration' do
8
8
  setup { Rabl.configuration }
9
9
 
10
10
  asserts(:include_json_root).equals true
11
+ asserts(:include_child_root).equals true
11
12
  asserts(:include_xml_root).equals false
12
13
  asserts(:enable_json_callbacks).equals false
13
14
  asserts(:view_paths).equals []
@@ -475,4 +475,31 @@ context "Rabl::Engine" do
475
475
  Rabl.reset_configuration!
476
476
  end
477
477
  end
478
+
479
+ context "without child root" do
480
+ setup do
481
+ Rabl.configure do |config|
482
+ config.include_child_root = false
483
+ config.include_xml_root = false
484
+ config.enable_json_callbacks = false
485
+ end
486
+ end
487
+
488
+ context "#child" do
489
+
490
+ asserts "that it can create a child node without child root" do
491
+ template = rabl %{
492
+ child @users
493
+ }
494
+ scope = Object.new
495
+ scope.instance_variable_set :@users, [User.new, User.new]
496
+ template.render(scope)
497
+ end.equals "{\"users\":[{},{}]}"
498
+
499
+ end
500
+
501
+ teardown do
502
+ Rabl.reset_configuration!
503
+ end
504
+ end
478
505
  end
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.6.12
4
+ version: 0.6.13
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: 2012-05-20 00:00:00.000000000 Z
12
+ date: 2012-06-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport