Sha256: d8143840379d676d18ccb5b0465082f9bbd83653a52a55c30aef4345c3d4fa38

Contents?: true

Size: 955 Bytes

Versions: 16

Compression:

Stored size: 955 Bytes

Contents

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

Version data entries

16 entries across 14 versions & 5 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/virtus-2.0.0/spec/integration/value_object_with_custom_constructor_spec.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/virtus-2.0.0/spec/integration/value_object_with_custom_constructor_spec.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/virtus-2.0.0/spec/integration/value_object_with_custom_constructor_spec.rb
virtus2-2.1.0 spec/integration/value_object_with_custom_constructor_spec.rb
virtus2-2.0.2 spec/integration/value_object_with_custom_constructor_spec.rb
virtus2-2.0.1 spec/integration/value_object_with_custom_constructor_spec.rb
virtus-2.0.0 spec/integration/value_object_with_custom_constructor_spec.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/virtus-1.0.5/spec/integration/value_object_with_custom_constructor_spec.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/virtus-1.0.5/spec/integration/value_object_with_custom_constructor_spec.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/virtus-1.0.5/spec/integration/value_object_with_custom_constructor_spec.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/virtus-1.0.5/spec/integration/value_object_with_custom_constructor_spec.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/virtus-1.0.5/spec/integration/value_object_with_custom_constructor_spec.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/virtus-1.0.5/spec/integration/value_object_with_custom_constructor_spec.rb
virtus-1.0.5 spec/integration/value_object_with_custom_constructor_spec.rb
virtus-1.0.4 spec/integration/value_object_with_custom_constructor_spec.rb
virtus-1.0.3 spec/integration/value_object_with_custom_constructor_spec.rb