Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
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 (...
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

•   Bu...
What’s Included?
•   Full MVC Components

•   Assets

•   Routes

•   Full Namespacing

•   Hot Code Loading /
    Unloadi...
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;

 ...
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://localhos...
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(:*...
Customize It
•   It’s just ruby. Monkey Patch it

•   Use the “patch” rake task

•   Slice code is in your app:

    •   M...
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/contr...
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 template...
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...
Custom Models
Custom Models

Stubs or Freeze?

$ rake slices:blog_slice:freeze:models
Custom Models
                                         module BlogSlice
                                           class P...
Custom Routes
Merb::Router.prepare do

  add_slice(:blog_slice, :path_prefix => quot;blogquot;, :default_routes => nil) do...
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)



• Set...
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
  ...
Some Important Files
blog_slice/lib
   blog_slice
     merbtasks.rb
     slicetasks.rb   < Here be dragons

     spectasks...
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 C...
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(:p...
Models
Namespace your models (you don’t have to)

 class BlogSlice::Post
   include DataMapper::Resource

   property   :i...
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
    re...
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::Slice...
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 stan...
Distribute It

• rake gemspec

• rake install
Questions?
You’ve finished this document.
Download and read it offline.
Upcoming SlideShare
Tips and Tricks for your Service Oriented Architecture @ CakeFest 2013 in San Francisco
Next
Upcoming SlideShare
Tips and Tricks for your Service Oriented Architecture @ CakeFest 2013 in San Francisco
Next
Download to read offline and view in fullscreen.

Share

Merb Slices

Download to read offline

Presentation given at merb-camp 08 outlining merb-slices

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

Merb Slices

  1. 1. Merb Slices
  2. 2. 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...)
  3. 3. Who Am I? • Daniel Neighman a.k.a. hassox • Engine Yarder • Proud Aussie
  4. 4. What’s a Merb Slice?
  5. 5. What’s a Merb Slice? • Full Stack Mini Merb Application • Designed to be shared between applications • Built on Merbs Public API
  6. 6. What’s Included? • Full MVC Components • Assets • Routes • Full Namespacing • Hot Code Loading / Unloading • Distribution as Gems
  7. 7. Slice It
  8. 8. Slice It • Get it: ➡ $ sudo gem install blog_slice
  9. 9. Slice It • Get it: ➡ $ sudo gem install blog_slice • Install it: ➡ $ rake slices:blog_slice:install
  10. 10. Configure It
  11. 11. Configure It config/init.rb dependency quot;merb-slicesquot; dependency quot;blog_slicequot;
  12. 12. Configure It config/init.rb dependency quot;merb-slicesquot; dependency quot;blog_slicequot; config/routes.rb Merb::Router.prepare do all_slices end
  13. 13. Run It
  14. 14. Run It $ merb
  15. 15. Use It
  16. 16. Use It • By Default, your slice is available at: • http://localhost:4000/blog_slice
  17. 17. Use It • By Default, your slice is available at: • http://localhost:4000/blog_slice • Get the posts: • http://localhost:4000/blog_slice/posts
  18. 18. Rake It
  19. 19. Rake It There are many rake tasks available
  20. 20. Rake It There are many rake tasks available
  21. 21. Rake It There are many rake tasks available • rake slices
  22. 22. Rake It There are many rake tasks available • rake slices • rake slices:install_as_gem
  23. 23. Rake It There are many rake tasks available • rake slices • rake slices:install_as_gem • rake -T slices:blog_slice
  24. 24. 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
  25. 25. 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
  26. 26. Custom Options
  27. 27. Custom Options • Each Slice has a Configuration Hash • Merb::Slices::config[:blog_slice]
  28. 28. Custom Options • Each Slice has a Configuration Hash • Merb::Slices::config[:blog_slice] • Aliased To: • BlogSlice[]
  29. 29. Custom Actions
  30. 30. Custom Actions Adding the Action to the controller $ rake slices:blog_slice:patch
  31. 31. 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
  32. 32. Custom Views
  33. 33. Custom Views $ rake slices:blog_slice:freeze:views
  34. 34. Custom Views $ rake slices:blog_slice:freeze:views Available in: Merb.root/slices/blog_slice/app/views
  35. 35. Custom Views $ rake slices:blog_slice:freeze:views Available in: Merb.root/slices/blog_slice/app/views Add templates for additional formats
  36. 36. Custom Layout
  37. 37. Custom Layout • By Default the Slice layout is used
  38. 38. Custom Layout • By Default the Slice layout is used Merb::BootLoader.after_app_loads do BlogSlice[:layout] = :blog_slice_layout end
  39. 39. Custom Models
  40. 40. Custom Models Stubs or Freeze? $ rake slices:blog_slice:freeze:models
  41. 41. 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
  42. 42. 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
  43. 43. Prefix URL Paths
  44. 44. Prefix URL Paths • Setup a Path Prefix • add_slice(:blog_slice, quot;awesomequot;) • Example: /awesome/posts
  45. 45. Prefix Named Routes
  46. 46. Prefix Named Routes • Default name_prefix on named routes: • add_slice(:blog_slice) • url(:blog_slice_posts)
  47. 47. 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)
  48. 48. Write It
  49. 49. Development Flow
  50. 50. Development Flow •Generate •Write •Use •Write •Use •Finish - Install
  51. 51. Development Flow •Generate •Write •Use •Write •Use $ slice •Finish - Install
  52. 52. Generate It $ merb-gen slice blog_slice
  53. 53. 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
  54. 54. Some Important Files blog_slice/lib blog_slice merbtasks.rb slicetasks.rb < Here be dragons spectasks.rb blog_slice.rb
  55. 55. Initialize It
  56. 56. Initialize It • blog_slice.rb is where the slice initializes
  57. 57. Initialize It • blog_slice.rb is where the slice initializes • Dependencies
  58. 58. Initialize It • blog_slice.rb is where the slice initializes • Dependencies • Router
  59. 59. Initialize It • blog_slice.rb is where the slice initializes • Dependencies • Router • Hooks
  60. 60. Fake It • Setup a fake “host app” env in config/init.rb • config/init.rb is not used normally
  61. 61. Controllers
  62. 62. Controllers • blog_slice/app/controllers/posts.rb class BlogSlice::Posts < BlogSlice::Application # Your Controller Code Here end # Posts
  63. 63. Views • blog_slice/app/views/posts/show.html.haml • blog_slice/app/views/layouts/application.html.haml
  64. 64. Layouts • blog_slice/lib/blog_slice.rb Merb::Slices::config[:blog_slice][:layout] ||= :blog_slice
  65. 65. slice_url
  66. 66. 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)
  67. 67. 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
  68. 68. Assets
  69. 69. Assets • blog_slice/public • images, css, javascipt • Read: blog_slice/app/helpers/application_helper.rb
  70. 70. Images • blog_slice/public/images • Helper: image_path(image)
  71. 71. Javascript • blog_slice/public/javascripts • Helper: javascript_path(javascript)
  72. 72. Stylesheets • blog_slice/public/stylesheets • Helper: stylesheet_path(style)
  73. 73. Install Assets • rake slices:blog_slice:copy_assets
  74. 74. 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
  75. 75. Hook It
  76. 76. Hook It • loaded
  77. 77. Hook It • loaded • init
  78. 78. Hook It • loaded • init • activate
  79. 79. Hook It • loaded • init • activate • deactivate
  80. 80. Hooks - loaded • Slice Code Loads • Hook - loaded • Boot Loader LoadClasses
  81. 81. Hooks - init, activate • Slice Hook - init • BootLoader AfterAppLoads • Slice Hook - activate: triggered by Merb::Slices.activate(BlogSlice)
  82. 82. Hooks - deactivate • Triggered by Merb::Slices.deactivate(BlogSlice)
  83. 83. Spec It • Specs go in blog_slice/spec • rake -T spec • Spec your slice like an app
  84. 84. Spec It - Setup • Setup the routes: before :all do Merb::Router.prepare do add_slice(:blog_slice) end if standalone? end
  85. 85. Distribute It • rake gemspec • rake install
  86. 86. Questions?
  • anacron

    Jul. 29, 2010
  • qinyu

    Dec. 17, 2008
  • shanesveller

    Nov. 20, 2008

Presentation given at merb-camp 08 outlining merb-slices

Views

Total views

6,307

On Slideshare

0

From embeds

0

Number of embeds

14

Actions

Downloads

91

Shares

0

Comments

0

Likes

3

×