[英]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 binDIR bin
DIR builtinDIR内置
68,465 CHANGELOG68,465 CHANGELOG
DIR configsDIR配置
DIR dispatchesDIR发送
DIR docDIR医生
DIR environmentsDIR环境
307 fresh_rakefile307 fresh_rakefile
DIR helpersDIR助手
DIR htmlDIR html
DIR libDIR lib
1,072 MIT-LICENSE1,072 MIT-LICENSE
11,969 Rakefile11,969 Rakefile
8,001 README8,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.xml7,089 build.xml
1,141 LICENSE.txt1,141 LICENSE.txt
DIR pluginsDIR插件
6,675 pom.xml6,675 pom.xml
1,447 README1,447自述文件
DIR samplesDIR样本
plugins/goldspike directory consists ofplugins / goldspike目录由
24 init.rb24 init.rb
25 install.rb25 install.rb.
DIR libDIR lib
549 Rakefile549 Rakefile
536 README536自述文件
DIR tasksDIR任务
DIR testDIR测试
The lib directory contains all the plugin source code.lib目录包含所有插件源代码。
Gem vs Plugins 宝石vs插件
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
。
So when will you use one or the other: 所以你什么时候使用其中一个:
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: 它包含:
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应用程序中,不能自行运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.