Skip to content

Instantly share code, notes, and snippets.

@wojtekmach
Last active August 29, 2015 14:19
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
wrapping_virtus.rb
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