-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #156 from bobbytables/entity-format-with
Entity format with
- Loading branch information
Showing
2 changed files
with
117 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,10 @@ | |
expect{ subject.expose :name, :email, :as => :foo }.to raise_error(ArgumentError) | ||
expect{ subject.expose :name, :as => :foo }.not_to raise_error | ||
end | ||
|
||
it 'should make sure that :format_with as a proc can not be used with a block' do | ||
expect { subject.expose :name, :format_with => Proc.new {} do |object,options| end }.to raise_error(ArgumentError) | ||
end | ||
end | ||
|
||
context 'with a block' do | ||
|
@@ -67,6 +71,38 @@ | |
child_class.exposures[:name].should have_key :proc | ||
end | ||
end | ||
|
||
context 'register formatters' do | ||
let(:date_formatter) { lambda {|date| date.strftime('%m/%d/%Y') }} | ||
|
||
it 'should register a formatter' do | ||
subject.format_with :timestamp, &date_formatter | ||
|
||
subject.formatters[:timestamp].should_not be_nil | ||
end | ||
|
||
it 'should inherit formatters from ancestors' do | ||
subject.format_with :timestamp, &date_formatter | ||
child_class = Class.new(subject) | ||
|
||
child_class.formatters.should == subject.formatters | ||
end | ||
|
||
it 'should not allow registering a formatter without a block' do | ||
expect{ subject.format_with :foo }.to raise_error(ArgumentError) | ||
end | ||
|
||
it 'should format an exposure with a registered formatter' do | ||
subject.format_with :timestamp do |date| | ||
date.strftime('%m/%d/%Y') | ||
end | ||
|
||
subject.expose :birthday, :format_with => :timestamp | ||
|
||
model = { :birthday => Time.new(2012, 2, 27) } | ||
subject.new(mock(model)).as_json[:birthday].should == '02/27/2012' | ||
end | ||
end | ||
end | ||
|
||
describe '.represent' do | ||
|
@@ -201,11 +237,13 @@ | |
context 'instance methods' do | ||
let(:model){ mock(attributes) } | ||
let(:attributes){ { | ||
:name => 'Bob Bobson', | ||
:name => 'Bob Bobson', | ||
:email => '[email protected]', | ||
:birthday => Time.new(2012, 2, 27), | ||
:fantasies => ['Unicorns', 'Double Rainbows', 'Nessy'], | ||
:friends => [ | ||
mock(:name => "Friend 1", :email => '[email protected]', :friends => []), | ||
mock(:name => "Friend 2", :email => '[email protected]', :friends => []) | ||
mock(:name => "Friend 1", :email => '[email protected]', :fantasies => [], :birthday => Time.new(2012, 2, 27), :friends => []), | ||
mock(:name => "Friend 2", :email => '[email protected]', :fantasies => [], :birthday => Time.new(2012, 2, 27), :friends => []) | ||
] | ||
} } | ||
subject{ fresh_class.new(model) } | ||
|
@@ -229,6 +267,14 @@ | |
expose :computed do |object, options| | ||
options[:awesome] | ||
end | ||
|
||
expose :birthday, :format_with => :timestamp | ||
|
||
def timestamp(date) | ||
date.strftime('%m/%d/%Y') | ||
end | ||
|
||
expose :fantasies, :format_with => lambda {|f| f.reverse } | ||
end | ||
end | ||
|
||
|
@@ -261,6 +307,14 @@ class FriendEntity < Grape::Entity | |
it 'should call through to the proc if there is one' do | ||
subject.send(:value_for, :computed, :awesome => 123).should == 123 | ||
end | ||
|
||
it 'should return a formatted value if format_with is passed' do | ||
subject.send(:value_for, :birthday).should == '02/27/2012' | ||
end | ||
|
||
it 'should return a formatted value if format_with is passed a lambda' do | ||
subject.send(:value_for, :fantasies).should == ['Nessy', 'Double Rainbows', 'Unicorns'] | ||
end | ||
end | ||
|
||
describe '#key_for' do | ||
|