File: value_object_with_custom_constructor_spec.rb

package info (click to toggle)
ruby-virtus 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 660 kB
  • sloc: ruby: 4,378; makefile: 2
file content (42 lines) | stat: -rw-r--r-- 955 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
require 'spec_helper'

describe "Defining a ValueObject with a custom constructor" do
  before do
    module Examples
      class Point
        include Virtus::ValueObject

        attribute :x, Integer
        attribute :y, Integer

        def initialize(attributes)
          if attributes.kind_of?(Array)
            self.x = attributes.first
            self.y = attributes.last
          else
            super
          end
        end
      end

      class Rectangle
        include Virtus

        attribute :top_left,     Point
        attribute :bottom_right, Point
      end
    end
  end

  subject do
    Examples::Rectangle.new(:top_left => [ 3, 4 ], :bottom_right => [ 5, 8 ])
  end

  specify "initialize a value object attribute with correct attributes" do
    expect(subject.top_left.x).to be(3)
    expect(subject.top_left.y).to be(4)

    expect(subject.bottom_right.x).to be(5)
    expect(subject.bottom_right.y).to be(8)
  end
end