Last active
August 29, 2015 14:19
-
-
Save wojtekmach/e585c33830f9a3300b83 to your computer and use it in GitHub Desktop.
wrapping_virtus.rb
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' | |
module ValueObject | |
def self.new(&block) | |
Class.new { | |
include Virtus.value_object | |
values do | |
class_eval(&block) | |
end | |
} | |
end | |
end | |
Person = ValueObject.new do | |
attribute :name, String | |
attribute :date_of_birth, Date | |
def age | |
(Date.today - date_of_birth).to_i | |
end | |
end | |
person = Person.new(name: "John Doe", date_of_birth: "1970-01-01") | |
p person.age | |
p Person.new(name: "Anonymous") == Person.new(name: "Anonymous") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment