SlideShare une entreprise Scribd logo
1  sur  15
Télécharger pour lire hors ligne
SF Software Design in Ruby Meetup Group

Ruby Gem Internals by Examples
Ben Zhang, 1/14/2014
benzhangpro@gmail.com
http://www.meetup.com/Software-Design-in-Ruby-Study-Group/
Types of Ruby Gem Features
● Global Singleton
○ typhoeus, twitter
● Single Web Feature (MVC method adding)
○ will_piginate, simple_form
● Mountable Engine
○ rails_admin, resque
● Global Observers
○ airbrake, newrelic_rpm
● Command Line Tool
○ rspec, html2haml
Choose a Good Interface
will_paginate

kaminari

Post.paginate(:page => params[:page],

Post.order(:name).page(params[:page]).

:per_page => 30)
class Post < ActiveRecord::Base
self.per_page = 10

per_page(30)
class Post < ActiveRecord::Base
paginates_per 50

end

end

<%= will_paginate @posts %>

<%= paginate @users %>
Configuration Options: Outcome
Kaminari.configure do |config|
config.default_per_page = 25
config.max_per_page = nil
end
Configuration Options: The How
module Kaminari
def self.configure(&block)
yield @config ||= Kaminari::Configuration.new
end
def self.config
@config
end
class Configuration
include ActiveSupport::Configurable
config_accessor :default_per_page {}
config_accessor :max_per_page {}
end
end
Gem Initialization I
#lib/kaminari.rb

require 'kaminari/config'

module Kaminari

require 'kaminari/helpers/paginator'

end

require ‘...’
if defined? Rails

begin
require 'rails'
rescue LoadError

#do nothing
end

require 'kaminari/railtie'
require 'kaminari/engine'
end

#lib/kaminari/railtie.rb
module Kaminari
class Railtie < ::Rails::Railtie

if !defined?(Rails) && !defined?(Sinatra)

initializer 'kaminari' do |_app|
Kaminari::Hooks.init

$stderr.puts “no framework detected.”
end

end
end
end
Gem Initialization II
module Kaminari
class Hooks
def self.init
ActiveSupport.on_load(:active_record) do
require 'kaminari/models/active_record_extension'
::ActiveRecord::Base.send :include, Kaminari::ActiveRecordExtension
end
begin; require 'data_mapper'; rescue LoadError; end
if defined? ::DataMapper
require 'kaminari/models/data_mapper_extension'
::DataMapper::Collection.send :include, Kaminari::DataMapperExtension::Collection
end
end
end
end
Add methods to controllers
module YourGem
class Feature
def user_agent(request)
end
end
end
ApplicationController.send :include, YourGem::Feature
class UsersController < ApplicationController
def index
user_agent(request)
end
end
Add methods to models
# in your code
class Artist
include Mongoid::Document
field :name, type: String
embeds_many :instruments
end
# in your gem
module Mongoid
class Document
def self.embeds_many(association)
end
def self.field(field, options={})
end
end
end
Add methods to views
#lib/kaminari.rb

#lib/kaminari/hooks.rb

require 'kaminari/helpers/action_view_extension'

module Kaminari
class Hooks
def self.init

#lib/kaminari/helpers/action_view_extension.rb

ActiveSupport.on_load(:action_view) do

module Kaminari

::ActionView::Base.send :include,

module ActionViewExtension

Kaminari::ActionViewExtension

def paginate(scope, options = {}, &block)
end

end

end

end
end

end
end
Add a rake task
#lib/erb2haml.rb

#lib/erb2haml/railties/erb2haml.rake

require 'erb2haml/railtie' if defined?(Rails)

require 'find'

#lib/erb2haml/railtie.rb
require 'erb2haml'
require 'rails'

namespace :haml do
desc "Perform bulk conversion of all html.
erb files to Haml in views folder"
task :replace_erbs do

module ERb2Haml

Find.find("app/views/") do |path|

class Railtie < Rails::Railtie

#convert files

rake_tasks do
load 'erb2haml/railties/erb2haml.rake'
end
end
end

end
end
end
Gem customization approaches
● YAML file
○ locale files

● DSL block
○ before {}, after {}, scriptorator

● Subclassing
○ devise

● Method Override(reopen gem class)
○ less preferred
Gem customization example
class RegistrationsController < Devise::RegistrationsController
protected
def after_update_path_for(resource)
user_path(resource)
end
end
devise_for :users, :controllers => { :registrations => :registrations }
A Good Open Source Contributor
●
●
●
●
●
●

follows conventions others established
cares about backward compatibility
has 100% test coverage of his code
knows when to meta-program
excels at object oriented design
is familiar with various design patterns
Recommended Readings
●
●
●
●

http://api.rubyonrails.org/classes/ActiveSupport/Configurable/ClassMethods.html
http://edgeapi.rubyonrails.org/classes/Rails/Railtie.html
http://edgeapi.rubyonrails.org/classes/Rails/Engine.html
http://railscasts.com/episodes/299-rails-initialization-walkthrough

Contenu connexe

En vedette

En vedette (7)

Competency builder Skill develoment and employbility
Competency builder Skill develoment and employbility Competency builder Skill develoment and employbility
Competency builder Skill develoment and employbility
 
Storyboard
StoryboardStoryboard
Storyboard
 
Printables For Theodora
Printables For TheodoraPrintables For Theodora
Printables For Theodora
 
Stop Doing and Start Being
Stop Doing and Start BeingStop Doing and Start Being
Stop Doing and Start Being
 
Ssoverview
SsoverviewSsoverview
Ssoverview
 
Examen 2ep
Examen 2epExamen 2ep
Examen 2ep
 
Your job
Your jobYour job
Your job
 

Similaire à Creating effective ruby gems

Adding custom ui controls to your application (1)
Adding custom ui controls to your application (1)Adding custom ui controls to your application (1)
Adding custom ui controls to your application (1)Oro Inc.
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to DjangoJoaquim Rocha
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to DjangoJames Casey
 
How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30fiyuer
 
Rails antipatterns
Rails antipatternsRails antipatterns
Rails antipatternsChul Ju Hong
 
Rails antipattern-public
Rails antipattern-publicRails antipattern-public
Rails antipattern-publicChul Ju Hong
 
using Mithril.js + postgREST to build and consume API's
using Mithril.js + postgREST to build and consume API'susing Mithril.js + postgREST to build and consume API's
using Mithril.js + postgREST to build and consume API'sAntônio Roberto Silva
 
Trailblazer Introduction by Nick Sutterer
Trailblazer Introduction by Nick SuttererTrailblazer Introduction by Nick Sutterer
Trailblazer Introduction by Nick SuttererPivorak MeetUp
 
JavaScript Modules Done Right
JavaScript Modules Done RightJavaScript Modules Done Right
JavaScript Modules Done RightMariusz Nowak
 
JRuby, Ruby, Rails and You on the Cloud
JRuby, Ruby, Rails and You on the CloudJRuby, Ruby, Rails and You on the Cloud
JRuby, Ruby, Rails and You on the CloudHiro Asari
 
Ruby On Rails Basics
Ruby On Rails BasicsRuby On Rails Basics
Ruby On Rails BasicsAmit Solanki
 
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)Antonio Peric-Mazar
 
End-to-end web-testing in ruby ecosystem
End-to-end web-testing in ruby ecosystemEnd-to-end web-testing in ruby ecosystem
End-to-end web-testing in ruby ecosystemAlex Mikitenko
 
Rails Engine | Modular application
Rails Engine | Modular applicationRails Engine | Modular application
Rails Engine | Modular applicationmirrec
 
Angular presentation
Angular presentationAngular presentation
Angular presentationMatus Szabo
 
Writing automation tests with python selenium behave pageobjects
Writing automation tests with python selenium behave pageobjectsWriting automation tests with python selenium behave pageobjects
Writing automation tests with python selenium behave pageobjectsLeticia Rss
 

Similaire à Creating effective ruby gems (20)

Adding custom ui controls to your application (1)
Adding custom ui controls to your application (1)Adding custom ui controls to your application (1)
Adding custom ui controls to your application (1)
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30
 
Rails antipatterns
Rails antipatternsRails antipatterns
Rails antipatterns
 
Rails antipattern-public
Rails antipattern-publicRails antipattern-public
Rails antipattern-public
 
using Mithril.js + postgREST to build and consume API's
using Mithril.js + postgREST to build and consume API'susing Mithril.js + postgREST to build and consume API's
using Mithril.js + postgREST to build and consume API's
 
Rails::Engine
Rails::EngineRails::Engine
Rails::Engine
 
Trailblazer Introduction by Nick Sutterer
Trailblazer Introduction by Nick SuttererTrailblazer Introduction by Nick Sutterer
Trailblazer Introduction by Nick Sutterer
 
Django introduction
Django introductionDjango introduction
Django introduction
 
JavaScript Modules Done Right
JavaScript Modules Done RightJavaScript Modules Done Right
JavaScript Modules Done Right
 
JRuby, Ruby, Rails and You on the Cloud
JRuby, Ruby, Rails and You on the CloudJRuby, Ruby, Rails and You on the Cloud
JRuby, Ruby, Rails and You on the Cloud
 
Ruby On Rails Basics
Ruby On Rails BasicsRuby On Rails Basics
Ruby On Rails Basics
 
Wt unit 5
Wt unit 5Wt unit 5
Wt unit 5
 
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
 
End-to-end web-testing in ruby ecosystem
End-to-end web-testing in ruby ecosystemEnd-to-end web-testing in ruby ecosystem
End-to-end web-testing in ruby ecosystem
 
Rails Engine | Modular application
Rails Engine | Modular applicationRails Engine | Modular application
Rails Engine | Modular application
 
Django Vs Rails
Django Vs RailsDjango Vs Rails
Django Vs Rails
 
Angular presentation
Angular presentationAngular presentation
Angular presentation
 
Writing automation tests with python selenium behave pageobjects
Writing automation tests with python selenium behave pageobjectsWriting automation tests with python selenium behave pageobjects
Writing automation tests with python selenium behave pageobjects
 

Dernier

Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 

Dernier (20)

Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 

Creating effective ruby gems

  • 1. SF Software Design in Ruby Meetup Group Ruby Gem Internals by Examples Ben Zhang, 1/14/2014 benzhangpro@gmail.com http://www.meetup.com/Software-Design-in-Ruby-Study-Group/
  • 2. Types of Ruby Gem Features ● Global Singleton ○ typhoeus, twitter ● Single Web Feature (MVC method adding) ○ will_piginate, simple_form ● Mountable Engine ○ rails_admin, resque ● Global Observers ○ airbrake, newrelic_rpm ● Command Line Tool ○ rspec, html2haml
  • 3. Choose a Good Interface will_paginate kaminari Post.paginate(:page => params[:page], Post.order(:name).page(params[:page]). :per_page => 30) class Post < ActiveRecord::Base self.per_page = 10 per_page(30) class Post < ActiveRecord::Base paginates_per 50 end end <%= will_paginate @posts %> <%= paginate @users %>
  • 4. Configuration Options: Outcome Kaminari.configure do |config| config.default_per_page = 25 config.max_per_page = nil end
  • 5. Configuration Options: The How module Kaminari def self.configure(&block) yield @config ||= Kaminari::Configuration.new end def self.config @config end class Configuration include ActiveSupport::Configurable config_accessor :default_per_page {} config_accessor :max_per_page {} end end
  • 6. Gem Initialization I #lib/kaminari.rb require 'kaminari/config' module Kaminari require 'kaminari/helpers/paginator' end require ‘...’ if defined? Rails begin require 'rails' rescue LoadError #do nothing end require 'kaminari/railtie' require 'kaminari/engine' end #lib/kaminari/railtie.rb module Kaminari class Railtie < ::Rails::Railtie if !defined?(Rails) && !defined?(Sinatra) initializer 'kaminari' do |_app| Kaminari::Hooks.init $stderr.puts “no framework detected.” end end end end
  • 7. Gem Initialization II module Kaminari class Hooks def self.init ActiveSupport.on_load(:active_record) do require 'kaminari/models/active_record_extension' ::ActiveRecord::Base.send :include, Kaminari::ActiveRecordExtension end begin; require 'data_mapper'; rescue LoadError; end if defined? ::DataMapper require 'kaminari/models/data_mapper_extension' ::DataMapper::Collection.send :include, Kaminari::DataMapperExtension::Collection end end end end
  • 8. Add methods to controllers module YourGem class Feature def user_agent(request) end end end ApplicationController.send :include, YourGem::Feature class UsersController < ApplicationController def index user_agent(request) end end
  • 9. Add methods to models # in your code class Artist include Mongoid::Document field :name, type: String embeds_many :instruments end # in your gem module Mongoid class Document def self.embeds_many(association) end def self.field(field, options={}) end end end
  • 10. Add methods to views #lib/kaminari.rb #lib/kaminari/hooks.rb require 'kaminari/helpers/action_view_extension' module Kaminari class Hooks def self.init #lib/kaminari/helpers/action_view_extension.rb ActiveSupport.on_load(:action_view) do module Kaminari ::ActionView::Base.send :include, module ActionViewExtension Kaminari::ActionViewExtension def paginate(scope, options = {}, &block) end end end end end end end
  • 11. Add a rake task #lib/erb2haml.rb #lib/erb2haml/railties/erb2haml.rake require 'erb2haml/railtie' if defined?(Rails) require 'find' #lib/erb2haml/railtie.rb require 'erb2haml' require 'rails' namespace :haml do desc "Perform bulk conversion of all html. erb files to Haml in views folder" task :replace_erbs do module ERb2Haml Find.find("app/views/") do |path| class Railtie < Rails::Railtie #convert files rake_tasks do load 'erb2haml/railties/erb2haml.rake' end end end end end end
  • 12. Gem customization approaches ● YAML file ○ locale files ● DSL block ○ before {}, after {}, scriptorator ● Subclassing ○ devise ● Method Override(reopen gem class) ○ less preferred
  • 13. Gem customization example class RegistrationsController < Devise::RegistrationsController protected def after_update_path_for(resource) user_path(resource) end end devise_for :users, :controllers => { :registrations => :registrations }
  • 14. A Good Open Source Contributor ● ● ● ● ● ● follows conventions others established cares about backward compatibility has 100% test coverage of his code knows when to meta-program excels at object oriented design is familiar with various design patterns