Sequel::Model:像create_table这样的方法从哪里来?

发布于 2024-08-12 11:46:25 字数 653 浏览 3 评论 0原文

我想了解续集是如何运作的。下面的示例继承自 Sequel::Model 并调用 set_schema、create_table 等。

我试图找到这些方法的文档,但在 Sequel::Model 的 rdoc 上没有运气: http://sequel.rubyforge.org/rdoc/classes/Sequel/Model.html

这些方法来自哪里Sequel::Model 如何使它们可用?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南风几经秋 2024-08-19 11:46:25

它们在 Sequel::Plugins::Schema::ClassMethods (lib/sequel/plugins/schema.rb) 中定义,并在您调用 plugin :schema 时包含在模型。

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

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

问题中的示例不完整,除非设置与数据库的连接,否则将无法工作并且从模型中调用 plugin :schema

 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> 

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> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文