Gem 制作

Posted on April 1, 2014

.gemspec

  • require_paths 默认值是lib,当gem加载时,该参数指定的目录会加入$LOAD_PATH

  • files 该gem包含的所有文件。只能是文件,目录会过滤掉

  • add_development_dependency(gem, *requirements) 添加开发模式下的依赖

  • add_runtime_dependency(gem, *requirements)

  • add_dependency Alias for: add_runtime_dependency

使用bundler创建gem

  • bundle gem my_gem

    create  my_gem/Gemfile
    create  my_gem/Rakefile
    create  my_gem/LICENSE.txt
    create  my_gem/README.md
    create  my_gem/.gitignore
    create  my_gem/my_gem.gemspec
    create  my_gem/lib/my_gem.rb
    create  my_gem/lib/my_gem/version.rb
    
    • Rakefile

      只有 require "bundler/gem_tasks 其中创建了用于安装的rakeRake::Task[:install]

    • Gemfile

      见</link/2014/03/30/clarifying-the-roles-of-the-gemspec-and-gemfile/>

  • 除了在.gemspec中添加其他gem的依赖外,gem代码中必须要手动require用到了的gem

  • 创建gem:gem build my_gem.gemspec

  • 在irb中引入gem进行调试bundle console

  • 发布到RubyGems.org rake release

引入rspec

  1. 创建my_gem/specmy_gem/spec/spec_helper.rb

  2. spec_helper.rb 中:

     require 'bundler/setup'
     require 'rails_service'
    
     RSpec.configure do |config|
       # some (optional) config here
     end
    
  3. 增加rake <default>rake test (可选), 在Rakefile里:

     require "bundler/gem_tasks"
    
     require "rspec/core/rake_task"
     RSpec::Core::RakeTask.new
     task :default => :spec
     task :test => :spec
    

参考资料