Skip to content

Instantly share code, notes, and snippets.

@JanDintel
Last active August 29, 2015 14:06
Show Gist options
  • Save JanDintel/1ecee5fe4cd251a2997d to your computer and use it in GitHub Desktop.
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
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
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