简体   繁体   English

Ruby on Rails 中的 Gem Vs 插件 Vs 引擎

[英]Gem Vs Plugin Vs Engine in Ruby on Rails

What is the difference between Gem package, plugin, and Engine in Ruby on Rails? Ruby on Rails 中的 Gem package、plugin 和 Engine 有什么区别?

I think we use the plugin before Rails3.2 and after rails3.2 is released we are using the gem package as a plugin but how can we use the engine in ROR?我认为我们在 Rails3.2 之前使用插件,在 rails3.2 发布之后我们使用 gem package 作为插件,但是我们如何在 ROR 中使用该引擎?

Plugins as you knew them from Rails 2 (ie plugins under the vendor/plugins folder) were deprecated for Rails 3.2; 您从Rails 2中知道的插件(即vendor/plugins文件夹下的vendor/plugins )已弃用Rails 3.2; support for it was completely removed in Rails 4. Now, there's a concept of a "gemified plugin" where the plugins are essentially built as gems, and can be shared across different Rails applications. 在Rails 4中完全删除了对它的支持。现在,有一个“gemified插件”的概念,其中插件基本上构建为gems,并且可以在不同的Rails应用程序之间共享。

But to answer your question about gems vs plugins, check out this Stackoverflow answer . 但要回答关于宝石与插件的问题,请查看此Stackoverflow答案 Long story short, plugins from the Rails 2 universe is an extension of the rails application, whereas a gem is a packaged ruby application. 长话短说, plugins从Rails的2宇宙是Rails应用程序的扩展 ,而创业板是一个Ruby应用程序包。

As for Rails engines, I've found this to be a pretty easy and intuitive definition of a Rails engine: 至于Rails引擎,我发现这是一个非常简单直观的Rails引擎定义

Rails Engines is basically a whole Rails app that lives in the container of another one. Rails Engines基本上是一个整个Rails应用程序,它位于另一个容器中。 Put another way, as the docs note: an app itself is basically just an engine at the root level. 换句话说,正如文档所指出的那样:应用程序本身基本上只是根级别的引擎。 Over the years, we've seen sen engines as parts of gems such as devise or rails_admin. 多年来,我们已经将sen引擎视为gems的一部分,例如devise或rails_admin。 These example show the power of engines by providing a large set of relatively self-contained functionality “mounted” into an app. 这些示例通过提供一组“安装”到应用程序中的相对独立的功能来展示引擎的强大功能。

And since both rails engines and plugins are types of ruby applications, they can all technically be packaged and used as a gem (usually). 由于轨道引擎和插件都是类型的红宝石应用程序,因此它们都可以在技术上进行打包并用作宝石(通常)。

Answer quoted from 答案引自 Difference between plugins and Ruby gems? 插件和Ruby宝石之间的区别?

Gem 宝石

  • Gem is a packaged ruby application using the packaging system defined by RubyGems . Gem是一个使用RubyGems定义的打包系统的打包ruby应用程序。
  • Rails itself is a Gem. Rails本身就是一个宝石。

    Rails gem is installed in jruby-1.0\\lib\\ruby\\gems\\1.8\\gems\\rails-1.2.3 as: Rails gem安装在jruby-1.0 \\ lib \\ ruby​​ \\ gems \\ 1.8 \\ gems \\ rails-1.2.3中:

    DIR bin DIR bin
    DIR builtin DIR内置
    68,465 CHANGELOG 68,465 CHANGELOG
    DIR configs DIR配置
    DIR dispatches DIR发送
    DIR doc DIR医生
    DIR environments DIR环境
    307 fresh_rakefile 307 fresh_rakefile
    DIR helpers DIR助手
    DIR html DIR html
    DIR lib DIR lib
    1,072 MIT-LICENSE 1,072 MIT-LICENSE
    11,969 Rakefile 11,969 Rakefile
    8,001 README 8,001自述文件
    The lib directory contains all the gem source code. lib目录包含所有gem源代码。

  • We can install,upgrade and query the gem version.If one uses a tool like my GemInstaller , one can easily automate the installation and loading of RubyGems with a single simple config file. 我们可以安装,升级和查询gem版本。如果使用像我的GemInstaller这样的工具,可以使用一个简单的配置文件轻松地自动安装和加载RubyGems。

  • Gem installed for Ruby interpreter can be used system-wide by that interpreter. 安装了Ruby解释器的Gem可以由该解释器在系统范围内使用。
  • Gem may be published as a plugin. Gem可以作为插件发布。
  • Can also be vendored in vendor/gems. 也可以在供应商/宝石中出售。

Plugin 插入

  • Plugin is an extension of Rails Framework. 插件是Rails框架的扩展。
  • Can not be upgraded by using a command. 无法使用命令升级。 To upgrade one have to uninstall and then install upgraded version. 升级一个必须卸载然后安装升级版本。
  • Has to be hooked into rails application. 必须要迷上rails应用程序。 (has to have init.rb) (必须有init.rb)
  • Have an install.rb file. 有一个install.rb文件。
  • Plugin cannot be published as a Gem. 插件无法作为Gem发布。
  • Can only be used application wide. 只能在应用范围内使用。

Goldspike plugin is installed in vendor\\plugins\\rails-integration directory of the application as: Goldspike插件安装在应用程序的vendor \\ plugins \\ rails-integration目录中,如下所示:
7,089 build.xml 7,089 build.xml
1,141 LICENSE.txt 1,141 LICENSE.txt
DIR plugins DIR插件
6,675 pom.xml 6,675 pom.xml
1,447 README 1,447自述文件
DIR samples DIR样本
plugins/goldspike directory consists of plugins / goldspike目录由
24 init.rb 24 init.rb
25 install.rb 25 install.rb.
DIR lib DIR lib
549 Rakefile 549 Rakefile
536 README 536自述文件
DIR tasks DIR任务
DIR test DIR测试
The lib directory contains all the plugin source code. lib目录包含所有插件源代码。

Gem vs Plugins 宝石vs插件

  • Rails had a way of loading plugins from the vendor/plugins/ directory. Rails有一种从vendor / plugins /目录加载插件的方法。 This will most likely deprecate as Rails has added support for bundling gems with the project in the vendor/gems/ directory. 这很可能会弃用,因为Rails已经添加了对与gem / gems /目录中的项目捆绑gems的支持。 The gem versions of rspec are the ones that are intended for everyday use. rspec的gem版本是用于日常使用的版本。 One should go with those unless you are supporting a Rails application in the 1.2.x family or earlier. 除非您在1.2.x系列或更早版本中支持Rails应用程序,否则应该使用它们。
  • It often becomes quicker to check-in and check-out of a repository using Gems as you are not including the library in your actual application. 使用Gems登记和签出存储库通常会变得更快,因为您不在实际应用程序中包含库。 There are often lesser problems using Plugins related to incompatibility arising concerning software versions among the distributed team. 使用与分布式团队中软件版本不兼容的插件相关的插件通常较少。
  • General rule of thumb is to make Rails-specific functionality a plugin while making more general Ruby libraries into gems. 一般的经验法则是将Rails特定的功能作为插件,同时将更多通用的Ruby库转换为gem。

Engine 发动机

An Engine in rails terminology is a actually a subapplication of a web-application. rails术语中的Engine实际上是Web应用程序的子应用程序。 For instance, something like a blog, a forum, or simple authentication: these are not full-blown applications, but pages/views/controllers/models that can be added to any rails application. 例如,博客,论坛或简单身份验证:这些不是完整的应用程序,而是可以添加到任何rails应用程序的页面/视图/控制器/模型。

In rails2 this would be done using a plugin . 在rails2中,这将使用plugin完成。 Now since rails3 an engine can be packaged in a gem . 现在因为rails3引擎可以包装在gem

  • A gem: is a generic library, which can be easily installed, which are version-managed, have dependencies and such. gem:是一个通用库,可以轻松安装,由版本管理,具有依赖性等。
  • An engine: is a sub-application of a Rails application, and since Rails 3 these are distributed as a gem (which is awesome!). 引擎:是Rails应用程序的子应用程序,并且由于Rails 3,它们作为gem分发(这很棒!)。

So when will you use one or the other: 所以你什么时候使用其中一个:

  • create a gem if you want to share ruby-functionality 如果要共享ruby功能,请创建一个gem
  • create an engine (and package it in a gem) if you have parts of your rails application that can be used more generally. 如果您的rails应用程序的某些部分可以更普遍地使用,则创建一个引擎(并将其打包在gem中)。

Here is an archived tutorial for creating an engine.. 这是一个用于创建引擎的存档教程。

There are no more plugins since Rails 4. Rails 4.0 release notes : 自Rails 4以来没有更多的插件 .Rails 4.0发行说明

Rails::Plugin has gone. Rails ::插件已经消失了。 Instead of adding plugins to vendor/plugins use gems or bundler with path or git dependencies. 而不是将插件添加到供应商/插件,而是使用带有路径或git依赖关系的gem或bundler。

Any engine can be contained in a gem. 任何引擎都可以包含在宝石中。 Gem is just an alias to a 'library'. 宝石只是“图书馆”的别名。


Best way to see what their difference is, is generating three of them and looking through their directory structure : 了解它们的不同之处的最佳方法是生成其中三个并查看其目录结构

bundle gem a_gem , use for non-rails-specific functionality. bundle gem a_gem ,用于非特定于rails的功能。

rails plugin new b_railtie , use for rails extensions that don't require full application-like setup. rails plugin new b_railtie ,用于不需要完全类似应用程序设置的rails扩展。 but, since it's still a rails-specific setup (you get your Rails dummy app in /test eg), you are probably going to use railtie in it. 但是,因为它仍然是特定于轨道的设置(例如你在/test得到你的Rails虚拟应用程序),你可能会在其中使用railstie railtie is a class that inherits from Rails::Railtie , and gives you the comfortable DSL to hook up your code into Rails. railtie是一个继承自Rails::Railtie的类,它为您提供了将代码连接到Rails的舒适DSL。 eg, if you want some action performed :before some Rails app initialization step, you can use initializer Railtie class_method. 例如,如果您想要执行某些操作:before某些Rails应用程序初始化步骤之前,您可以使用initializer Railtie class_method。 Paperclip 回形针

rails plugin new c_engine --full , use for rails extensions that will be full-fledged app themselves, mounted into your app. rails plugin new c_engine --full ,用于rails扩展,将成为完全成熟的应用程序,安装到您的应用程序中。 will give you /app dir and Engine subclass besides basic non- --full setup. 除了基本的非--full设置之外,还会给你/app dir和Engine子类。

rails plugin new c_engine --mountable , same as --full , but will create namespaces, ready to be mounted into your app engine. rails plugin new c_engine --mountable ,一样--full ,但将创建命名空间,随时被安装到您的应用程序引擎。 Spree 狂欢

And here is a pretty good link: http://hawkins.io/2012/03/defining_plugins_gems_railties_and_engines . 这是一个非常好的链接: http//hawkins.io/2012/03/defining_plugins_gems_railties_and_engines

Engines are very related to plugins. 引擎与插件非常相关。 Engines can be plugins and plugins can be engines. 引擎可以是插件,插件可以是引擎。 All of them can be created using rails plugin generator with 2 different options --full or --mountable . 所有这些都可以使用rails plugin生成器创建,具有2个不同的选项--full--mountable

I think the main different here between Engines and Gems. 我认为Engines和Gems之间的主要区别在于。

Gems is just a bit of code providing a set of functionalities to anyone who integrates it in its code. Gems只是一些代码,它为在代码中集成它的任何人提供了一系列功能。

It contains: 它包含:

  • Gemspec Gemspec
  • Lib folder Lib文件夹

Can be packaged and pushed to RubyGems servers 可以打包并推送到RubyGems服务器

Engines are actually gems. 发动机实际上是宝石。 All engines can be gems (if packaged) but not all gems are engines. 所有引擎都可以是宝石(如果打包)但并非所有宝石都是引擎。

We can say it with a different word, Engines is a Ruby on Rails feature, that can contain Rails-specific entities: models, controllers, views, migrations. 我们可以用不同的词来说,Engines是一个Ruby on Rails功能,它可以包含特定于Rails的实体:模型,控制器,视图,迁移。

It needs to be integrated inside Rails application and can't run on their own. 它需要集成在Rails应用程序中,不能自行运行。

Very good and quick read Artricle 非常好,快速阅读Artricle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM