SlideShare a Scribd company logo
1 of 86
Download to read offline
Merb Slices
Slice By Who?

• Fabien Franzen a.k.a. loob2
• Actually remembers Merb 0.0.0.1 a.k.a ‘the pastie’
• Representing Belgium (or maybe not...)
Who Am I?
• Daniel Neighman a.k.a. hassox
• Engine Yarder
• Proud Aussie
What’s a Merb Slice?
What’s a Merb Slice?

•   Full Stack Mini Merb
    Application

•   Designed to be shared
    between applications

•   Built on Merbs Public
    API
What’s Included?
•   Full MVC Components

•   Assets

•   Routes

•   Full Namespacing

•   Hot Code Loading /
    Unloading

•   Distribution as Gems
Slice It
Slice It

• Get it:
➡   $ sudo gem install blog_slice
Slice It

• Get it:
➡   $ sudo gem install blog_slice



•   Install it:
➡   $ rake slices:blog_slice:install
Configure It
Configure It
                           config/init.rb

dependency quot;merb-slicesquot;
dependency quot;blog_slicequot;
Configure It
                            config/init.rb

dependency quot;merb-slicesquot;
dependency quot;blog_slicequot;

                           config/routes.rb

Merb::Router.prepare do
  all_slices
end
Run It
Run It


$ merb
Use It
Use It

• By Default, your slice is available at:
 • http://localhost:4000/blog_slice
Use It

• By Default, your slice is available at:
 • http://localhost:4000/blog_slice

• Get the posts:
 • http://localhost:4000/blog_slice/posts
Rake It
Rake It
There are many rake tasks available
Rake It
There are many rake tasks available
Rake It
There are many rake tasks available


•   rake slices
Rake It
There are many rake tasks available


•   rake slices
•   rake slices:install_as_gem
Rake It
There are many rake tasks available


•   rake slices
•   rake slices:install_as_gem
•   rake -T slices:blog_slice
Common Rake Tasks
• rake slices:blog_slice:copy_assets

• rake slices:blog_slice:patch

• rake slices:blog_slice:freeze(:*)

• rake slices:blog_slice:migrate

• rake slices:blog_slice:spec
Customize It
•   It’s just ruby. Monkey Patch it

•   Use the “patch” rake task

•   Slice code is in your app:

    •   Merb.root/slices/blog_slice
Custom Options
Custom Options

• Each Slice has a Configuration Hash
•   Merb::Slices::config[:blog_slice]
Custom Options

• Each Slice has a Configuration Hash
•   Merb::Slices::config[:blog_slice]




• Aliased To:
•   BlogSlice[]
Custom Actions
Custom Actions
Adding the Action to the controller
$ rake slices:blog_slice:patch
Custom Actions
Adding the Action to the controller
$ rake slices:blog_slice:patch



Edit: Merb.root/blog_slices/app/controllers/posts.rb

def publish
  @post = Post.first(:permalink => params[:permalink])
  if @post.publish!
    redirect slice_url(:post, @post), :message => quot;Posts Publishedquot;
  else
    render :edit
  end
end
Custom Views
Custom Views
$ rake slices:blog_slice:freeze:views
Custom Views
$ rake slices:blog_slice:freeze:views


Available in:
   Merb.root/slices/blog_slice/app/views
Custom Views
$ rake slices:blog_slice:freeze:views


Available in:
   Merb.root/slices/blog_slice/app/views


Add templates for additional formats
Custom Layout
Custom Layout

• By Default the Slice layout is used
Custom Layout

• By Default the Slice layout is used
  Merb::BootLoader.after_app_loads do

    BlogSlice[:layout] = :blog_slice_layout
  end
Custom Models
Custom Models

Stubs or Freeze?

$ rake slices:blog_slice:freeze:models
Custom Models
                                         module BlogSlice
                                           class Post

                                            property :published, Boolean
                                            property :published_at, DateTime
Stubs or Freeze?
                                             def publish!
                                               self.published = true
$ rake slices:blog_slice:freeze:models         self.published_at = DateTime.now
                                               save
                                             end

                                           end # Post
                                         end # BlogSlice
Custom Routes
Merb::Router.prepare do

  add_slice(:blog_slice, :path_prefix => quot;blogquot;, :default_routes => nil) do
    identify BlogSlice::Post => :permalink do
      match(quot;/posts/:permalink/publishquot;).
        to(:controller => quot;postsquot;, :action => quot;publishquot;).
        name(:publish)
    end
  end

end
Prefix URL Paths
Prefix URL Paths


• Setup a Path Prefix
•   add_slice(:blog_slice, quot;awesomequot;)

•   Example: /awesome/posts
Prefix Named Routes
Prefix Named Routes
• Default name_prefix on named routes:
 •   add_slice(:blog_slice)

 •   url(:blog_slice_posts)
Prefix Named Routes
• Default name_prefix on named routes:
 •   add_slice(:blog_slice)

 •   url(:blog_slice_posts)



• Setup a Name Prefix
 •   add_slice(:blog_slice, :name_prefix => quot;blogquot;)

 •   url(:blog_posts)
Write It
Development Flow
Development Flow

•Generate
•Write
•Use
•Write
•Use
•Finish - Install
Development Flow

•Generate
•Write
•Use
•Write
•Use
                    $ slice
•Finish - Install
Generate It


$ merb-gen slice blog_slice
New Slice Structure
blog_slice           blog_slice
   |-app                |-public
   |---controllers      |---images
   |---helpers          |---javascripts
   |---models           |---stylesheets
   |---views            |-spec
   |-----layout         |---controllers
   |-----main           |-stubs
   |-lib                |---app
   |---blog_slice       |-----controllers
   |-pkg                |-----models
Some Important Files
blog_slice/lib
   blog_slice
     merbtasks.rb
     slicetasks.rb   < Here be dragons

     spectasks.rb
   blog_slice.rb
Initialize It
Initialize It

• blog_slice.rb is where the slice initializes
Initialize It

• blog_slice.rb is where the slice initializes
 • Dependencies
Initialize It

• blog_slice.rb is where the slice initializes
 • Dependencies
 • Router
Initialize It

• blog_slice.rb is where the slice initializes
 • Dependencies
 • Router
 • Hooks
Fake It


• Setup a fake “host app” env in config/init.rb
• config/init.rb is not used normally
Controllers
Controllers
•    blog_slice/app/controllers/posts.rb


    class BlogSlice::Posts < BlogSlice::Application

      # Your Controller Code Here

    end # Posts
Views


•   blog_slice/app/views/posts/show.html.haml

•   blog_slice/app/views/layouts/application.html.haml
Layouts


• blog_slice/lib/blog_slice.rb
 Merb::Slices::config[:blog_slice][:layout] ||= :blog_slice
slice_url
slice_url
• Use slice_url for url generation inside a slice
• slice_url(:controller => ..., :action => ...)
• slice_url(:post, @post)
• slice_url(:merb_auth_slice_password, :login)
Models
Namespace your models (you don’t have to)

 class BlogSlice::Post
   include DataMapper::Resource

   property   :id,         Serial
   property   :title,      String, :lenth => 255
   property   :body,       Text
   property   :slug,       Slug

   before :save do
     self.slug = self.title unless self.permalink
   end

 end # BlogSlice::Post
Assets
Assets

• blog_slice/public
       • images, css, javascipt

• Read:
  blog_slice/app/helpers/application_helper.rb
Images


• blog_slice/public/images
• Helper: image_path(image)
Javascript


• blog_slice/public/javascripts
• Helper: javascript_path(javascript)
Stylesheets


• blog_slice/public/stylesheets
• Helper: stylesheet_path(style)
Install Assets


• rake slices:blog_slice:copy_assets
Route It
blog_slice/lib/blog_slice.rb (the init.rb)

def self.setup_router(scope)
  scope.identify Post => :slug do
    resource :posts
  end
end
Hook It
Hook It

• loaded
Hook It

• loaded
• init
Hook It

• loaded
• init
• activate
Hook It

• loaded
• init
• activate
• deactivate
Hooks - loaded

• Slice Code Loads
• Hook - loaded
• Boot Loader LoadClasses
Hooks - init, activate

• Slice Hook - init
• BootLoader AfterAppLoads
• Slice Hook - activate: triggered by
  Merb::Slices.activate(BlogSlice)
Hooks - deactivate


• Triggered by
  Merb::Slices.deactivate(BlogSlice)
Spec It

• Specs go in blog_slice/spec
• rake -T spec
• Spec your slice like an app
Spec It - Setup

• Setup the routes:
 before :all do
   Merb::Router.prepare do
     add_slice(:blog_slice)
   end if standalone?
 end
Distribute It

• rake gemspec

• rake install
Questions?

More Related Content

What's hot

NLIT 2011: Chef & Capistrano
NLIT 2011: Chef & CapistranoNLIT 2011: Chef & Capistrano
NLIT 2011: Chef & Capistrano
nickblah
 

What's hot (20)

Mojolicious
MojoliciousMojolicious
Mojolicious
 
Building web framework with Rack
Building web framework with RackBuilding web framework with Rack
Building web framework with Rack
 
Vagrant WordCamp Hamilton
Vagrant  WordCamp HamiltonVagrant  WordCamp Hamilton
Vagrant WordCamp Hamilton
 
Debugging on rails
Debugging on railsDebugging on rails
Debugging on rails
 
Ruby off Rails (japanese)
Ruby off Rails (japanese)Ruby off Rails (japanese)
Ruby off Rails (japanese)
 
Sinatra Rack And Middleware
Sinatra Rack And MiddlewareSinatra Rack And Middleware
Sinatra Rack And Middleware
 
Mojolicious and REST
Mojolicious and RESTMojolicious and REST
Mojolicious and REST
 
Ruby MVC from scratch with Rack
Ruby MVC from scratch with RackRuby MVC from scratch with Rack
Ruby MVC from scratch with Rack
 
Intro to Rails
Intro to Rails Intro to Rails
Intro to Rails
 
WordCamp Ann Arbor 2015 Introduction to Backbone + WP REST API
WordCamp Ann Arbor 2015 Introduction to Backbone + WP REST APIWordCamp Ann Arbor 2015 Introduction to Backbone + WP REST API
WordCamp Ann Arbor 2015 Introduction to Backbone + WP REST API
 
Optimising Your Front End Workflow With Symfony, Twig, Bower and Gulp
Optimising Your Front End Workflow With Symfony, Twig, Bower and GulpOptimising Your Front End Workflow With Symfony, Twig, Bower and Gulp
Optimising Your Front End Workflow With Symfony, Twig, Bower and Gulp
 
Apache and PHP: Why httpd.conf is your new BFF!
Apache and PHP: Why httpd.conf is your new BFF!Apache and PHP: Why httpd.conf is your new BFF!
Apache and PHP: Why httpd.conf is your new BFF!
 
Ruby on Rails - Introduction
Ruby on Rails - IntroductionRuby on Rails - Introduction
Ruby on Rails - Introduction
 
Capistrano2
Capistrano2Capistrano2
Capistrano2
 
Strangers In The Night: Ruby, Rack y Sinatra - Herramientas potentes para con...
Strangers In The Night: Ruby, Rack y Sinatra - Herramientas potentes para con...Strangers In The Night: Ruby, Rack y Sinatra - Herramientas potentes para con...
Strangers In The Night: Ruby, Rack y Sinatra - Herramientas potentes para con...
 
Intro to Rack
Intro to RackIntro to Rack
Intro to Rack
 
Rails Girls: Programming, Web Applications and Ruby on Rails
Rails Girls: Programming, Web Applications and Ruby on RailsRails Girls: Programming, Web Applications and Ruby on Rails
Rails Girls: Programming, Web Applications and Ruby on Rails
 
Cloud Automation with Opscode Chef
Cloud Automation with Opscode ChefCloud Automation with Opscode Chef
Cloud Automation with Opscode Chef
 
The DOM is a Mess @ Yahoo
The DOM is a Mess @ YahooThe DOM is a Mess @ Yahoo
The DOM is a Mess @ Yahoo
 
NLIT 2011: Chef & Capistrano
NLIT 2011: Chef & CapistranoNLIT 2011: Chef & Capistrano
NLIT 2011: Chef & Capistrano
 

Similar to Merb Slices

OSDC 2009 Rails Turtorial
OSDC 2009 Rails TurtorialOSDC 2009 Rails Turtorial
OSDC 2009 Rails Turtorial
Yi-Ting Cheng
 
Railsconf2011 deployment tips_for_slideshare
Railsconf2011 deployment tips_for_slideshareRailsconf2011 deployment tips_for_slideshare
Railsconf2011 deployment tips_for_slideshare
tomcopeland
 
Deploying Rails Applications with Capistrano
Deploying Rails Applications with CapistranoDeploying Rails Applications with Capistrano
Deploying Rails Applications with Capistrano
Almir Mendes
 
Your own (little) gem: building an online business with Ruby
Your own (little) gem: building an online business with RubyYour own (little) gem: building an online business with Ruby
Your own (little) gem: building an online business with Ruby
Lindsay Holmwood
 
Curscatalyst
CurscatalystCurscatalyst
Curscatalyst
Kar Juan
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworks
diego_k
 

Similar to Merb Slices (20)

Deliverance: Plone theming without the learning curve from Plone Symposium Ea...
Deliverance: Plone theming without the learning curve from Plone Symposium Ea...Deliverance: Plone theming without the learning curve from Plone Symposium Ea...
Deliverance: Plone theming without the learning curve from Plone Symposium Ea...
 
OSDC 2009 Rails Turtorial
OSDC 2009 Rails TurtorialOSDC 2009 Rails Turtorial
OSDC 2009 Rails Turtorial
 
Capistrano
CapistranoCapistrano
Capistrano
 
Practical Chef and Capistrano for Your Rails App
Practical Chef and Capistrano for Your Rails AppPractical Chef and Capistrano for Your Rails App
Practical Chef and Capistrano for Your Rails App
 
Rails Engine | Modular application
Rails Engine | Modular applicationRails Engine | Modular application
Rails Engine | Modular application
 
Railsconf2011 deployment tips_for_slideshare
Railsconf2011 deployment tips_for_slideshareRailsconf2011 deployment tips_for_slideshare
Railsconf2011 deployment tips_for_slideshare
 
Writing Software not Code with Cucumber
Writing Software not Code with CucumberWriting Software not Code with Cucumber
Writing Software not Code with Cucumber
 
Rails::Engine
Rails::EngineRails::Engine
Rails::Engine
 
Damage Control
Damage ControlDamage Control
Damage Control
 
Umleitung: a tiny mochiweb/CouchDB app
Umleitung: a tiny mochiweb/CouchDB appUmleitung: a tiny mochiweb/CouchDB app
Umleitung: a tiny mochiweb/CouchDB app
 
Deploying Rails Applications with Capistrano
Deploying Rails Applications with CapistranoDeploying Rails Applications with Capistrano
Deploying Rails Applications with Capistrano
 
Deploy like a pro!
Deploy like a pro!Deploy like a pro!
Deploy like a pro!
 
Performance
PerformancePerformance
Performance
 
Capistrano Overview
Capistrano OverviewCapistrano Overview
Capistrano Overview
 
Your own (little) gem: building an online business with Ruby
Your own (little) gem: building an online business with RubyYour own (little) gem: building an online business with Ruby
Your own (little) gem: building an online business with Ruby
 
Deploying Symfony | symfony.cat
Deploying Symfony | symfony.catDeploying Symfony | symfony.cat
Deploying Symfony | symfony.cat
 
Capistrano
CapistranoCapistrano
Capistrano
 
Curscatalyst
CurscatalystCurscatalyst
Curscatalyst
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworks
 
Snakes on a Treadmill
Snakes on a TreadmillSnakes on a Treadmill
Snakes on a Treadmill
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
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
Enterprise Knowledge
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
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
 
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
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony 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...
 
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
 
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...
 
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
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
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
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
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...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

Merb Slices