Sha256: 7c8f6444d12edc45e24e9e8a9d9e6df91258117541a35f03e168b28a29c539d7

Contents?: true

Size: 1.56 KB

Versions: 9

Compression:

Stored size: 1.56 KB

Contents

require 'spec_helper'

describe 'I can create a Virtus module' do
  before do
    module Examples
      NoncoercingModule = Virtus.model { |config|
        config.coerce = false
      }

      CoercingModule = Virtus.model { |config|
        config.coerce = true

        config.coercer do |coercer|
          coercer.string.boolean_map = { 'yup' => true, 'nope' => false }
        end
      }

      StrictModule = Virtus.model { |config|
        config.strict = true
      }

      class NoncoercedUser
        include NoncoercingModule

        attribute :name, String
        attribute :happy, String
      end

      class CoercedUser
        include CoercingModule

        attribute :name, String
        attribute :happy, Boolean
      end

      class StrictModel
        include StrictModule

        attribute :stuff, Hash
        attribute :happy, Boolean, :strict => false
      end
    end
  end

  specify 'including a custom module with coercion disabled' do
    user = Examples::NoncoercedUser.new(:name => :Giorgio, :happy => 'yes')

    expect(user.name).to be(:Giorgio)
    expect(user.happy).to eql('yes')
  end

  specify 'including a custom module with coercion enabled' do
    user = Examples::CoercedUser.new(:name => 'Paul', :happy => 'nope')

    expect(user.name).to eql('Paul')
    expect(user.happy).to be(false)
  end

  specify 'including a custom module with strict enabled' do
    model = Examples::StrictModel.new

    expect { model.stuff = 'foo' }.to raise_error(Virtus::CoercionError)

    model.happy = 'foo'

    expect(model.happy).to eql('foo')
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
virtus-1.0.4 spec/integration/building_module_spec.rb
virtus-1.0.3 spec/integration/building_module_spec.rb
virtus-1.0.2 spec/integration/building_module_spec.rb
virtus-1.0.1 spec/integration/building_module_spec.rb
virtus-1.0.0 spec/integration/building_module_spec.rb
virtus-1.0.0.rc2 spec/integration/building_module_spec.rb
virtus-1.0.0.rc1 spec/integration/building_module_spec.rb
virtus-1.0.0.beta8 spec/integration/building_module_spec.rb
virtus-1.0.0.beta7 spec/integration/building_module_spec.rb