Sha256: 347dc6c78531407d70bfa8caa5735bdabac9c81e0b327d0dd6f5874a8bb577c5

Contents?: true

Size: 1.5 KB

Versions: 2

Compression:

Stored size: 1.5 KB

Contents

require 'spec_helper'

describe 'callbacks' do
  class Tree
    include CouchPotato::Persistence

    before_validation :grow_leaf, 'grow_branch', lambda {|tree| tree.root_count ||= 0; tree.root_count += 1 }

    property :leaf_count
    property :root_count
    property :branch_count
    property :watered
    

    def grow_leaf
      self.leaf_count ||= 0
      self.leaf_count += 1
    end
    
    def grow_branch
      self.branch_count ||= 0
      self.branch_count += 1
    end
  end
  
  class AppleTree < Tree
    attr_accessor :watered
    
    before_validation :water
    
    def water
      self.watered = true
    end
    
    def watered?
      watered
    end
  end

  it "should call a method from a symbol when validated" do
    tree = Tree.new(:leaf_count => 1, :root_count => 1)
    tree.valid?
    tree.leaf_count.should == 2
  end
  
  it "should call a method from a string when validated" do
    tree = Tree.new(:branch_count => 0)
    tree.valid?
    tree.branch_count.should == 1
  end

  it "should call a lambda when validated" do
    tree = Tree.new(:leaf_count => 1, :root_count => 1)
    tree.valid?
    tree.root_count.should == 2
  end
  
  context 'inheritance' do
    it "should call the callbacks of the super class" do
      tree = AppleTree.new :leaf_count => 1
      tree.valid?
      tree.leaf_count.should == 2
    end
    
    it "should call the callbacks of the child class" do
      tree = AppleTree.new :leaf_count => 1
      tree.valid?
      tree.should be_watered
    end
  end
  
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
davber_couch_potato-0.3.0 spec/unit/callbacks_spec.rb
couch_potato-0.3.0 spec/unit/callbacks_spec.rb