SlideShare ist ein Scribd-Unternehmen logo
1 von 86
Downloaden Sie, um offline zu lesen
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
Congure It
Congure It
                           cong/init.rb

dependency quot;merb-slicesquot;
dependency quot;blog_slicequot;
Congure It
                            cong/init.rb

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

                           cong/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
Prex URL Paths
Prex URL Paths


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

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

 •   url(:blog_slice_posts)
Prex 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?

Weitere ähnliche Inhalte

Was ist angesagt?

Intro to Rack
Intro to RackIntro to Rack
Intro to Rack
Rubyc Slides
 
NLIT 2011: Chef & Capistrano
NLIT 2011: Chef & CapistranoNLIT 2011: Chef & Capistrano
NLIT 2011: Chef & Capistrano
nickblah
 

Was ist angesagt? (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
 

Ähnlich wie 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
 
Performance
PerformancePerformance
Performance
Derek Kraan
 
Capistrano Overview
Capistrano OverviewCapistrano Overview
Capistrano Overview
Travis Roberts
 
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
 

Ähnlich wie 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
 

KĂźrzlich hochgeladen

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

KĂźrzlich hochgeladen (20)

ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 

Merb Slices