File: inheritance_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-- 1,055 bytes parent folder | download
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 'Inheritance' do
  before do
    module Examples
      class Base
        include Virtus.model
      end

      class First < Base
        attribute :id, Integer
        attribute :name, String, default: ->(first, _) { "Named: #{first.id}" }
        attribute :description, String
      end

      class Second < Base
        attribute :something, String
      end
    end
  end

  it 'inherits model from the base class' do
    expect(Examples::First.attribute_set.map(&:name)).to eql([:id, :name, :description])
    expect(Examples::Second.attribute_set.map(&:name)).to eql([:something])
  end

  it 'sets correct attributes on the descendant classes' do
    first = Examples::First.new(:id => 1, :description => 'hello world')

    expect(first.id).to be(1)
    expect(first.name).to eql('Named: 1')
    expect(first.description).to eql('hello world')

    second = Examples::Second.new

    expect(second.something).to be(nil)

    second.something = 'foo bar'

    expect(second.something).to eql('foo bar')
  end
end