Sha256: 0cff8af4799eb012683b82844af6ac03157dd566a1c5df486ad35b5bcc78276f

Contents?: true

Size: 1001 Bytes

Versions: 2

Compression:

Stored size: 1001 Bytes

Contents

require 'spec_helper'

class Foo
  include Transformable
  attr_accessor :title
  clean(:title) { |v| v.gsub(/\s/, "")}
end

class Bar
  include Transformable
  attr_accessor :title
  clean(:title, :skip_nil => false) {|v| v.nil? ? "bar" : v }
end

class NoNils
  include Transformable
  attr_accessor :title
  clean(:title) {|v| "bar" }
end

describe Transformable do
  it "used the provided block to filter attributes" do
    f = Foo.new
    f.title = "bar bar"
    f.title.should == "barbar"
  end

  context "when :skip_nil is not set (aka :skip_nil => true)" do
    it "doesn't let nil values through to the block" do
      n = NoNils.new
      n.title = nil
      n.title.should be_nil
    end
  end


  context "when :skip_nil is set to false" do
    it "lets nil values through" do
      b = Bar.new
      b.title = nil
      b.title.should == "bar"
    end

    it "lets non-nil values through" do
      b = Bar.new
      b.title = "foobar"
      b.title.should == "foobar"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
transformable-1.0.0 spec/transformable_spec.rb
transformable-0.0.5 spec/transformable_spec.rb