-
Notifications
You must be signed in to change notification settings - Fork 228
Allow a default value when the attribute is assigned as nil, with the :allow_nil option #292
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
Open
JanDintel
wants to merge
3
commits into
solnic:master
Choose a base branch
from
JanDintel:allow-nil-option
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
3 commits
Select commit
Hold shift + click to select a range
ad1b7f9
Add the :allow_nil option, which is true by default, and its #allow_n…
JanDintel b0b941c
Add the Virtus::Attribute::Accessor#present? predicate, to determine …
JanDintel a75ba02
Implement the feature of allowing a default value when the attribute …
JanDintel 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| require 'spec_helper' | ||
|
|
||
| describe Virtus::Attribute, '#allow_nil?' do | ||
| subject { object.allow_nil? } | ||
|
|
||
| let(:object) { described_class.build(String, :allow_nil => allow_nil) } | ||
|
|
||
| context 'when allow_nil option is true' do | ||
| let(:allow_nil) { true } | ||
|
|
||
| it { should be(true) } | ||
| end | ||
|
|
||
| context 'when allow_nil option is false' do | ||
| let(:allow_nil) { false } | ||
|
|
||
| it { should be(false) } | ||
| end | ||
| end |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| require 'spec_helper' | ||
|
|
||
| describe Virtus::Attribute, '#present?' do | ||
| subject { object.present?(instance) } | ||
|
|
||
| let(:model) { Class.new { attr_accessor :test } } | ||
| let(:name) { :test } | ||
| let(:instance) { model.new } | ||
|
|
||
| context 'when the attribute allows nil values' do | ||
| let(:object) { described_class.build(String, :name => name, :allow_nil => true) } | ||
|
|
||
| context 'and the attribute value is defined as nil' do | ||
| before { instance.test = nil } | ||
| it { should be(true) } | ||
| end | ||
|
|
||
| context 'and the attribute value is NOT defined' do | ||
| it { should be(false) } | ||
| end | ||
| end | ||
|
|
||
| context 'when the attribute does NOT allow nil values' do | ||
| let(:object) { described_class.build(String, :name => name, :allow_nil => false) } | ||
|
|
||
| context 'and the attribute value is defined' do | ||
| before { instance.test = 'foo' } | ||
| it { should be(true) } | ||
| end | ||
|
|
||
| context 'and the attribute value is defined as nil' do | ||
| before { instance.test = nil } | ||
| it { should be(false) } | ||
| end | ||
|
|
||
| context 'and the attribute value is NOT defined' do | ||
| it { should be(false) } | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -7,7 +7,9 @@ | |
|
|
||
| attribute :id, Integer | ||
| attribute :name, String, :default => 'John Doe' | ||
| attribute :alias, String, :default => 'jdoe', :allow_nil => false | ||
| attribute :email, String, :default => '[email protected]', :lazy => true, :writer => :private | ||
| attribute :age, Integer, :default => 18, :allow_nil => false, :lazy => true | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -16,21 +18,31 @@ | |
|
|
||
| it 'sets default values for non-lazy attributes' do | ||
| expect(subject.instance_variable_get('@name')).to eql('John Doe') | ||
| expect(subject.instance_variable_get('@alias')).to eql('jdoe') | ||
| end | ||
|
|
||
| it 'skips setting default values for lazy attributes' do | ||
| expect(subject.instance_variable_get('@email')).to be(nil) | ||
| expect(subject.instance_variable_get('@age')).to be(nil) | ||
| end | ||
| end | ||
|
|
||
| context 'with attribute hash' do | ||
| subject { model.new(:id => 1, :name => 'Jane Doe') } | ||
| subject { model.new(:id => 1, :name => 'Jane Doe', :alias => nil, :age => nil) } | ||
|
|
||
| it 'sets attributes with public writers' do | ||
| expect(subject.id).to be(1) | ||
| expect(subject.name).to eql('Jane Doe') | ||
| end | ||
|
|
||
| it 'sets default values for attributes assigned as nil without allow_nil' do | ||
| expect(subject.alias).to eql('jdoe') | ||
| end | ||
|
|
||
| it 'sets default values for lazy attributes assigned as nil without allow_nil' do | ||
| expect(subject.age).to eql(18) | ||
| end | ||
|
|
||
| it 'skips setting attributes with private writers' do | ||
| expect(subject.instance_variable_get('@email')).to be(nil) | ||
| end | ||
|
|
||
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.
I don't think the example should use a
default, because it's much less likely that you'd need to worry aboutnilif you've got a default.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.
Oh, you're suggesting to use the default if
nilis passed in. Hmm. That's a harder sell.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.
Why do you need
:allow_nil => falsewhen you send a default?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.
@envygeeks Maybe this example explains it the best:
With the
:allow_nil => falseoption, the default is used when the attributes is assigned asnil.Edit: Just saw that I published a gist that describes the behaviour as well.
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.
Can you talk more about the differences between
page = Page.new(title: nil, author: nil)andpage = Page.new?