merb-slices 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. data/Generators +4 -0
  2. data/LICENSE +20 -0
  3. data/README +102 -0
  4. data/Rakefile +65 -0
  5. data/TODO +0 -0
  6. data/lib/generators/base.rb +21 -0
  7. data/lib/generators/full.rb +21 -0
  8. data/lib/generators/templates/full/LICENSE +20 -0
  9. data/lib/generators/templates/full/README +170 -0
  10. data/lib/generators/templates/full/Rakefile +48 -0
  11. data/lib/generators/templates/full/TODO +15 -0
  12. data/lib/generators/templates/full/app/controllers/application.rb +5 -0
  13. data/lib/generators/templates/full/app/controllers/main.rb +7 -0
  14. data/lib/generators/templates/full/app/helpers/application_helper.rb +64 -0
  15. data/lib/generators/templates/full/app/views/layout/%symbol_name%.html.erb +16 -0
  16. data/lib/generators/templates/full/app/views/main/index.html.erb +1 -0
  17. data/lib/generators/templates/full/lib/%base_name%.rb +78 -0
  18. data/lib/generators/templates/full/lib/%base_name%/merbtasks.rb +166 -0
  19. data/lib/generators/templates/full/lib/%base_name%/slicetasks.rb +18 -0
  20. data/lib/generators/templates/full/public/javascripts/master.js +0 -0
  21. data/lib/generators/templates/full/public/stylesheets/master.css +2 -0
  22. data/lib/generators/templates/full/spec/%base_name%_spec.rb +130 -0
  23. data/lib/generators/templates/full/spec/controllers/main_spec.rb +61 -0
  24. data/lib/generators/templates/full/spec/spec_helper.rb +44 -0
  25. data/lib/generators/templates/full/stubs/app/controllers/application.rb +2 -0
  26. data/lib/generators/templates/full/stubs/app/controllers/main.rb +2 -0
  27. data/lib/generators/templates/thin/LICENSE +20 -0
  28. data/lib/generators/templates/thin/README +130 -0
  29. data/lib/generators/templates/thin/Rakefile +46 -0
  30. data/lib/generators/templates/thin/TODO +7 -0
  31. data/lib/generators/templates/thin/application.rb +36 -0
  32. data/lib/generators/templates/thin/lib/%base_name%.rb +93 -0
  33. data/lib/generators/templates/thin/lib/%base_name%/merbtasks.rb +106 -0
  34. data/lib/generators/templates/thin/lib/%base_name%/slicetasks.rb +18 -0
  35. data/lib/generators/templates/thin/public/javascripts/master.js +0 -0
  36. data/lib/generators/templates/thin/public/stylesheets/master.css +2 -0
  37. data/lib/generators/templates/thin/stubs/application.rb +9 -0
  38. data/lib/generators/templates/thin/views/layout/%symbol_name%.html.erb +16 -0
  39. data/lib/generators/templates/thin/views/main/index.html.erb +1 -0
  40. data/lib/generators/templates/very_thin/LICENSE +20 -0
  41. data/lib/generators/templates/very_thin/README +110 -0
  42. data/lib/generators/templates/very_thin/Rakefile +46 -0
  43. data/lib/generators/templates/very_thin/TODO +7 -0
  44. data/lib/generators/templates/very_thin/application.rb +36 -0
  45. data/lib/generators/templates/very_thin/lib/%base_name%.rb +89 -0
  46. data/lib/generators/templates/very_thin/lib/%base_name%/merbtasks.rb +106 -0
  47. data/lib/generators/templates/very_thin/lib/%base_name%/slicetasks.rb +18 -0
  48. data/lib/generators/thin.rb +21 -0
  49. data/lib/generators/very_thin.rb +21 -0
  50. data/lib/merb-slices.rb +126 -0
  51. data/lib/merb-slices/controller_mixin.rb +129 -0
  52. data/lib/merb-slices/merbtasks.rb +19 -0
  53. data/lib/merb-slices/module.rb +338 -0
  54. data/lib/merb-slices/module_mixin.rb +535 -0
  55. data/lib/merb-slices/router_ext.rb +75 -0
  56. data/spec/full_slice_generator_spec.rb +21 -0
  57. data/spec/merb-slice_spec.rb +7 -0
  58. data/spec/slice_generator_spec.rb +22 -0
  59. data/spec/spec_helper.rb +9 -0
  60. data/spec/thin_slice_generator_spec.rb +21 -0
  61. data/spec/very_thin_slice_generator_spec.rb +21 -0
  62. metadata +157 -0
@@ -0,0 +1,61 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe "<%= module_name %>::Main (controller)" do
4
+
5
+ # Feel free to remove the specs below
6
+
7
+ before :all do
8
+ Merb::Router.prepare { |r| r.add_slice(:<%= module_name %>) } if standalone?
9
+ end
10
+
11
+ after :all do
12
+ Merb::Router.reset! if standalone?
13
+ end
14
+
15
+ it "should have access to the slice module" do
16
+ controller = dispatch_to(<%= module_name %>::Main, :index)
17
+ controller.slice.should == <%= module_name %>
18
+ controller.slice.should == <%= module_name %>::Main.slice
19
+ end
20
+
21
+ it "should have an index action" do
22
+ controller = dispatch_to(<%= module_name %>::Main, :index)
23
+ controller.status.should == 200
24
+ controller.body.should contain('<%= module_name %>')
25
+ end
26
+
27
+ it "should work with the default route" do
28
+ controller = get("/<%= base_name %>/main/index")
29
+ controller.should be_kind_of(<%= module_name %>::Main)
30
+ controller.action_name.should == 'index'
31
+ end
32
+
33
+ it "should work with the example named route" do
34
+ controller = get("/<%= base_name %>/index.html")
35
+ controller.should be_kind_of(<%= module_name %>::Main)
36
+ controller.action_name.should == 'index'
37
+ end
38
+
39
+ it "should have routes in <%= module_name %>.routes" do
40
+ <%= module_name %>.routes.should_not be_empty
41
+ end
42
+
43
+ it "should have a slice_url helper method for slice-specific routes" do
44
+ controller = dispatch_to(<%= module_name %>::Main, 'index')
45
+ controller.slice_url(:action => 'show', :format => 'html').should == "/<%= base_name %>/main/show.html"
46
+ controller.slice_url(:<%= symbol_name %>_index, :format => 'html').should == "/<%= base_name %>/index.html"
47
+ end
48
+
49
+ it "should have helper methods for dealing with public paths" do
50
+ controller = dispatch_to(<%= module_name %>::Main, :index)
51
+ controller.public_path_for(:image).should == "/slices/<%= base_name %>/images"
52
+ controller.public_path_for(:javascript).should == "/slices/<%= base_name %>/javascripts"
53
+ controller.public_path_for(:stylesheet).should == "/slices/<%= base_name %>/stylesheets"
54
+ end
55
+
56
+ it "should have a slice-specific _template_root" do
57
+ <%= module_name %>::Main._template_root.should == <%= module_name %>.dir_for(:view)
58
+ <%= module_name %>::Main._template_root.should == <%= module_name %>::Application._template_root
59
+ end
60
+
61
+ end
@@ -0,0 +1,44 @@
1
+ require 'rubygems'
2
+ require 'merb-core'
3
+ require 'merb-slices'
4
+ require 'spec'
5
+
6
+ # Add <%= base_name %>.rb to the search path
7
+ Merb::Plugins.config[:merb_slices][:auto_register] = true
8
+ Merb::Plugins.config[:merb_slices][:search_path] = File.join(File.dirname(__FILE__), '..', 'lib', '<%= base_name %>.rb')
9
+
10
+ # Using Merb.root below makes sure that the correct root is set for
11
+ # - testing standalone, without being installed as a gem and no host application
12
+ # - testing from within the host application; its root will be used
13
+ Merb.start_environment(
14
+ :testing => true,
15
+ :adapter => 'runner',
16
+ :environment => ENV['MERB_ENV'] || 'test',
17
+ :merb_root => Merb.root,
18
+ :session_store => 'memory'
19
+ )
20
+
21
+ module Merb
22
+ module Test
23
+ module SliceHelper
24
+
25
+ # The absolute path to the current slice
26
+ def current_slice_root
27
+ @current_slice_root ||= File.expand_path(File.join(File.dirname(__FILE__), '..'))
28
+ end
29
+
30
+ # Whether the specs are being run from a host application or standalone
31
+ def standalone?
32
+ Merb.root == ::<%= module_name %>.root
33
+ end
34
+
35
+ end
36
+ end
37
+ end
38
+
39
+ Spec::Runner.configure do |config|
40
+ config.include(Merb::Test::ViewHelper)
41
+ config.include(Merb::Test::RouteHelper)
42
+ config.include(Merb::Test::ControllerHelper)
43
+ config.include(Merb::Test::SliceHelper)
44
+ end
@@ -0,0 +1,2 @@
1
+ class <%= module_name %>::Application < Merb::Controller
2
+ end
@@ -0,0 +1,2 @@
1
+ class <%= module_name %>::Main < <%= module_name %>::Application
2
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) <%= Time.now.year %> YOUR NAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,130 @@
1
+ <%= module_name %>
2
+ <%= "=" * module_name.size %>
3
+
4
+ A thin slice for the Merb framework.
5
+
6
+ ------------------------------------------------------------------------------
7
+
8
+ <%= base_name %>
9
+ |-- LICENSE
10
+ |-- README
11
+ |-- Rakefile [1]
12
+ |-- TODO
13
+ |-- application.rb [2]
14
+ |-- lib
15
+ | |-- <%= base_name %>
16
+ | | |-- merbtasks.rb [3]
17
+ | | `-- slicetasks.rb [4]
18
+ | `-- <%= base_name %>.rb [5]
19
+ |-- public [6]
20
+ | |-- javascripts
21
+ | | `-- master.js
22
+ | `-- stylesheets
23
+ | `-- master.css
24
+ |-- stubs [7]
25
+ | `-- application.rb
26
+ `-- views [8]
27
+ |-- layout
28
+ | `-- <%= base_name %>.html.erb [9]
29
+ `-- main
30
+ `-- index.html.erb
31
+
32
+
33
+ 1. Rake tasks to package/install the gem - edit this to modify the manifest.
34
+ 2. The slice application in a single file: controllers, models, helper methods.
35
+ 3. Standard rake tasks available to your application.
36
+ 4. Your custom application rake tasks.
37
+ 5. The main slice file - contains all slice setup logic/config.
38
+ 6. Public assets you (optionally) install using rake slices:my_slice:install
39
+ 7. Stubs of classes/views/files for the end-user to override - usually these
40
+ mimic the files in app/ and/or public/; use rake slices:<%= base_name %>:stubs to
41
+ get started with the override stubs. Also, slices:<%= base_name %>:patch will
42
+ copy over views to override in addition to the files found in /stubs.
43
+ 8. A thin slice stores its views seperately.
44
+ 9. The default layout, as specified in Merb::Slices::config[:my_slice][:layout]
45
+ change this to :application to use the app's layout.
46
+
47
+
48
+ To see all available tasks for <%= module_name %> run:
49
+
50
+ rake -T slices:<%= base_name %>
51
+
52
+ ------------------------------------------------------------------------------
53
+
54
+ Instructions for installation:
55
+
56
+ file: config/init.rb
57
+
58
+ # add the slice as a regular dependency
59
+
60
+ dependency '<%= base_name %>'
61
+
62
+ # if needed, configure which slices to load and in which order
63
+
64
+ Merb::Plugins.config[:merb_slices] = { :queue => ["<%= module_name %>", ...] }
65
+
66
+ # optionally configure the plugins in a before_app_loads callback
67
+
68
+ Merb::BootLoader.before_app_loads do
69
+
70
+ Merb::Slices::config[:<%= symbol_name %>][:option] = value
71
+
72
+ end
73
+
74
+ file: config/router.rb
75
+
76
+ # example: /<%= base_name %>/:controller/:action/:id
77
+
78
+ r.add_slice(:<%= module_name %>)
79
+
80
+ # example: /foo/:controller/:action/:id
81
+
82
+ r.add_slice(:<%= module_name %>, 'foo') # same as :path => 'foo'
83
+
84
+ # example: /:lang/:controller/:action/:id (with :a param set)
85
+
86
+ r.add_slice(:<%= module_name %>, :path => ':lang', :params => { :a => 'b' })
87
+
88
+ # example: /:controller/:action/:id
89
+
90
+ r.slice(:<%= module_name %>)
91
+
92
+ Normally you should also run the following rake task:
93
+
94
+ rake slices:<%= symbol_name %>:install
95
+
96
+ ------------------------------------------------------------------------------
97
+
98
+ About Slices
99
+ ============
100
+
101
+ Merb-Slices is a Merb plugin for using and creating application 'slices' which
102
+ help you modularize your application. Usually these are reuseable extractions
103
+ from your main app. In effect, a Slice is just like a regular Merb MVC
104
+ application, both in functionality as well as in structure.
105
+
106
+ When you generate a Slice stub structure, a module is setup to serve as a
107
+ namespace for your controller, models, helpers etc. This ensures maximum
108
+ encapsulation. You could say a Slice is a mixture between a Merb plugin (a
109
+ Gem) and a Merb application, reaping the benefits of both.
110
+
111
+ A host application can 'mount' a Slice inside the router, which means you have
112
+ full over control how it integrates. By default a Slice's routes are prefixed
113
+ by its name (a router :namespace), but you can easily provide your own prefix
114
+ or leave it out, mounting it at the root of your url-schema. You can even
115
+ mount a Slice multiple times and give extra parameters to customize an
116
+ instance's behaviour.
117
+
118
+ A Slice's Application controller uses controller_for_slice to setup slice
119
+ specific behaviour, which mainly affects cascaded view handling. Additionaly,
120
+ this method is available to any kind of controller, so it can be used for
121
+ Merb Mailer too for example.
122
+
123
+ There are many ways which let you customize a Slice's functionality and
124
+ appearance without ever touching the Gem-level code itself. It's not only easy
125
+ to add template/layout overrides, you can also add/modify controllers, models
126
+ and other runtime code from within the host application.
127
+
128
+ To create your own Slice run this (somewhere outside of your merb app):
129
+
130
+ $ merb-gen slice <your-lowercase-slice-name>
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'merb-core/version'
4
+ require 'merb-core/tasks/merb_rake_helper'
5
+
6
+ NAME = "<%= base_name %>"
7
+ AUTHOR = "Your Name"
8
+ EMAIL = "Your Email"
9
+ HOMEPAGE = "http://merbivore.com/"
10
+ SUMMARY = "Merb Slice that provides ..."
11
+ GEM_VERSION = "<%= Merb::VERSION %>"
12
+
13
+ spec = Gem::Specification.new do |s|
14
+ s.rubyforge_project = 'merb'
15
+ s.name = NAME
16
+ s.version = GEM_VERSION
17
+ s.platform = Gem::Platform::RUBY
18
+ s.has_rdoc = true
19
+ s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
20
+ s.summary = SUMMARY
21
+ s.description = s.summary
22
+ s.author = AUTHOR
23
+ s.email = EMAIL
24
+ s.homepage = HOMEPAGE
25
+ s.add_dependency('merb-slices', '>= <%= Merb::VERSION %>')
26
+ s.require_path = 'lib'
27
+ s.files = %w(LICENSE README Rakefile TODO application.rb) + Dir.glob("{lib,public,views,stubs}/**/*")
28
+ end
29
+
30
+ Rake::GemPackageTask.new(spec) do |pkg|
31
+ pkg.gem_spec = spec
32
+ end
33
+
34
+ desc "Install <%= module_name %> as a gem"
35
+ task :install => [:package] do
36
+ sh %{#{sudo} gem install pkg/#{NAME}-#{GEM_VERSION} --no-update-sources}
37
+ end
38
+
39
+ namespace :jruby do
40
+
41
+ desc "Run :package and install the resulting .gem with jruby"
42
+ task :install => :package do
43
+ sh %{#{sudo} jruby -S gem install #{install_home} pkg/#{NAME}-#{GEM_VERSION}.gem --no-rdoc --no-ri}
44
+ end
45
+
46
+ end
@@ -0,0 +1,7 @@
1
+ TODO:
2
+
3
+ - Fix <%= module_name %>.description and <%= module_name %>.version
4
+ - Fix LICENSE with your name
5
+ - Fix Rakefile with your name and contact info
6
+ - Add your code to lib/<%= base_name %>.rb
7
+ - Add your Merb rake tasks to lib/<%= base_name %>/merbtasks.rb
@@ -0,0 +1,36 @@
1
+ module <%= module_name %>
2
+
3
+ # All Slice code is expected to be namespaced inside this module.
4
+
5
+ class Application < Merb::Controller
6
+
7
+ controller_for_slice
8
+
9
+ private
10
+
11
+ # Construct a path relative to the public directory
12
+ def public_path_for(type, *segments)
13
+ ::<%= module_name %>.public_path_for(type, *segments)
14
+ end
15
+
16
+ # Construct an app-level path.
17
+ def app_path_for(type, *segments)
18
+ ::<%= module_name %>.app_path_for(type, *segments)
19
+ end
20
+
21
+ # Construct a slice-level path
22
+ def slice_path_for(type, *segments)
23
+ ::<%= module_name %>.slice_path_for(type, *segments)
24
+ end
25
+
26
+ end
27
+
28
+ class Main < Application
29
+
30
+ def index
31
+ render
32
+ end
33
+
34
+ end
35
+
36
+ end
@@ -0,0 +1,93 @@
1
+ if defined?(Merb::Plugins)
2
+
3
+ $:.unshift File.dirname(__FILE__)
4
+
5
+ load_dependency 'merb-slices'
6
+ Merb::Plugins.add_rakefiles "<%= base_name %>/merbtasks", "<%= base_name %>/slicetasks"
7
+
8
+ # Register the Slice for the current host application
9
+ Merb::Slices::register(__FILE__)
10
+
11
+ # Slice configuration - set this in a before_app_loads callback.
12
+ # By default a Slice uses its own layout.
13
+ Merb::Slices::config[:<%= symbol_name %>][:layout] ||= :<%= symbol_name %>
14
+
15
+ # All Slice code is expected to be namespaced inside a module
16
+ module <%= module_name %>
17
+
18
+ # Slice metadata
19
+ self.description = "<%= module_name %> is a thin Merb slice!"
20
+ self.version = "0.0.1"
21
+ self.author = "YOUR NAME"
22
+
23
+ # Stub classes loaded hook - runs before LoadClasses BootLoader
24
+ # right after a slice's classes have been loaded internally.
25
+ def self.loaded
26
+ end
27
+
28
+ # Initialization hook - runs before AfterAppLoads BootLoader
29
+ def self.init
30
+ end
31
+
32
+ # Activation hook - runs after AfterAppLoads BootLoader
33
+ def self.activate
34
+ end
35
+
36
+ # Deactivation hook - triggered by Merb::Slices.deactivate(<%= module_name %>)
37
+ def self.deactivate
38
+ end
39
+
40
+ # Setup routes inside the host application
41
+ #
42
+ # @param scope<Merb::Router::Behaviour>
43
+ # Routes will be added within this scope (namespace). In fact, any
44
+ # router behaviour is a valid namespace, so you can attach
45
+ # routes at any level of your router setup.
46
+ #
47
+ # @note prefix your named routes with :<%= symbol_name %>_
48
+ # to avoid potential conflicts with global named routes.
49
+ def self.setup_router(scope)
50
+ end
51
+
52
+ # This sets up a thin slice's structure.
53
+ def self.setup_default_structure!
54
+ self.push_app_path(:root, Merb.root / 'slices' / self.identifier, nil)
55
+
56
+ self.push_path(:stub, root_path('stubs'), nil)
57
+ self.push_app_path(:stub, app_dir_for(:root), nil)
58
+
59
+ self.push_path(:application, root, 'application.rb')
60
+ self.push_app_path(:application, app_dir_for(:root), 'application.rb')
61
+
62
+ self.push_path(:view, dir_for(:application) / "views")
63
+ self.push_app_path(:view, app_dir_for(:application) / "views")
64
+
65
+ self.push_path(:public, root_path('public'), nil)
66
+ self.push_app_path(:public, Merb.root / 'public' / 'slices' / self.identifier, nil)
67
+
68
+ public_components.each do |component|
69
+ self.push_path(component, dir_for(:public) / "#{component}s", nil)
70
+ self.push_app_path(component, app_dir_for(:public) / "#{component}s", nil)
71
+ end
72
+ end
73
+
74
+ end
75
+
76
+ # Setup the slice layout for <%= module_name %>
77
+ #
78
+ # Use <%= module_name %>.push_path and <%= module_name %>.push_app_path
79
+ # to set paths to <%= base_name %>-level and app-level paths. Example:
80
+ #
81
+ # <%= module_name %>.push_path(:application, <%= module_name %>.root)
82
+ # <%= module_name %>.push_app_path(:application, Merb.root / 'slices' / '<%= base_name %>')
83
+ # ...
84
+ #
85
+ # Any component path that hasn't been set will default to <%= module_name %>.root
86
+ #
87
+ # For a thin slice we just add application.rb, :view and :public locations.
88
+ <%= module_name %>.setup_default_structure!
89
+
90
+ # Add dependencies for other <%= module_name %> classes below. Example:
91
+ # dependency "<%= base_name %>/other"
92
+
93
+ end