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,106 @@
1
+ namespace :slices do
2
+ namespace :<%= symbol_name %> do
3
+
4
+ desc "Install <%= module_name %>"
5
+ task :install => [:preflight, :setup_directories, :copy_assets, :migrate]
6
+
7
+ desc "Test for any dependencies"
8
+ task :preflight do # see slicetasks.rb
9
+ end
10
+
11
+ desc "Setup directories"
12
+ task :setup_directories do
13
+ puts "Creating directories for host application"
14
+ [:application, :view, :model, :controller, :helper, :mailer, :part, :public].each do |type|
15
+ if File.directory?(<%= module_name %>.dir_for(type))
16
+ if !File.directory?(dst_path = <%= module_name %>.app_dir_for(type))
17
+ relative_path = dst_path.relative_path_from(Merb.root)
18
+ puts "- creating directory :#{type} #{File.basename(Merb.root) / relative_path}"
19
+ mkdir_p(dst_path)
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ desc "Copy stub files to host application"
26
+ task :stubs do
27
+ puts "Copying stubs for <%= module_name %> - resolves any collisions"
28
+ copied, preserved = <%= module_name %>.mirror_stubs!
29
+ puts "- no files to copy" if copied.empty? && preserved.empty?
30
+ copied.each { |f| puts "- copied #{f}" }
31
+ preserved.each { |f| puts "! preserved override as #{f}" }
32
+ end
33
+
34
+ desc "Copy stub files and views to host application"
35
+ task :patch => [ "stubs", "freeze:views" ]
36
+
37
+ desc "Copy public assets to host application"
38
+ task :copy_assets do
39
+ puts "Copying assets for <%= module_name %> - resolves any collisions"
40
+ copied, preserved = <%= module_name %>.mirror_public!
41
+ puts "- no files to copy" if copied.empty? && preserved.empty?
42
+ copied.each { |f| puts "- copied #{f}" }
43
+ preserved.each { |f| puts "! preserved override as #{f}" }
44
+ end
45
+
46
+ desc "Migrate the database"
47
+ task :migrate do # see slicetasks.rb
48
+ end
49
+
50
+ desc "Freeze <%= module_name %> into your app (only <%= base_name %>/app)"
51
+ task :freeze => [ "freeze:app" ]
52
+
53
+ namespace :freeze do
54
+
55
+ desc "Freezes <%= module_name %> by installing the gem into application/gems using merb-freezer"
56
+ task :gem do
57
+ begin
58
+ Object.const_get(:Freezer).freeze(ENV["GEM"] || "<%= base_name %>", ENV["UPDATE"], ENV["MODE"] || 'rubygems')
59
+ rescue NameError
60
+ puts "! dependency 'merb-freezer' missing"
61
+ end
62
+ end
63
+
64
+ desc "Freezes <%= module_name %> by copying all files from <%= base_name %>/app to your application"
65
+ task :app do
66
+ puts "Copying all <%= base_name %>/app files to your application - resolves any collisions"
67
+ copied, preserved = <%= module_name %>.mirror_app!
68
+ puts "- no files to copy" if copied.empty? && preserved.empty?
69
+ copied.each { |f| puts "- copied #{f}" }
70
+ preserved.each { |f| puts "! preserved override as #{f}" }
71
+ end
72
+
73
+ desc "Freeze all views into your application for easy modification"
74
+ task :views do
75
+ puts "Copying all view templates to your application - resolves any collisions"
76
+ copied, preserved = <%= module_name %>.mirror_files_for :view
77
+ puts "- no files to copy" if copied.empty? && preserved.empty?
78
+ copied.each { |f| puts "- copied #{f}" }
79
+ preserved.each { |f| puts "! preserved override as #{f}" }
80
+ end
81
+
82
+ desc "Freeze all models into your application for easy modification"
83
+ task :models do
84
+ puts "Copying all models to your application - resolves any collisions"
85
+ copied, preserved = <%= module_name %>.mirror_files_for :model
86
+ puts "- no files to copy" if copied.empty? && preserved.empty?
87
+ copied.each { |f| puts "- copied #{f}" }
88
+ preserved.each { |f| puts "! preserved override as #{f}" }
89
+ end
90
+
91
+ desc "Freezes <%= module_name %> as a gem and copies over <%= base_name %>/app"
92
+ task :app_with_gem => [:gem, :app]
93
+
94
+ desc "Freezes <%= module_name %> by unpacking all files into your application"
95
+ task :unpack do
96
+ puts "Unpacking <%= module_name %> files to your application - resolves any collisions"
97
+ copied, preserved = <%= module_name %>.unpack_slice!
98
+ puts "- no files to copy" if copied.empty? && preserved.empty?
99
+ copied.each { |f| puts "- copied #{f}" }
100
+ preserved.each { |f| puts "! preserved override as #{f}" }
101
+ end
102
+
103
+ end
104
+
105
+ end
106
+ end
@@ -0,0 +1,18 @@
1
+ namespace :slices do
2
+ namespace :<%= symbol_name %> do
3
+
4
+ # add your own <%= base_name %> tasks here
5
+
6
+ # implement this to test for structural/code dependencies
7
+ # like certain directories or availability of other files
8
+ desc "Test for any dependencies"
9
+ task :preflight do
10
+ end
11
+
12
+ # implement this to perform any database related setup steps
13
+ desc "Migrate the database"
14
+ task :migrate do
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,2 @@
1
+ html, body { margin: 0; padding: 0; }
2
+ #container { width: 800px; margin: 4em auto; padding: 4em 4em 6em 4em; background: #DDDDDD; }
@@ -0,0 +1,9 @@
1
+ module <%= module_name %>
2
+
3
+ class Application < Merb::Controller
4
+ end
5
+
6
+ class Main < Application
7
+ end
8
+
9
+ end
@@ -0,0 +1,16 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
3
+ <head>
4
+ <meta http-equiv="content-type" content="text/html; charset=utf-8" />
5
+ <title>Fresh <%= module_name %> Thin Slice</title>
6
+ <link href="<%%= public_path_for :stylesheet, 'master.css' %>" type="text/css" charset="utf-8" rel="stylesheet" media="all" />
7
+ <script src="<%%= public_path_for :javascript, 'master.js' %>" type="text/javascript" charset="utf-8"></script>
8
+ </head>
9
+ <!-- you can override this layout at slices/<%= base_name %>/views/layout/<%= symbol_name %>.html.erb -->
10
+ <body class="<%= base_name %>-slice">
11
+ <div id="container">
12
+ <h1><%= module_name %> Thin Slice</h1>
13
+ <div id="main"><%%= catch_content :for_layout %></div>
14
+ </div>
15
+ </body>
16
+ </html>
@@ -0,0 +1 @@
1
+ <strong><%%= slice.description %></strong> (v. <%%= slice.version %>)
@@ -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,110 @@
1
+ <%= module_name %>
2
+ <%= "=" * module_name.size %>
3
+
4
+ A very 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
+
20
+
21
+ 1. Rake tasks to package/install the gem - edit this to modify the manifest.
22
+ 2. The slice application in a single file: controllers, models, helper methods.
23
+ 3. Standard rake tasks available to your application.
24
+ 4. Your custom application rake tasks.
25
+ 5. The main slice file - contains all slice setup logic/config.
26
+
27
+
28
+ To see all available tasks for <%= module_name %> run:
29
+
30
+ rake -T slices:<%= symbol_name %>
31
+
32
+ ------------------------------------------------------------------------------
33
+
34
+ Instructions for installation:
35
+
36
+ file: config/init.rb
37
+
38
+ # add the slice as a regular dependency
39
+
40
+ dependency '<%= base_name %>'
41
+
42
+ # if needed, configure which slices to load and in which order
43
+
44
+ Merb::Plugins.config[:merb_slices] = { :queue => ["<%= module_name %>", ...] }
45
+
46
+ # optionally configure the plugins in a before_app_loads callback
47
+
48
+ Merb::BootLoader.before_app_loads do
49
+
50
+ Merb::Slices::config[:<%= symbol_name %>][:option] = value
51
+
52
+ end
53
+
54
+ file: config/router.rb
55
+
56
+ # example: /<%= base_name %>/:controller/:action/:id
57
+
58
+ r.add_slice(:<%= module_name %>)
59
+
60
+ # example: /foo/:controller/:action/:id
61
+
62
+ r.add_slice(:<%= module_name %>, 'foo') # same as :path => 'foo'
63
+
64
+ # example: /:lang/:controller/:action/:id (with :a param set)
65
+
66
+ r.add_slice(:<%= module_name %>, :path => ':lang', :params => { :a => 'b' })
67
+
68
+ # example: /:controller/:action/:id
69
+
70
+ r.slice(:<%= module_name %>)
71
+
72
+ Normally you should also run the following rake task:
73
+
74
+ rake slices:<%= symbol_name %>:install
75
+
76
+ ------------------------------------------------------------------------------
77
+
78
+ About Slices
79
+ ============
80
+
81
+ Merb-Slices is a Merb plugin for using and creating application 'slices' which
82
+ help you modularize your application. Usually these are reuseable extractions
83
+ from your main app. In effect, a Slice is just like a regular Merb MVC
84
+ application, both in functionality as well as in structure.
85
+
86
+ When you generate a Slice stub structure, a module is setup to serve as a
87
+ namespace for your controller, models, helpers etc. This ensures maximum
88
+ encapsulation. You could say a Slice is a mixture between a Merb plugin (a
89
+ Gem) and a Merb application, reaping the benefits of both.
90
+
91
+ A host application can 'mount' a Slice inside the router, which means you have
92
+ full over control how it integrates. By default a Slice's routes are prefixed
93
+ by its name (a router :namespace), but you can easily provide your own prefix
94
+ or leave it out, mounting it at the root of your url-schema. You can even
95
+ mount a Slice multiple times and give extra parameters to customize an
96
+ instance's behaviour.
97
+
98
+ A Slice's Application controller uses controller_for_slice to setup slice
99
+ specific behaviour, which mainly affects cascaded view handling. Additionaly,
100
+ this method is available to any kind of controller, so it can be used for
101
+ Merb Mailer too for example.
102
+
103
+ There are many ways which let you customize a Slice's functionality and
104
+ appearance without ever touching the Gem-level code itself. It's not only easy
105
+ to add template/layout overrides, you can also add/modify controllers, models
106
+ and other runtime code from within the host application.
107
+
108
+ To create your own Slice run this (somewhere outside of your merb app):
109
+
110
+ $ 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,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 "#{slice.description} (v. #{slice.version})"
32
+ end
33
+
34
+ end
35
+
36
+ end
@@ -0,0 +1,89 @@
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
+ Merb::Slices::config[:<%= symbol_name %>][:foo] ||= :bar
13
+
14
+ # All Slice code is expected to be namespaced inside a module
15
+ module <%= module_name %>
16
+
17
+ # Slice metadata
18
+ self.description = "<%= module_name %> is a very thin Merb slice!"
19
+ self.version = "0.0.1"
20
+ self.author = "YOUR NAME"
21
+
22
+ # Stub classes loaded hook - runs before LoadClasses BootLoader
23
+ # right after a slice's classes have been loaded internally.
24
+ def self.loaded
25
+ end
26
+
27
+ # Initialization hook - runs before AfterAppLoads BootLoader
28
+ def self.init
29
+ end
30
+
31
+ # Activation hook - runs after AfterAppLoads BootLoader
32
+ def self.activate
33
+ end
34
+
35
+ # Deactivation hook - triggered by Merb::Slices.deactivate(<%= module_name %>)
36
+ def self.deactivate
37
+ end
38
+
39
+ # Setup routes inside the host application
40
+ #
41
+ # @param scope<Merb::Router::Behaviour>
42
+ # Routes will be added within this scope (namespace). In fact, any
43
+ # router behaviour is a valid namespace, so you can attach
44
+ # routes at any level of your router setup.
45
+ #
46
+ # @note prefix your named routes with :<%= symbol_name %>_
47
+ # to avoid potential conflicts with global named routes.
48
+ def self.setup_router(scope)
49
+ end
50
+
51
+ # This sets up a very thin slice's structure.
52
+ def self.setup_default_structure!
53
+ self.push_app_path(:root, Merb.root / 'slices' / self.identifier, nil)
54
+
55
+ self.push_path(:stub, root_path('stubs'), nil)
56
+ self.push_app_path(:stub, app_dir_for(:root), nil)
57
+
58
+ self.push_path(:application, root, 'application.rb')
59
+ self.push_app_path(:application, app_dir_for(:root), 'application.rb')
60
+
61
+ self.push_path(:public, root_path('public'), nil)
62
+ self.push_app_path(:public, Merb.root / 'public' / 'slices' / self.identifier, nil)
63
+
64
+ public_components.each do |component|
65
+ self.push_path(component, dir_for(:public) / "#{component}s", nil)
66
+ self.push_app_path(component, app_dir_for(:public) / "#{component}s", nil)
67
+ end
68
+ end
69
+
70
+ end
71
+
72
+ # Setup the slice layout for <%= module_name %>
73
+ #
74
+ # Use <%= module_name %>.push_path and <%= module_name %>.push_app_path
75
+ # to set paths to <%= base_name %>-level and app-level paths. Example:
76
+ #
77
+ # <%= module_name %>.push_path(:application, <%= module_name %>.root)
78
+ # <%= module_name %>.push_app_path(:application, Merb.root / 'slices' / '<%= base_name %>')
79
+ # ...
80
+ #
81
+ # Any component path that hasn't been set will default to <%= module_name %>.root
82
+ #
83
+ # For a very thin slice we just add application.rb and :public locations.
84
+ <%= module_name %>.setup_default_structure!
85
+
86
+ # Add dependencies for other <%= module_name %> classes below. Example:
87
+ # dependency "<%= base_name %>/other"
88
+
89
+ end