tags:

views:

331

answers:

1

I am trying to understand how Sequel works. The example below inherit from Sequel::Model and calls set_schema, create_table, etc.

I was trying to find the documentation for these methods, but no luck on the rdoc for Sequel::Model: http://sequel.rubyforge.org/rdoc/classes/Sequel/Model.html

Where are these methods coming from and how does Sequel::Model make them available?

class Task < Sequel::Model
  set_schema do
    primary_key :id

    varchar :title, :unique => true, :empty => false
    boolean :done, :default => false
  end

  create_table unless table_exists?

  if empty?
    create :title => 'Laundry'
    create :title => 'Wash dishes'
  end
end
+1  A: 

They're defined in Sequel::Plugins::Schema::ClassMethods (lib/sequel/plugins/schema.rb) and included when you call plugin :schema in your model.

http://sequel.rubyforge.org/rdoc-plugins/classes/Sequel/Plugins/Schema/ClassMethods.html#M000110

http://sequel.rubyforge.org/rdoc/classes/Sequel/Model.html#M000130

The example in the question is incomplete and won't work unless a connection to a database is setup and the plugin :schema is called from the model.

 irb(main):001:0> require "rubygems"
 => true
 irb(main):002:0> require "sequel"
 => true
 irb(main):003:0> 
 irb(main):004:0*   # connect to an in-memory database
 irb(main):005:0*   DB = Sequel.sqlite
 => #<Sequel::SQLite::Database: "sqlite:/">
 irb(main):006:0> class Task < Sequel::Model
 irb(main):007:1>     set_schema do
 irb(main):008:2*           primary_key :id
 irb(main):009:2>     
 irb(main):010:2*           varchar :title, :unique => true, :empty => false
 irb(main):011:2>         boolean :done, :default => false
 irb(main):012:2>       end
 irb(main):013:1>   
 irb(main):014:1*       create_table unless table_exists?
 irb(main):015:1>   
 irb(main):016:1*       if empty?
 irb(main):017:2>         create :title => 'Laundry'
 irb(main):018:2>         create :title => 'Wash dishes'
 irb(main):019:2>       end
 irb(main):020:1>   end
 NoMethodError: undefined method `set_schema' for Task:Class
   from (irb):7
 irb(main):021:0> class Task < Sequel::Model
 irb(main):022:1>   plugin :schema
 irb(main):023:1>     set_schema do
 irb(main):024:2*           primary_key :id
 irb(main):025:2>     
 irb(main):026:2*           varchar :title, :unique => true, :empty => false
 irb(main):027:2>         boolean :done, :default => false
 irb(main):028:2>       end
 irb(main):029:1>   
 irb(main):030:1*       create_table unless table_exists?
 irb(main):031:1>   
 irb(main):032:1*       if empty?
 irb(main):033:2>         create :title => 'Laundry'
 irb(main):034:2>         create :title => 'Wash dishes'
 irb(main):035:2>       end
 irb(main):036:1>   end
 => #<Task @values={:title=>"Wash dishes", :done=>false, :id=>2}>
 irb(main):037:0>
samg
Who is calling plugin :schema? The superclass? My example model is not calling it...
Sergio Oliveira Jr.
@Sergio - I believe that you need to call `plugin :schema` in your model, and that your example is incomplete an will not work as coded. Adding this line to your example will make it work, by extending your model with the Schema plugin's methods.I've edited my above answer with an irb session demonstrating this.
samg
@Sergio - If your example does work as is it's possible you're using a different version of Sequel. My answer refers to version 3.6.0 which I believe is the latest (at the time of this writing.)
samg