File: overriding_virtus_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 (46 lines) | stat: -rw-r--r-- 1,217 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
43
44
45
46
require 'spec_helper'

describe 'overriding virtus behavior' do

  before do
    module Examples
      class Article
        include Virtus

        attribute :title, String

        def title
          super || '<unknown>'
        end

        def title=(name)
          super unless self.title == "can't be changed"
        end
      end
    end
  end

  describe 'overriding an attribute getter' do
    specify 'calls the defined getter' do
      expect(Examples::Article.new.title).to eq('<unknown>')
    end

    specify 'super can be used to access the getter defined by virtus' do
      expect(Examples::Article.new(:title => 'example article').title).to eq('example article')
    end
  end

  describe 'overriding an attribute setter' do
    specify 'calls the defined setter' do
      article = Examples::Article.new(:title => "can't be changed")
      article.title = 'this will never be assigned'
      expect(article.title).to eq("can't be changed")
    end

    specify 'super can be used to access the setter defined by virtus' do
      article = Examples::Article.new(:title => 'example article')
      article.title = 'my new title'
      expect(article.title).to eq('my new title')
    end
  end
end