我应该使用哪种方法来使用 Rails 3.1 创建我的第一个开源 Ruby Gem?

发布于 2024-12-06 16:46:36 字数 1539 浏览 1 评论 0原文

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

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

发布评论

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

评论(3

烂人 2024-12-13 16:46:36

一般来说,Ruby gem 非常简单,Rails 插件只是以某种方式接触 Rails API 的 gem。 (正如另一个答案所指出的,Railtie 是挂钩 Rails 的好方法)。要编写 gem,请指定一个文件夹,并在其中包含:

  • .gemspec:这列出了 gem 的名称、版本、作者、网站、gem 依赖项列表和文件列表包含在宝石中。 (Rails 的 plugin new 和 Bundler gem 都会为您生成良好的 .gemspecs,请参见下文)
  • lib/.rb:这是您的主要代码宝石。通常这包含一个以您的 gem 命名的类或模块。 (Rails 的 plugin new 与模块一起使用)。

其他一切都是可选的,但良好的做法包括:

  • test/spec/:这是您的测试所在。由于 Rails 插件通常在应用程序中使用,Rails 的 plugin newtest/ 中创建了一个虚拟应用程序。如果您的 gem 非常独立,没有显式的 Rails 集成,则不需要虚拟应用程序。
  • 自述文件,用于描述 gem 的用途以及如何使用它。

您可以完全手动创建此文件夹,以及更重要的是 .gemspec 文件,但这两个工具将为您提供以下功能:

Bundler 为您做的事情:

  • 创建一个 Gemfile并自动将 .gemspec 中指定的依赖项包含在捆绑包中(它是单个 Gemfile 命令,gemspec
  • 创建一个示例.gemspec 为您设置从 lib//version.rb 提取您的 gem 版本,
  • 并使用 Git 生成列表gem 文件(方便,但 gem 构建机器需要安装 Git)。通常我使用 Rake 来完成此操作,但它更烦人。

Rails 的 plugin new 执行 Gemfile.gemspec,就像 Bundler 一样。另外:

  • Rails 使用 Ruby 生成 gemfile 列表。好的。
  • 它为您设置了一个最小的目录结构,大部分是空的。
  • 它设置了一个示例测试,以及一个用于运行测试的整个虚拟应用程序(!)。我刚刚看到这个,我觉得这是天才。

Rails 还会为您创建一个 Rakefile,用于添加 rake 任务,并且这两个工具都会为 Git 预先配置一个 .gitignore 以忽略一些常见产品。

要在实际应用中测试您的 gem,请执行 gem build.gemspec,并将 gem 添加到应用的 Gemfile。这将创建一个 -.gem 文件,其中包含整个 gem。您可以gem install-.gem,Bundler 会选择它。

当您准备好发布时,gem push-.gem 会将其推送到 RubyGems.org。有关获取帐户和推送 gem 的详细信息,请参阅 http://rubygems.org

另外,在开发过程中,您会想要对 gem 进行更改,而不必继续构建(和安装)版本。单元测试将解决其中的一些问题,但如果您想直接从源代码使用 gem,Bundler 有一个很棒的工具:

gem, :path =>; ""

该文件夹是包含 .gemspec 文件的文件夹。

有人知道使用珠宝商的理由吗?我比这些人更想知道它的作用。一般来说,一旦你有了一个可以复制、粘贴和修改的模板,手动编写gemspec对我来说就非常容易了。另外,如果我在每个工具为您提供的内容中犯了任何错误或遗漏,我已将答案制作为 wiki,以便可以更正它们。

Generally, Ruby gems are pretty simple, and Rails plugins are just gems that in some way touch Rails APIs. (as the other answer pointed out, Railties are a good way to hook into Rails). To write a gem, designate a folder, and have in it:

  • <gemname>.gemspec: this lists the gem's name, version, authors, website, a list of gem dependencies and a list of files to include in the gem. (Rails' plugin new and Bundler gem will both generate good .gemspecs for you, see below)
  • lib/<gemname>.rb: this is the main code of your gem. Usually this contains a class or a module named after your gem. (Rails' plugin new goes with module).

Everything else is optional, but good practices include:

  • test/ or spec/: this is where your tests go. Because a Rails plugin is usually used in an app, Rails' plugin new makes a dummy app in test/. If your gem is pretty standalone, with no explicit Rails integration, you don't need the dummy app.
  • README, for describing what you gem does and how it's to be used.

You can make this folder and, critically, the .gemspec file entirely by hand, but the two tools will give you the following:

Bundler does for you:

  • creates a Gemfile for you and automatically includes dependencies specified in your <gemname>.gemspec in the bundle (it's a single Gemfile command, gemspec)
  • creates a sample <gemname>.gemspec for you, sets it up to pull your gem's version from lib/<gemname>/version.rb
  • sets up using Git to generate a list of gem files (convenient, but the gem-building machine needs to have Git installed). Usually I do this using Rake, but it's more annoying.

Rails' plugin new does the Gemfile and .gemspec, just like Bundler. Also:

  • Rails uses Ruby to generate the list of gemfiles. Nice.
  • It sets up a minimal directory structure, mostly empty, for you.
  • It sets up a sample test, and an entire dummy app(!) for the test to run against. I just saw this, and it strikes me as genius.

Rails will also make you a Rakefile, for adding rake tasks, and both tools will preconfigure a .gitignore for Git to ignore some common products.

To test your gem in an actual app, do gem build <gemname>.gemspec, and add gem <gemname> to your app's Gemfile. This will make a -.gem file, which contains the whole gem. You can gem install <gemname>-<version>.gem, and Bundler will pick it up.

When you're ready to publish, gem push <gemname>-<version>.gem will push it up to RubyGems.org. See http://rubygems.org for details on getting an account and pushing gems.

Also, while you're developing, you'll want to make changes to your gem and not have to keep building (and installing) versions. Unit testing will take care of some of that, but if you'd like to use your gem directly from source, Bundler has a fantastic tool:

gem <gemname>, :path => "<path/to/your/gems/folder>"

The folder is the one that contains the .gemspec file.

Does anyone know any reason to use jeweler? I'd love know what it does more than these guys. Generally, writing gemspecs by hand is pretty easy for me, once you've got one template to copy-and-paste-and-modify. Also, if I made any mistakes or omissions in what each tool gives you, I've made the answer a wiki so they can be corrected.

握住我的手 2024-12-13 16:46:36

rails plugin new 创建一个空的 gem,它会立即挂接到 Rails 中。因此,它确实创建了一个“框架”供您填写。

然后您仍然需要 bundlerjeweler 来实际将这组文件转换为 gem 并发布它。

您从这两者中选择哪一个有点取决于品味。最近我发现很多人实际上都在提倡使用bundler。但对于 bundler 来说,它仍然主要是一个手动过程。

我个人认为使用jeweler还是很有趣的。使用 Jeweler 的优点是什么:

  • 在任何 Rails 或其他项目中使用 Gemfile ,Jeweler 将生成与其相对应的 gemspec。请注意,这与来自打包程序的 Yehuda 的建议相反:他建议手动填写 gemspec,然后通过在文件中指定 gemspec 来让 Gemfile 使用它。
  • 添加了一组 rake 任务,使您的生活更轻松地发布和构建您的 gem,
  • 允许轻松的版本管理
  • 与 git 很好地集成,将自动标记、推送、提交。

简而言之,在完成了 rails plugin new 之后,我仍然建议使用 jeweler

希望这有帮助。

rails plugin new creates an empty gem, that immediately hooks into rails. So it does creates a "frame" for you to fill in.

You will then still need either bundler or jeweler to actually convert this set of files to a gem and publish it.

Which you would choose from the two is a bit a matter of taste. Lately I find a lot of people are actually advocating to use bundler. But with bundler it still is mainly a manual process.

I personally believe it still is very interesting to use jeweler. What are the advantages of using jeweler:

  • use a Gemfile as in any rails or other project, jeweler will generate a gemspec that corresponds to that. Note that this is opposite to what Yehuda from bundler fame proposes: he proposes to fill in the gemspec manually, and then lets the Gemfile use that by specifying gemspec in the file.
  • adds a set of rake tasks to make your life easier to publish and build your gem
  • allows easy version management
  • very well integrated with git, will automatically tag, push, commit.

In short I still recommend to use jeweler, after doing a rails plugin new.

Hope this helps.

最近可好 2024-12-13 16:46:36

由于 gem 更易于安装和管理,因此最好使用 jeweler 来创建您的 gem。寻找灵感的最好方法是打开任何集成了 Rails 的 gem 的源代码,看看他们是如何做到的。一般来说,这是通过 Railties 设施完成的。

Since gems are easier to install and manage, it might be best to use jeweler to create your gem. The best way to find inspiration is to open up the source of any gem that has Rails integration and see how they do it. Generally this is done through the Railties facility.

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