forked from Empact/roxml
-
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
Allow defining :default
as lambda
#263
Open
yogeshjain999
wants to merge
2
commits into
trailblazer:master
Choose a base branch
from
yogeshjain999:default-lambda
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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 |
---|---|---|
@@ -1,34 +1,75 @@ | ||
require "test_helper" | ||
|
||
class DefaultTest < MiniTest::Spec | ||
Song = Struct.new(:id, :title) | ||
TIMESTAMP = Time.now | ||
|
||
Composer = Struct.new(:id, :name, :keywords) | ||
Song = Struct.new(:id, :title, :album, :composers, :created_at, :created_by) | ||
|
||
representer! do | ||
property :id | ||
property :title, default: "Huber Breeze" #->(options) { options[:default] } | ||
property :album, default: ->(represented:, **) { represented.album || "Spring" } | ||
collection :composers, instance: ->(*) { Composer.new } do | ||
property :id | ||
property :name, default: "Unknown" | ||
property :keywords, default: ->(represented:, **) { [represented.name.downcase] } | ||
end | ||
nested :metadata do | ||
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. It would be helpful if this nested also showed how to use a |
||
property :created_by, default: :default_creator, exec_context: :decorator | ||
property :created_at, default: ->(*) { TIMESTAMP } | ||
|
||
def default_creator(*) | ||
represented.composers&.first ? represented.composers.first.id : nil | ||
end | ||
|
||
def created_by | ||
represented.created_by | ||
end | ||
|
||
def created_by=(value) | ||
represented.created_by = value | ||
end | ||
end | ||
end | ||
|
||
describe "#from_hash" do | ||
let(:song) { Song.new.extend(representer) } | ||
let(:new_song) { Song.new.extend(representer) } | ||
let(:old_song) { Song.new(1, nil, "Atumn",[Composer.new(1, "Jerry")]).extend(representer) } | ||
|
||
it { _(old_song.from_hash({})).must_equal Song.new(1, "Huber Breeze", "Atumn",[Composer.new(1, "Jerry")]) } | ||
it { _(new_song.from_hash({})).must_equal Song.new(nil, "Huber Breeze", "Spring") } | ||
|
||
it { _(old_song.from_hash({"title"=>"Blindfold", "album"=>"Lil"})).must_equal Song.new(1, "Blindfold", "Lil",[Composer.new(1, "Jerry")]) } | ||
it { _(new_song.from_hash({"title"=>"Blindfold", "album"=>"Lil"})).must_equal Song.new(nil, "Blindfold", "Lil") } | ||
|
||
it { _(song.from_hash({})).must_equal Song.new(nil, "Huber Breeze") } | ||
# default doesn't apply when empty string. | ||
it { _(song.from_hash({"title"=>""})).must_equal Song.new(nil, "") } | ||
it { _(song.from_hash({"title"=>nil})).must_equal Song.new(nil, nil) } | ||
it { _(song.from_hash({"title"=>"Blindfold"})).must_equal Song.new(nil, "Blindfold") } | ||
it { _(old_song.from_hash({"title"=>"", "album"=>""})).must_equal Song.new(1, "", "",[Composer.new(1, "Jerry")]) } | ||
it { _(new_song.from_hash({"title"=>"", "album"=>""})).must_equal Song.new(nil, "", "") } | ||
it { _(old_song.from_hash({"title"=>nil, "album"=>nil})).must_equal Song.new(1, nil, nil,[Composer.new(1, "Jerry")]) } | ||
it { _(new_song.from_hash({"title"=>nil, "album"=>nil})).must_equal Song.new(nil, nil, nil) } | ||
|
||
# defaults within empty collections and nested | ||
it { _(old_song.from_hash({"composers"=>[],"metadata"=>{}})).must_equal Song.new(1, "Huber Breeze", "Atumn", [], TIMESTAMP, nil) } | ||
it { _(new_song.from_hash({"composers"=>[],"metadata"=>{}})).must_equal Song.new(nil, "Huber Breeze", "Spring", [], TIMESTAMP, nil) } | ||
it { _(old_song.from_hash({"composers"=>[{}],"metadata"=>{"created_at"=>"", "created_by"=>""}})).must_equal Song.new(1, "Huber Breeze", "Atumn", [Composer.new(nil, "Unknown", ["unknown"])], "", "") } | ||
it { _(new_song.from_hash({"composers"=>[{}],"metadata"=>{"created_at"=>"", "created_by"=>""}})).must_equal Song.new(nil, "Huber Breeze", "Spring", [Composer.new(nil, "Unknown", ["unknown"])], "", "") } | ||
|
||
# defaults within filled collections and nested | ||
it { _(new_song.from_hash({"composers"=>[{"id"=>1,"name"=>"Tom"}],"metadata"=>{"created_at"=>2022}})).must_equal Song.new(nil, "Huber Breeze", "Spring", [Composer.new(1, "Tom", ["tom"])], 2022, 1) } | ||
end | ||
|
||
describe "#to_json" do | ||
it "uses :default when not available from object" do | ||
_(Song.new.extend(representer).to_hash).must_equal({"title"=>"Huber Breeze"}) | ||
_(Song.new.extend(representer).to_hash).must_equal({"title"=>"Huber Breeze", "album"=>"Spring", "metadata"=>{"created_at"=>TIMESTAMP}}) | ||
end | ||
|
||
it "uses value from represented object when present" do | ||
_(Song.new(nil, "After The War").extend(representer).to_hash).must_equal({"title"=>"After The War"}) | ||
_(Song.new(nil, "After The War", "1964").extend(representer).to_hash).must_equal({"title"=>"After The War","album"=>"1964","metadata"=>{"created_at"=>TIMESTAMP}}) | ||
end | ||
|
||
it "uses value from represented object when emtpy string" do | ||
_(Song.new(nil, "").extend(representer).to_hash).must_equal({"title"=>""}) | ||
_(Song.new(nil, "", "").extend(representer).to_hash).must_equal({"title"=>"", "album"=>"","metadata"=>{"created_at"=>TIMESTAMP}}) | ||
end | ||
end | ||
end |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It would be very helpful if this collection also showed how to use a
default:
.BTW, is there a benefit of using
instance: ->(*) { Composer.new }
instead ofclass: Composer
? The tests seem to pass either way.