|
59 | 59 | end |
60 | 60 | end |
61 | 61 |
|
| 62 | + describe '.root' do |
| 63 | + context 'with singular and plural root keys' do |
| 64 | + before(:each) do |
| 65 | + subject.root 'things', 'thing' |
| 66 | + end |
| 67 | + |
| 68 | + context 'with a single object' do |
| 69 | + it 'should allow a root element name to be specified' do |
| 70 | + representation = subject.represent(Object.new) |
| 71 | + representation.should be_kind_of(Hash) |
| 72 | + representation.should have_key('thing') |
| 73 | + representation['thing'].should be_kind_of(subject) |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + context 'with an array of objects' do |
| 78 | + it 'should allow a root element name to be specified' do |
| 79 | + representation = subject.represent(4.times.map{Object.new}) |
| 80 | + representation.should be_kind_of(Hash) |
| 81 | + representation.should have_key('things') |
| 82 | + representation['things'].should be_kind_of(Array) |
| 83 | + representation['things'].size.should == 4 |
| 84 | + representation['things'].reject{|r| r.kind_of?(subject)}.should be_empty |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + context 'it can be overridden' do |
| 89 | + it 'can be disabled' do |
| 90 | + representation = subject.represent(4.times.map{Object.new}, :root=>false) |
| 91 | + representation.should be_kind_of(Array) |
| 92 | + representation.size.should == 4 |
| 93 | + representation.reject{|r| r.kind_of?(subject)}.should be_empty |
| 94 | + end |
| 95 | + it 'can use a different name' do |
| 96 | + representation = subject.represent(4.times.map{Object.new}, :root=>'others') |
| 97 | + representation.should be_kind_of(Hash) |
| 98 | + representation.should have_key('others') |
| 99 | + representation['others'].should be_kind_of(Array) |
| 100 | + representation['others'].size.should == 4 |
| 101 | + representation['others'].reject{|r| r.kind_of?(subject)}.should be_empty |
| 102 | + end |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + context 'with singular root key' do |
| 107 | + before(:each) do |
| 108 | + subject.root nil, 'thing' |
| 109 | + end |
| 110 | + |
| 111 | + context 'with a single object' do |
| 112 | + it 'should allow a root element name to be specified' do |
| 113 | + representation = subject.represent(Object.new) |
| 114 | + representation.should be_kind_of(Hash) |
| 115 | + representation.should have_key('thing') |
| 116 | + representation['thing'].should be_kind_of(subject) |
| 117 | + end |
| 118 | + end |
| 119 | + |
| 120 | + context 'with an array of objects' do |
| 121 | + it 'should allow a root element name to be specified' do |
| 122 | + representation = subject.represent(4.times.map{Object.new}) |
| 123 | + representation.should be_kind_of(Array) |
| 124 | + representation.size.should == 4 |
| 125 | + representation.reject{|r| r.kind_of?(subject)}.should be_empty |
| 126 | + end |
| 127 | + end |
| 128 | + end |
| 129 | + |
| 130 | + context 'with plural root key' do |
| 131 | + before(:each) do |
| 132 | + subject.root 'things' |
| 133 | + end |
| 134 | + |
| 135 | + context 'with a single object' do |
| 136 | + it 'should allow a root element name to be specified' do |
| 137 | + subject.represent(Object.new).should be_kind_of(subject) |
| 138 | + end |
| 139 | + end |
| 140 | + |
| 141 | + context 'with an array of objects' do |
| 142 | + it 'should allow a root element name to be specified' do |
| 143 | + representation = subject.represent(4.times.map{Object.new}) |
| 144 | + representation.should be_kind_of(Hash) |
| 145 | + representation.should have_key('things') |
| 146 | + representation['things'].should be_kind_of(Array) |
| 147 | + representation['things'].size.should == 4 |
| 148 | + representation['things'].reject{|r| r.kind_of?(subject)}.should be_empty |
| 149 | + end |
| 150 | + end |
| 151 | + end |
| 152 | + end |
| 153 | + |
62 | 154 | describe '#initialize' do |
63 | 155 | it 'should take an object and an optional options hash' do |
64 | 156 | expect{ subject.new(Object.new) }.not_to raise_error |
|
77 | 169 | context 'instance methods' do |
78 | 170 | let(:model){ mock(attributes) } |
79 | 171 | let(:attributes){ { |
80 | | - :name => 'Bob Bobson', |
| 172 | + :name => 'Bob Bobson', |
81 | 173 | |
82 | 174 | :friends => [ |
83 | | - mock(:name => "Friend 1", :email => '[email protected]', :friends => []), |
| 175 | + mock(:name => "Friend 1", :email => '[email protected]', :friends => []), |
84 | 176 | mock(:name => "Friend 2", :email => '[email protected]', :friends => []) |
85 | 177 | ] |
86 | 178 | } } |
|
119 | 211 | rep.last.serializable_hash[:name].should == 'Friend 2' |
120 | 212 | end |
121 | 213 |
|
| 214 | + it 'should disable root key name for child representations' do |
| 215 | + class FriendEntity < Grape::Entity |
| 216 | + root 'friends', 'friend' |
| 217 | + expose :name, :email |
| 218 | + end |
| 219 | + fresh_class.class_eval do |
| 220 | + expose :friends, :using => FriendEntity |
| 221 | + end |
| 222 | + rep = subject.send(:value_for, :friends) |
| 223 | + rep.should be_kind_of(Array) |
| 224 | + rep.reject{|r| r.is_a?(FriendEntity)}.should be_empty |
| 225 | + rep.first.serializable_hash[:name].should == 'Friend 1' |
| 226 | + rep.last.serializable_hash[:name].should == 'Friend 2' |
| 227 | + end |
| 228 | + |
122 | 229 | it 'should call through to the proc if there is one' do |
123 | 230 | subject.send(:value_for, :computed, :awesome => 123).should == 123 |
124 | 231 | end |
|
0 commit comments