Skip to content

Instantly share code, notes, and snippets.

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