Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Crafting Gems: RailsConf 2013

Crafting Gems: RailsConf 2013

Gems underpin almost every piece of Ruby code we write – and so, being able to write your own gems is not only incredibly useful, it provides an avenue for code reuse and open source sharing.

During this session, Pat will talk about the ecosystem around gems and the knowledge required to write your own gems. He’ll also cover some of the tools available to assist with this, along with ideal approaches and best practices for gem development.

29f82ebe1801087f04de6aaae92e19ea?s=128

Pat Allan

May 01, 2013
Tweet

More Decks by Pat Allan

Other Decks in Technology

Transcript

  1. Cut and Polish A Guide to Crafting Gems

  2. Pat Allan @pat freelancing-gods.com

  3. None
  4. None
  5. Say Hello!

  6. “Use rubygems to publish your gem to rubygems”

  7. “Use rubygems to publish your gem to rubygems”

  8. “Use gem to publish your gem to rubygems”

  9. “Use gem to publish your gem to rubygems”

  10. “Use the gem command to publish your gem to rubygems.org”

  11. noun a precious or semiprecious stone, esp. when cut and

    polished or engraved. |jem| gem
  12. noun a packaged code library written in the Ruby programming

    language. |jem| gem
  13. noun a command line executable distributed with the RubyGems package

    manager. |jem| gem
  14. noun a package manager for Ruby libraries. |ˈro͞obējemz| rubygems

  15. noun a website that stores published versions of Ruby gems.

    |ˈro͞obējemz| rubygems
  16. Why?

  17. Why? re-use code

  18. Why? easy to install

  19. Why? easy to share

  20. History

  21. History 2003 rubyforge.org

  22. History 2004 rubygems

  23. “If you have libraries you would like to include, please

    send the gem les to either myself or Rich Kilmer” Chad Fowler, 15 March 2004
  24. History gems were magical

  25. History permission was required

  26. History 2009 gemcutter.org

  27. History anyone can publish

  28. History rubygems.org

  29. Gemspecs

  30. Gemspecs pdx.gemspec

  31. # pdx.gemspec Gem::Specification.new do |s| s.name = ‘pdx’ s.version =

    ‘0.0.1’ s.authors = [‘Pat Allan’] s.email = [‘pat@email.com’] s.homepage = ‘’ s.summary = ‘RailsConf Gem’ s.description = ‘’ s.files = [...] s.test_files = [...] s.executables = [...] end
  32. s.name = ‘pdx’ Naming

  33. Naming # name ‘will_paginate’ # requiring require ‘will_paginate’ # Class/Module

    WillPaginate
  34. Naming # name ‘cucumber-rails’ # requiring require ‘cucumber/rails’ # Class/Module

    Cucumber::Rails
  35. Versions s.version = ‘0.0.1’

  36. Versions s.version = ‘Major.Minor.Release’

  37. Versions Major Versions

  38. Versions Minor Versions

  39. Versions Release Versions

  40. Versions Reference

  41. Versions # lib/pdx/version.rb module PDX VERSION = ‘0.0.1’ end

  42. Versions # pdx.gemspec require ‘pdx/version’ # ... s.version = PDX::VERSION

  43. Versions VERSION = ‘1.0.0.beta1’

  44. Authors, Emails & Homepage s.authors = [‘Pat Allan’] s.email =

    [‘pat@email.com’] s.homepage = ‘https:// github.com/pat/pdx’
  45. Summary & Description s.summary = ‘RailsConf Gem’ s.description = ‘All

    the Ruby code’
  46. Files s.files = [ ‘lib/pdx.rb’, ‘lib/pdx/version.rb’, ‘README.textile’, ‘LICENCE’ ]

  47. Files lib/pdx.rb

  48. Files PDX::RailsConf lib/pdx/rails_conf.rb PDX::Hipster lib/pdx/hipster.rb

  49. Files # Don’t do this lib/pdx.rb lib/rails_conf.rb lib/hipster.rb

  50. Information Files README

  51. Information Files LICENCE

  52. Information Files HISTORY

  53. Test Files s.test_files = [ ‘spec/pdx_spec.rb’, ‘spec/spec_helper.rb’ ]

  54. Test Files Beware Large Test Suites

  55. Test Files Travis CI

  56. Executables s.executables = [ ‘bin/pdx’ ]

  57. Executables #!/usr/bin/env ruby require ‘pdx’ PDX::CLI.run

  58. # pdx.gemspec Gem::Specification.new do |s| s.name = ‘pdx’ s.version =

    ‘0.0.1’ s.authors = [‘Pat Allan’] s.email = [‘pat@email.com’] s.homepage = ‘’ s.summary = ‘RailsConf Gem’ s.description = ‘’ s.files = [...] s.test_files = [...] s.executables = [...] end
  59. Dependencies s.add_runtime_dependency ‘rails’, ‘>= 3.0.0’

  60. Dependencies s.add_development_dependency ‘rspec’, ‘>= 2.6.0’

  61. Dependencies Dependency Versions

  62. Dependencies s.add_runtime_dependency ‘rails’, ‘~> 3.1’

  63. Dependencies ~>

  64. Dependencies ~> 3.1 == [>= 3.1 && < 4.0] ~>

    3.0.3 == [>= 3.0.3 && < 3.1]
  65. Dependencies s.add_development_dependency ‘rspec’, ‘= 2.6.0’

  66. Rake Tasks

  67. # run tests rake spec # generate documentation rake yard

    # default task should run tests rake
  68. Railties

  69. Rails Railties class PDX::Railtie < Rails::Railtie rake_tasks do load File.expand_path(

    '../tasks.rb', __FILE__ ) end end
  70. Engines

  71. Rails Engines class PDX::Engine < Rails::Engine engine_name :pdx paths['app/controllers'] <<

    'lib/pdx/controllers' end
  72. Publishing

  73. Publishing gem build pdx.gemspec #=> pdx-0.0.1.gem

  74. Gem Files .gem == .tar

  75. Publishing gem push pdx-0.0.1.gem

  76. Publishing Permanent

  77. Publishing (Don’t) Yank

  78. Publishing MRI 1.9 Caution

  79. Community

  80. Community Support

  81. Community GitHub

  82. Community Releases

  83. Bundler

  84. Bundler Recommended

  85. Bundler Not Required

  86. Bundler # Gemfile source ‘https://rubygems.org’ gemspec

  87. Bundler # Gemfile gemspec gem ‘mysql2’, :platform => :ruby gem

    ‘jdbc-mysql’, :platform => :jruby
  88. Bundler rake release

  89. Bundler # Rakefile Bundler::GemHelper. install_tasks

  90. Tools

  91. Tools Hoe

  92. Tools Jeweler

  93. Examples

  94. Examples bundle open pdx

  95. Thank You Questions?

  96. http://railsconftutorials.com http://bit.ly/pat-gems Tutorial Instructions here: Gems available here: