Sha256: 73d3f245a9981333f620b602154329215a0f9bae013b7e29bfa3e4640bcbeca7

Contents?: true

Size: 1.7 KB

Versions: 15

Compression:

Stored size: 1.7 KB

Contents

require File.join(File.dirname(__FILE__), "spec_helper")

module Sequel::Plugins

  module Timestamped
    def self.apply(m, opts)
      m.class_def(:get_stamp) {@values[:stamp]}
      m.meta_def(:stamp_opts) {opts}
      m.before_save {@values[:stamp] = Time.now}
    end
    
    module InstanceMethods
      def abc; timestamped_opts; end
    end
    
    module ClassMethods
      def deff; timestamped_opts; end
    end

    module DatasetMethods
      def ghi; timestamped_opts; end
    end
  end

end

describe Sequel::Model, "using a plugin" do

  it "should fail if the plugin is not found" do
    proc do
      c = Class.new(Sequel::Model) do
        is :something_or_other
      end
    end.should raise_error(LoadError)
  end
  
  it "should apply the plugin to the class" do
    c = nil
    proc do
      c = Class.new(Sequel::Model) do
        set_dataset MODEL_DB[:items]
        is :timestamped, :a => 1, :b => 2
      end
    end.should_not raise_error(LoadError)
    
    c.should respond_to(:stamp_opts)
    c.stamp_opts.should == {:a => 1, :b => 2}
    
    # instance methods
    m = c.new
    m.should respond_to(:get_stamp)
    m.should respond_to(:abc)
    m.abc.should == {:a => 1, :b => 2}
    t = Time.now
    m[:stamp] = t
    m.get_stamp.should == t
    
    # class methods
    c.should respond_to(:deff)
    c.deff.should == {:a => 1, :b => 2}
    
    # dataset methods
    c.dataset.should respond_to(:ghi)
    c.dataset.ghi.should == {:a => 1, :b => 2}
  end
  
  it "should fail to apply if the plugin has DatasetMethod and the model has no datset" do
    proc do
      Class.new(Sequel::Model) do
        is :timestamped, :a => 1, :b => 2
      end
    end.should raise_error(Sequel::Error)
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
sequel-1.4.0 spec/plugins_spec.rb
sequel-1.5.1 spec/plugins_spec.rb
sequel-1.5.0 spec/plugins_spec.rb
sequel_model-0.3.2.1 spec/plugins_spec.rb
sequel_model-0.3.2 spec/plugins_spec.rb
sequel_model-0.4.2 spec/plugins_spec.rb
sequel_model-0.5 spec/plugins_spec.rb
sequel_model-0.3.1 spec/plugins_spec.rb
sequel_model-0.3.3 spec/plugins_spec.rb
sequel_model-0.4 spec/plugins_spec.rb
sequel_model-0.3 spec/plugins_spec.rb
sequel_model-0.4.1 spec/plugins_spec.rb
sequel_model-0.2 spec/plugins_spec.rb
sequel_model-0.5.0.2 spec/plugins_spec.rb
sequel_model-0.5.0.1 spec/plugins_spec.rb