-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide default alias/naming option at Representer-level #131
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,6 +84,20 @@ end | |
song.to_json #=> {"name":"Fallout","track":1} | ||
``` | ||
|
||
You can also a naming strategy at a Representer-level: | ||
|
||
```ruby | ||
module SongRepresenter | ||
include Representable::JSON | ||
|
||
self.naming_strategy = ->(name) { name.camelize } # (with ActiveSupport available) | ||
|
||
property :song_title | ||
property :song_track, as: :track | ||
end | ||
|
||
song.to_json #=> {"songTitle":"Fallout","track":1} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you were using the Either use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @NobodysNightmare quite right! |
||
``` | ||
|
||
## Wrapping | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
require 'test_helper' | ||
|
||
class AsStrategyTest < BaseTest | ||
let(:format) { :hash } | ||
let(:song) { representer.prepare(Struct.new(:title).new('Revolution')) } | ||
|
||
class ReverseNamingStrategy | ||
def call(name) | ||
name.reverse | ||
end | ||
end | ||
|
||
describe 'module' do | ||
describe 'with lambda' do | ||
let(:output) { { 'TITLE' => 'Revolution' } } | ||
let(:input) { { 'TITLE' => 'Wie es geht' } } | ||
|
||
representer! do | ||
self.naming_strategy = ->(name) { name.upcase } | ||
property :title | ||
end | ||
|
||
it { render(song).must_equal_document(output) } | ||
it { parse(song, input).title.must_equal('Wie es geht') } | ||
end | ||
|
||
describe 'with class responding to #call?' do | ||
let(:output) { { 'eltit' => 'Revolution' } } | ||
let(:input) { { 'eltit' => 'Wie es geht' } } | ||
|
||
representer! do | ||
self.naming_strategy = ReverseNamingStrategy.new | ||
property :title | ||
end | ||
|
||
it { render(song).must_equal_document(output) } | ||
it { parse(song, input).title.must_equal('Wie es geht') } | ||
end | ||
|
||
describe 'with class not responding to #call?' do | ||
representer! do | ||
self.naming_strategy = Object.new | ||
property :title | ||
end | ||
|
||
it { -> { render(song) }.must_raise(RuntimeError) } | ||
end | ||
end | ||
|
||
describe 'decorator' do | ||
describe 'with a lambda' do | ||
let(:output) { { 'TITLE' => 'Revolution' } } | ||
let(:input) { { 'TITLE' => 'Wie es geht' } } | ||
|
||
representer!(decorator: true) do | ||
self.naming_strategy = ->(name) { name.upcase } | ||
property :title | ||
end | ||
|
||
it { render(song).must_equal_document(output) } | ||
it { parse(song, input).title.must_equal('Wie es geht') } | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing a verb here ;-)
I guess it should read