Sha256: 885009a519e567d357656e9e6a90160a2b290df8e127d89f8d90478498c2ff0f

Contents?: true

Size: 1.97 KB

Versions: 20

Compression:

Stored size: 1.97 KB

Contents

require 'spec_helper'

describe Virtus, '.finalize' do
  before do
    module Examples
      class Person
        include Virtus.model(:finalize => false)

        attribute :name,     String
        attribute :articles, Array['Examples::Article']
        attribute :address,  :'Examples::Address'
      end

      class Article
        include Virtus.model(:finalize => false)

        attribute :posts, Hash['Examples::Person' => 'Examples::Post']
        attribute :person, :'Examples::Person'
      end

      class Post
        include Virtus.model

        attribute :title, String
      end

      class Address
        include Virtus.model

        attribute :street, String
      end
    end

    expect(Examples::Post.attribute_set[:title]).to be_finalized
    expect(Examples::Address.attribute_set[:street]).to be_finalized

    expect(Virtus::Builder.pending).not_to include(Examples::Post)
    expect(Virtus::Builder.pending).not_to include(Examples::Address)

    Virtus.finalize
  end

  it "sets attributes that don't require finalization" do
    expect(Examples::Person.attribute_set[:name]).to be_instance_of(Virtus::Attribute)
    expect(Examples::Person.attribute_set[:name].primitive).to be(String)
  end

  it 'it finalizes member type for a collection attribute' do
    expect(Examples::Person.attribute_set[:address].primitive).to be(Examples::Address)
  end

  it 'it finalizes key type for a hash attribute' do
    expect(Examples::Article.attribute_set[:posts].key_type.primitive).to be(Examples::Person)
  end

  it 'it finalizes value type for a hash attribute' do
    expect(Examples::Article.attribute_set[:posts].value_type.primitive).to be(Examples::Post)
  end

  it 'it finalizes type for an EV attribute' do
    expect(Examples::Article.attribute_set[:person].type.primitive).to be(Examples::Person)
  end

  it 'automatically resolves constant when it is already available' do
    expect(Examples::Article.attribute_set[:person].type.primitive).to be(Examples::Person)
  end
end

Version data entries

20 entries across 18 versions & 5 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/virtus-2.0.0/spec/unit/virtus/class_methods/finalize_spec.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/virtus-2.0.0/spec/unit/virtus/class_methods/finalize_spec.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/virtus-2.0.0/spec/unit/virtus/class_methods/finalize_spec.rb
virtus2-2.1.0 spec/unit/virtus/class_methods/finalize_spec.rb
virtus2-2.0.2 spec/unit/virtus/class_methods/finalize_spec.rb
virtus2-2.0.1 spec/unit/virtus/class_methods/finalize_spec.rb
virtus-2.0.0 spec/unit/virtus/class_methods/finalize_spec.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/virtus-1.0.5/spec/unit/virtus/class_methods/finalize_spec.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/virtus-1.0.5/spec/unit/virtus/class_methods/finalize_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/unit/virtus/class_methods/finalize_spec.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/virtus-1.0.5/spec/unit/virtus/class_methods/finalize_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/unit/virtus/class_methods/finalize_spec.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/virtus-1.0.5/spec/unit/virtus/class_methods/finalize_spec.rb
virtus-1.0.5 spec/unit/virtus/class_methods/finalize_spec.rb
virtus-1.0.4 spec/unit/virtus/class_methods/finalize_spec.rb
virtus-1.0.3 spec/unit/virtus/class_methods/finalize_spec.rb
virtus-1.0.2 spec/unit/virtus/class_methods/finalize_spec.rb
virtus-1.0.1 spec/unit/virtus/class_methods/finalize_spec.rb
virtus-1.0.0 spec/unit/virtus/class_methods/finalize_spec.rb
virtus-1.0.0.rc2 spec/unit/virtus/class_methods/finalize_spec.rb