Last active
August 29, 2015 14:06
-
-
Save JanDintel/1ecee5fe4cd251a2997d to your computer and use it in GitHub Desktop.
Virtus: Allow a default value when the attribute is assigned as nil, with the :allow_nil option
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
require 'virtus' | |
class Page | |
include Virtus.model | |
attribute :title, String | |
attribute :font, String, :default => 'Helvetica' | |
attribute :header, String, :default => 'Welcome' | |
attribute :author, String, :default => 'Mies', :allow_nil => false | |
end | |
page = Page.new(:title => 'Virtus README', :header => nil, :author => nil) | |
page.title # => 'Virtus README' | |
page.font # => 'Helvetica' | |
page.header # => nil | |
page.author # => nil |
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
require 'virtus' | |
class Page | |
include Virtus.model | |
attribute :title, String | |
attribute :font, String, :default => 'Helvetica' | |
attribute :header, String, :default => 'Welcome' | |
attribute :author, String, :default => 'Mies', :allow_nil => false | |
end | |
page = Page.new(:title => 'Virtus README', :header => nil, :author => nil) | |
page.title # => 'Virtus README' | |
page.font # => 'Helvetica' | |
page.header # => nil | |
page.author # => 'Mies' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment