Skip to content

Instantly share code, notes, and snippets.

@avdi
Forked from solnic/virtus-coercion.rb
Created May 11, 2012 22:07
Show Gist options
  • Save avdi/2662690 to your computer and use it in GitHub Desktop.
Save avdi/2662690 to your computer and use it in GitHub Desktop.
ValueObject in Virtus with a custom constructor - proposed
require 'virtus'
Point = Struct.new(:x, :y)
class Rectangle
include Virtus
attribute :top_left, Point
attribute :bottom_right, Point
end
rect = Rectangle.new(top_left: [3,5], bottom_right: [8,7])
puts rect.top_left.inspect # => #<Point x=3 y=5>
puts rect.bottom_right.inspect # => #<Point x=8 y=7>
@solnic
Copy link

solnic commented May 11, 2012

@avdi I'm going to make that possible in the upcoming Virtus 0.5.0 :) thanks for inspirations man

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment