SlideShare une entreprise Scribd logo
1  sur  55
Télécharger pour lire hors ligne
Plugins
James Adam
Rails
$ script/plugin install
lib
init.rb
tasks install.rb
uninstall.rb test
Plugins
developing
Rails plugins
can add
anything
Rails plugins
can change
anything
Rails plugins
can do
anything
Methods
Adding
with Ruby, and Rails
Modules
module MineNotYours
def copyright
"(c) me"
end
end
Adding methods to instances...
class SomeClass
include MineNotYours
end
c = SomeClass.new
c.copyright # => "(c) me"
... and so to Models
class SomeModel < AR::Base
include MineNotYours
end
m = SomeModel.find(:first)
m.copyright # => "(c) me"
Adding methods to all models
# Re-opening the target class
# directly
class ActiveRecord::Base
include MineNotYours
end
Adding methods to all models
# Send the ‘include’ message to
# our target class
AR::Base.send(:include, MineNotYours)
Behaviour
Adding
as a part of Class Definitions
Plain Ol’ Ruby Objects
class ConferenceDelegate
attr_accessor :name
end
c = ConferenceDelegate.new
c.name = "Joe Blogs"
c.name # => "Joe Blogs"
Plain Ol’ Ruby Objects
class ConferenceDelegate
:name
end
c = ConferenceDelegate.new
c.name = "Joe Blogs"
c.name # => "Joe Blogs"
attr_accessor
“Class” methods
irb:> c.class.private_methods
=> ["abort", "alias_method", "at_exit", "attr",
"attr_accessor", "attr_reader", "attr_writer",
"binding", "block_given?", "define_method",
"eval", "exec", "exit", "extended", "fail",
"fork", "getc", "gets", "global_variables",
"include", "included", "irb_binding", "lambda",
"load", "local_variables", "private", "proc",
"protected", "public", "puts", "raise",
"remove_class_variable", "remove_const",
"remove_instance_variable", "remove_method",
"require", "sleep", "split", "sprintf", "srand",
"sub", "sub!", "syscall", "system", "test",
"throw", "undef_method", "untrace_var", "warn"]
Ruby Hacker; Know Thy-self
class ConferenceDelegate
attr_accessor :name
end
# self == ConferenceDelegate
Ruby Hacker; Know Thy-self
class ConferenceDelegate
def self.do_something
“OK”
end
do_something
end
# => “OK”
Ruby Hacker; Know Thy-self
class ConferenceDelegate
def self.has_name
attr_accessor :name
end
has_name
end
t = ConferenceDelegate.new
t.name = “Joe Blogs”
Another module
module Personable
def has_person_attributes
attr_accessor :first_name
attr_accessor :last_name
end
end
Adding “class” methods
class RubyGuru
extend Personable
end
has_person_attributes
g = RubyGuru.new
g.first_name = “Dave”
g.last_name = “Thomas”
Specifying Behaviour in Rails
class SuperModel < ActiveRecord::Base
validates_presence_of :rich_boyfriend
validates_size_of :entourage,
:minimum => 20
has_many :vices, :through => :boyfriends
end
class FootballersWife < ActiveRecord::Base
validates_presence_of :rich_boyfriend
validates_size_of :entourage,
:minimum => 5
has_many :vices, :through => :boyfriends
end
Bundling behaviour
module ModelValidation
def acts_as_glamourous(size)
validates_presence_of :rich_boyfriend
validates_size_of :entourage,
:minimum => size
has_many :vices, :through => :boyfriends
end
end
# ... add this method to the target class
ActiveRecord::Base.send(:extend,
ModelValidation)
Our own ActiveRecord behaviour
class Celebrity < AR::Base
acts_as_glamourous(50)
end
Class and instance behaviour
module GlamourPlugin
def acts_as_glamourous(size)
validates_presence_of :rich_boyfriend
# etc...
end
end
Class and instance behaviour
module GlamourPlugin
def acts_as_glamourous
validates_presence_of :rich_boyfriend
# etc...
end
module InstanceBehaviour
def react_to(other_person)
if other_person.is_a?(Paparazzo)
other_person.destroy
end
end
end
end
Class and instance behaviour
module GlamourPlugin
def acts_as_glamourous
validates_presence_of :rich_boyfriend
# etc...
include GlamourPlugin::InstanceBehaviour
end
module InstanceBehaviour
def react_to(other_person)
if other_person.is_a?(Paparazzo)
other_person.destroy
end
end
end
end
Our plugin in action
class Diva < ActiveRecord::Base
acts_as_glamourous
end
dude = Paparazzo.create(:lens => "Huge")
Paparazzo.count # => 1
starlet = Diva.new(:name => "Britney",
:entourage => 873,
:quirk => "No hair")
starlet.react_to(dude)
Paparazzo.count # => 0
RailsPatching
Rails
RailsPatching
Rails
HACKING
changing the behaviour
of existing classes
Changing existing behaviour
# replacing implementation
# via inheritance
class Thing < Object
def object_id
@my_custom_value
end
end
Inheritance is not
always possible
ActiveRecord::Base
ActionController::Base
ActionView::Base
Dependencies ActionMailer::Base
Routing
DispatchingAssociations
... and more...
changing the
implementation of
existing methods
Ruby classes are always open
class ActiveRecord::Base
def count
execute("SELECT COUNT(*)
FROM #{table_name}") / 2
end
end
Aliasing methods
class ActiveRecord::Base
end
alias_method :__count, :count
def count
__count / 2
end
Method chains with Rails
class FunkyBass
def play_it
"Funky"
end
end
bass = FunkyBass.new
bass.play_it # => "Funky"
Method chains - new behaviour
module Soul
def play_it_with_soul
"Smooth & " +
play_it_without_soul
end
end
How alias_method_chain works
class FunkyBass
include Soul
alias_method_chain :play_it, :soul
end
alias_method :play_it_without_soul,
:play_it
alias_method :play_it,
:play_it_with_soul
# underneath the hood:
Adding the new functionality
class FunkyBass
include Soul
alias_method_chain :play_it,
:soul
end
bass.play_it
# => "Smooth & Funky"
Method chains in action
class ActiveRecord::Base
def count_with_fixes
return count_without_fixes + 1
end
alias_method_chain :count, :fixes
end
MyModel.count # calls new method
Patching Rails’ Dependencies
class Dependencies
def require_or_load(file_name)
# load the file from the normal
# places, i.e. $LOAD_PATH
end
end
New plugin-loading behaviour
module LoadingFromPlugins
# we want to replace Rails’ default loading
# behaviour with this
def require_or_load_with_plugins(file_name)
if file_exists_in_plugin(file_name)
load_file_from_plugin(file_name)
else
require_or_load_without_plugins(file_name)
end
end
end
Injecting the new behaviour
module LoadingFromPlugins
def require_or_load_with_plugins(file_name)
if file_exists_in_plugins(file_name)
load_file_from_plugin(file_name)
else
require_or_load_without_plugins(file_name)
end
end
end
def self.included(base)
base.send(:alias_method_chain,
:require_or_load, :plugins)
end
Dependencies.send(:include, LoadingFromPlugins)

Contenu connexe

Similaire à Extending Rails with Plugins (2007)

Ruby Metaprogramming
Ruby MetaprogrammingRuby Metaprogramming
Ruby MetaprogrammingNando Vieira
 
Say Goodbye to Procedural Programming - Nick Sutterer
Say Goodbye to Procedural Programming - Nick SuttererSay Goodbye to Procedural Programming - Nick Sutterer
Say Goodbye to Procedural Programming - Nick SuttererRuby Meditation
 
Model of the colossus @ Rupy Brazil 2013
Model of the colossus @ Rupy Brazil 2013 Model of the colossus @ Rupy Brazil 2013
Model of the colossus @ Rupy Brazil 2013 Mauro George
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy CodeNaresh Jain
 
Lightning talk
Lightning talkLightning talk
Lightning talknpalaniuk
 
Ruby: Beyond the Basics
Ruby: Beyond the BasicsRuby: Beyond the Basics
Ruby: Beyond the BasicsMichael Koby
 
Metaprogramovanie #1
Metaprogramovanie #1Metaprogramovanie #1
Metaprogramovanie #1Jano Suchal
 
Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11Pedro Cunha
 
Intro to Ruby - Twin Cities Code Camp 7
Intro to Ruby - Twin Cities Code Camp 7Intro to Ruby - Twin Cities Code Camp 7
Intro to Ruby - Twin Cities Code Camp 7Brian Hogan
 
OSDC 2009 Rails Turtorial
OSDC 2009 Rails TurtorialOSDC 2009 Rails Turtorial
OSDC 2009 Rails TurtorialYi-Ting Cheng
 
WordPress plugin #2
WordPress plugin #2WordPress plugin #2
WordPress plugin #2giwoolee
 
The Ring programming language version 1.3 book - Part 30 of 88
The Ring programming language version 1.3 book - Part 30 of 88The Ring programming language version 1.3 book - Part 30 of 88
The Ring programming language version 1.3 book - Part 30 of 88Mahmoud Samir Fayed
 
Perl Teach-In (part 2)
Perl Teach-In (part 2)Perl Teach-In (part 2)
Perl Teach-In (part 2)Dave Cross
 
Template rendering in rails
Template rendering in rails Template rendering in rails
Template rendering in rails Hung Wu Lo
 
PHP OOP Lecture - 02.pptx
PHP OOP Lecture - 02.pptxPHP OOP Lecture - 02.pptx
PHP OOP Lecture - 02.pptxAtikur Rahman
 

Similaire à Extending Rails with Plugins (2007) (20)

SOLID Ruby, SOLID Rails
SOLID Ruby, SOLID RailsSOLID Ruby, SOLID Rails
SOLID Ruby, SOLID Rails
 
Ruby Metaprogramming
Ruby MetaprogrammingRuby Metaprogramming
Ruby Metaprogramming
 
Say Goodbye to Procedural Programming - Nick Sutterer
Say Goodbye to Procedural Programming - Nick SuttererSay Goodbye to Procedural Programming - Nick Sutterer
Say Goodbye to Procedural Programming - Nick Sutterer
 
Model of the colossus @ Rupy Brazil 2013
Model of the colossus @ Rupy Brazil 2013 Model of the colossus @ Rupy Brazil 2013
Model of the colossus @ Rupy Brazil 2013
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy Code
 
Lightning talk
Lightning talkLightning talk
Lightning talk
 
Ruby: Beyond the Basics
Ruby: Beyond the BasicsRuby: Beyond the Basics
Ruby: Beyond the Basics
 
Metaprogramovanie #1
Metaprogramovanie #1Metaprogramovanie #1
Metaprogramovanie #1
 
Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11
 
Dsl
DslDsl
Dsl
 
Intro to Ruby - Twin Cities Code Camp 7
Intro to Ruby - Twin Cities Code Camp 7Intro to Ruby - Twin Cities Code Camp 7
Intro to Ruby - Twin Cities Code Camp 7
 
Django Vs Rails
Django Vs RailsDjango Vs Rails
Django Vs Rails
 
Ruby on rails delegate
Ruby on rails delegateRuby on rails delegate
Ruby on rails delegate
 
OSDC 2009 Rails Turtorial
OSDC 2009 Rails TurtorialOSDC 2009 Rails Turtorial
OSDC 2009 Rails Turtorial
 
WordPress plugin #2
WordPress plugin #2WordPress plugin #2
WordPress plugin #2
 
The Ring programming language version 1.3 book - Part 30 of 88
The Ring programming language version 1.3 book - Part 30 of 88The Ring programming language version 1.3 book - Part 30 of 88
The Ring programming language version 1.3 book - Part 30 of 88
 
Module Magic
Module MagicModule Magic
Module Magic
 
Perl Teach-In (part 2)
Perl Teach-In (part 2)Perl Teach-In (part 2)
Perl Teach-In (part 2)
 
Template rendering in rails
Template rendering in rails Template rendering in rails
Template rendering in rails
 
PHP OOP Lecture - 02.pptx
PHP OOP Lecture - 02.pptxPHP OOP Lecture - 02.pptx
PHP OOP Lecture - 02.pptx
 

Plus de lazyatom

Engines: Team Development on Rails (2005)
Engines: Team Development on Rails (2005)Engines: Team Development on Rails (2005)
Engines: Team Development on Rails (2005)lazyatom
 
The Even Darker Art of Rails Engines (2009)
The Even Darker Art of Rails Engines (2009)The Even Darker Art of Rails Engines (2009)
The Even Darker Art of Rails Engines (2009)lazyatom
 
Gem That (2009)
Gem That (2009)Gem That (2009)
Gem That (2009)lazyatom
 
Do we need more test frameworks? (2011)
Do we need more test frameworks? (2011)Do we need more test frameworks? (2011)
Do we need more test frameworks? (2011)lazyatom
 
IoT Printer (2012)
IoT Printer (2012)IoT Printer (2012)
IoT Printer (2012)lazyatom
 
The Even Darker Art Of Rails Engines
The Even Darker Art Of Rails EnginesThe Even Darker Art Of Rails Engines
The Even Darker Art Of Rails Engineslazyatom
 

Plus de lazyatom (6)

Engines: Team Development on Rails (2005)
Engines: Team Development on Rails (2005)Engines: Team Development on Rails (2005)
Engines: Team Development on Rails (2005)
 
The Even Darker Art of Rails Engines (2009)
The Even Darker Art of Rails Engines (2009)The Even Darker Art of Rails Engines (2009)
The Even Darker Art of Rails Engines (2009)
 
Gem That (2009)
Gem That (2009)Gem That (2009)
Gem That (2009)
 
Do we need more test frameworks? (2011)
Do we need more test frameworks? (2011)Do we need more test frameworks? (2011)
Do we need more test frameworks? (2011)
 
IoT Printer (2012)
IoT Printer (2012)IoT Printer (2012)
IoT Printer (2012)
 
The Even Darker Art Of Rails Engines
The Even Darker Art Of Rails EnginesThe Even Darker Art Of Rails Engines
The Even Darker Art Of Rails Engines
 

Dernier

Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 

Dernier (20)

Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 

Extending Rails with Plugins (2007)

  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10. lib
  • 19. Adding methods to instances... class SomeClass include MineNotYours end c = SomeClass.new c.copyright # => "(c) me"
  • 20. ... and so to Models class SomeModel < AR::Base include MineNotYours end m = SomeModel.find(:first) m.copyright # => "(c) me"
  • 21. Adding methods to all models # Re-opening the target class # directly class ActiveRecord::Base include MineNotYours end
  • 22. Adding methods to all models # Send the ‘include’ message to # our target class AR::Base.send(:include, MineNotYours)
  • 23.
  • 24. Behaviour Adding as a part of Class Definitions
  • 25. Plain Ol’ Ruby Objects class ConferenceDelegate attr_accessor :name end c = ConferenceDelegate.new c.name = "Joe Blogs" c.name # => "Joe Blogs"
  • 26. Plain Ol’ Ruby Objects class ConferenceDelegate :name end c = ConferenceDelegate.new c.name = "Joe Blogs" c.name # => "Joe Blogs" attr_accessor
  • 27. “Class” methods irb:> c.class.private_methods => ["abort", "alias_method", "at_exit", "attr", "attr_accessor", "attr_reader", "attr_writer", "binding", "block_given?", "define_method", "eval", "exec", "exit", "extended", "fail", "fork", "getc", "gets", "global_variables", "include", "included", "irb_binding", "lambda", "load", "local_variables", "private", "proc", "protected", "public", "puts", "raise", "remove_class_variable", "remove_const", "remove_instance_variable", "remove_method", "require", "sleep", "split", "sprintf", "srand", "sub", "sub!", "syscall", "system", "test", "throw", "undef_method", "untrace_var", "warn"]
  • 28. Ruby Hacker; Know Thy-self class ConferenceDelegate attr_accessor :name end # self == ConferenceDelegate
  • 29. Ruby Hacker; Know Thy-self class ConferenceDelegate def self.do_something “OK” end do_something end # => “OK”
  • 30. Ruby Hacker; Know Thy-self class ConferenceDelegate def self.has_name attr_accessor :name end has_name end t = ConferenceDelegate.new t.name = “Joe Blogs”
  • 31. Another module module Personable def has_person_attributes attr_accessor :first_name attr_accessor :last_name end end
  • 32. Adding “class” methods class RubyGuru extend Personable end has_person_attributes g = RubyGuru.new g.first_name = “Dave” g.last_name = “Thomas”
  • 33. Specifying Behaviour in Rails class SuperModel < ActiveRecord::Base validates_presence_of :rich_boyfriend validates_size_of :entourage, :minimum => 20 has_many :vices, :through => :boyfriends end class FootballersWife < ActiveRecord::Base validates_presence_of :rich_boyfriend validates_size_of :entourage, :minimum => 5 has_many :vices, :through => :boyfriends end
  • 34. Bundling behaviour module ModelValidation def acts_as_glamourous(size) validates_presence_of :rich_boyfriend validates_size_of :entourage, :minimum => size has_many :vices, :through => :boyfriends end end # ... add this method to the target class ActiveRecord::Base.send(:extend, ModelValidation)
  • 35. Our own ActiveRecord behaviour class Celebrity < AR::Base acts_as_glamourous(50) end
  • 36. Class and instance behaviour module GlamourPlugin def acts_as_glamourous(size) validates_presence_of :rich_boyfriend # etc... end end
  • 37. Class and instance behaviour module GlamourPlugin def acts_as_glamourous validates_presence_of :rich_boyfriend # etc... end module InstanceBehaviour def react_to(other_person) if other_person.is_a?(Paparazzo) other_person.destroy end end end end
  • 38. Class and instance behaviour module GlamourPlugin def acts_as_glamourous validates_presence_of :rich_boyfriend # etc... include GlamourPlugin::InstanceBehaviour end module InstanceBehaviour def react_to(other_person) if other_person.is_a?(Paparazzo) other_person.destroy end end end end
  • 39. Our plugin in action class Diva < ActiveRecord::Base acts_as_glamourous end dude = Paparazzo.create(:lens => "Huge") Paparazzo.count # => 1 starlet = Diva.new(:name => "Britney", :entourage => 873, :quirk => "No hair") starlet.react_to(dude) Paparazzo.count # => 0
  • 42. changing the behaviour of existing classes
  • 43. Changing existing behaviour # replacing implementation # via inheritance class Thing < Object def object_id @my_custom_value end end
  • 44. Inheritance is not always possible ActiveRecord::Base ActionController::Base ActionView::Base Dependencies ActionMailer::Base Routing DispatchingAssociations ... and more...
  • 46. Ruby classes are always open class ActiveRecord::Base def count execute("SELECT COUNT(*) FROM #{table_name}") / 2 end end
  • 47. Aliasing methods class ActiveRecord::Base end alias_method :__count, :count def count __count / 2 end
  • 48. Method chains with Rails class FunkyBass def play_it "Funky" end end bass = FunkyBass.new bass.play_it # => "Funky"
  • 49. Method chains - new behaviour module Soul def play_it_with_soul "Smooth & " + play_it_without_soul end end
  • 50. How alias_method_chain works class FunkyBass include Soul alias_method_chain :play_it, :soul end alias_method :play_it_without_soul, :play_it alias_method :play_it, :play_it_with_soul # underneath the hood:
  • 51. Adding the new functionality class FunkyBass include Soul alias_method_chain :play_it, :soul end bass.play_it # => "Smooth & Funky"
  • 52. Method chains in action class ActiveRecord::Base def count_with_fixes return count_without_fixes + 1 end alias_method_chain :count, :fixes end MyModel.count # calls new method
  • 53. Patching Rails’ Dependencies class Dependencies def require_or_load(file_name) # load the file from the normal # places, i.e. $LOAD_PATH end end
  • 54. New plugin-loading behaviour module LoadingFromPlugins # we want to replace Rails’ default loading # behaviour with this def require_or_load_with_plugins(file_name) if file_exists_in_plugin(file_name) load_file_from_plugin(file_name) else require_or_load_without_plugins(file_name) end end end
  • 55. Injecting the new behaviour module LoadingFromPlugins def require_or_load_with_plugins(file_name) if file_exists_in_plugins(file_name) load_file_from_plugin(file_name) else require_or_load_without_plugins(file_name) end end end def self.included(base) base.send(:alias_method_chain, :require_or_load, :plugins) end Dependencies.send(:include, LoadingFromPlugins)