Sha256: fb3645320be31d11681b05cec565c2cc4fe4ff922ba3c3e55dc656c40c7a1aae

Contents?: true

Size: 1.34 KB

Versions: 16

Compression:

Stored size: 1.34 KB

Contents

require 'spec_helper'

describe 'I can define attributes within a module' do
  before do
    module Examples
      module Common
        include Virtus
      end

      module Name
        include Common

        attribute :name, String
        attribute :gamer, Boolean
      end

      module Age
        include Common

        attribute :age,  Integer
      end

      class User
        include Name
      end

      class Admin < User
        include Age
      end

      class Moderator; end
    end
  end

  specify 'including a module with attributes into a class' do
    expect(Examples::User.attribute_set[:name]).to be_instance_of(Virtus::Attribute)
    expect(Examples::User.attribute_set[:gamer]).to be_instance_of(Virtus::Attribute::Boolean)

    expect(Examples::Admin.attribute_set[:name]).to be_instance_of(Virtus::Attribute)
    expect(Examples::Admin.attribute_set[:age]).to be_instance_of(Virtus::Attribute)

    user = Examples::Admin.new(:name => 'Piotr', :age => 29)
    expect(user.name).to eql('Piotr')
    expect(user.age).to eql(29)
  end

  specify 'including a module with attributes into an instance' do
    moderator = Examples::Moderator.new
    moderator.extend(Examples::Name, Examples::Age)

    moderator.attributes = { :name => 'John', :age => 21 }
    expect(moderator.name).to eql('John')
    expect(moderator.age).to eql(21)
  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/using_modules_spec.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/virtus-2.0.0/spec/integration/using_modules_spec.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/virtus-2.0.0/spec/integration/using_modules_spec.rb
virtus2-2.1.0 spec/integration/using_modules_spec.rb
virtus2-2.0.2 spec/integration/using_modules_spec.rb
virtus2-2.0.1 spec/integration/using_modules_spec.rb
virtus-2.0.0 spec/integration/using_modules_spec.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/virtus-1.0.5/spec/integration/using_modules_spec.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/virtus-1.0.5/spec/integration/using_modules_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/using_modules_spec.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/virtus-1.0.5/spec/integration/using_modules_spec.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/virtus-1.0.5/spec/integration/using_modules_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/using_modules_spec.rb
virtus-1.0.5 spec/integration/using_modules_spec.rb
virtus-1.0.4 spec/integration/using_modules_spec.rb
virtus-1.0.3 spec/integration/using_modules_spec.rb