|
24 | 24 | expect{ subject.expose :name, :email, :as => :foo }.to raise_error(ArgumentError)
|
25 | 25 | expect{ subject.expose :name, :as => :foo }.not_to raise_error
|
26 | 26 | end
|
| 27 | + |
| 28 | + it 'should make sure that :format_with as a proc can not be used with a block' do |
| 29 | + expect { subject.expose :name, :format_with => Proc.new {} do |object,options| end }.to raise_error(ArgumentError) |
| 30 | + end |
27 | 31 | end
|
28 | 32 |
|
29 | 33 | context 'with a block' do
|
|
67 | 71 | child_class.exposures[:name].should have_key :proc
|
68 | 72 | end
|
69 | 73 | end
|
| 74 | + |
| 75 | + context 'register formatters' do |
| 76 | + let(:date_formatter) { lambda {|date| date.strftime('%m/%d/%Y') }} |
| 77 | + |
| 78 | + it 'should register a formatter' do |
| 79 | + subject.format_with :timestamp, &date_formatter |
| 80 | + |
| 81 | + subject.formatters[:timestamp].should_not be_nil |
| 82 | + end |
| 83 | + |
| 84 | + it 'should inherit formatters from ancestors' do |
| 85 | + subject.format_with :timestamp, &date_formatter |
| 86 | + child_class = Class.new(subject) |
| 87 | + |
| 88 | + child_class.formatters.should == subject.formatters |
| 89 | + end |
| 90 | + |
| 91 | + it 'should not allow registering a formatter without a block' do |
| 92 | + expect{ subject.format_with :foo }.to raise_error(ArgumentError) |
| 93 | + end |
| 94 | + |
| 95 | + it 'should format an exposure with a registered formatter' do |
| 96 | + subject.format_with :timestamp do |date| |
| 97 | + date.strftime('%m/%d/%Y') |
| 98 | + end |
| 99 | + |
| 100 | + subject.expose :birthday, :format_with => :timestamp |
| 101 | + |
| 102 | + model = { :birthday => Time.new(2012, 2, 27) } |
| 103 | + subject.new(mock(model)).as_json[:birthday].should == '02/27/2012' |
| 104 | + end |
| 105 | + end |
70 | 106 | end
|
71 | 107 |
|
72 | 108 | describe '.represent' do
|
|
201 | 237 | context 'instance methods' do
|
202 | 238 | let(:model){ mock(attributes) }
|
203 | 239 | let(:attributes){ {
|
204 |
| - :name => 'Bob Bobson', |
| 240 | + :name => 'Bob Bobson', |
205 | 241 |
|
| 242 | + :birthday => Time.new(2012, 2, 27), |
| 243 | + :fantasies => ['Unicorns', 'Double Rainbows', 'Nessy'], |
206 | 244 | :friends => [
|
207 |
| - mock(:name => "Friend 1", :email => '[email protected]', :friends => []), |
208 |
| - mock(:name => "Friend 2", :email => '[email protected]', :friends => []) |
| 245 | + mock(:name => "Friend 1", :email => '[email protected]', :fantasies => [], :birthday => Time.new(2012, 2, 27), :friends => []), |
| 246 | + mock(:name => "Friend 2", :email => '[email protected]', :fantasies => [], :birthday => Time.new(2012, 2, 27), :friends => []) |
209 | 247 | ]
|
210 | 248 | } }
|
211 | 249 | subject{ fresh_class.new(model) }
|
|
229 | 267 | expose :computed do |object, options|
|
230 | 268 | options[:awesome]
|
231 | 269 | end
|
| 270 | + |
| 271 | + expose :birthday, :format_with => :timestamp |
| 272 | + |
| 273 | + def timestamp(date) |
| 274 | + date.strftime('%m/%d/%Y') |
| 275 | + end |
| 276 | + |
| 277 | + expose :fantasies, :format_with => lambda {|f| f.reverse } |
232 | 278 | end
|
233 | 279 | end
|
234 | 280 |
|
@@ -261,6 +307,14 @@ class FriendEntity < Grape::Entity
|
261 | 307 | it 'should call through to the proc if there is one' do
|
262 | 308 | subject.send(:value_for, :computed, :awesome => 123).should == 123
|
263 | 309 | end
|
| 310 | + |
| 311 | + it 'should return a formatted value if format_with is passed' do |
| 312 | + subject.send(:value_for, :birthday).should == '02/27/2012' |
| 313 | + end |
| 314 | + |
| 315 | + it 'should return a formatted value if format_with is passed a lambda' do |
| 316 | + subject.send(:value_for, :fantasies).should == ['Nessy', 'Double Rainbows', 'Unicorns'] |
| 317 | + end |
264 | 318 | end
|
265 | 319 |
|
266 | 320 | describe '#key_for' do
|
|
0 commit comments