merb-slices 0.9.7 → 0.9.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. data/README +4 -4
  2. data/Rakefile +27 -13
  3. data/TODO +1 -2
  4. data/bin/slice +77 -0
  5. data/lib/generators/templates/common/Rakefile +16 -12
  6. data/lib/generators/templates/full/README +4 -4
  7. data/lib/generators/templates/full/Rakefile +32 -13
  8. data/lib/generators/templates/full/config/init.rb +43 -0
  9. data/lib/generators/templates/full/lib/%base_name%.rb +3 -1
  10. data/lib/generators/templates/full/spec/%base_name%_spec.rb +10 -121
  11. data/lib/generators/templates/full/spec/controllers/main_spec.rb +18 -8
  12. data/lib/generators/templates/thin/README +4 -4
  13. data/lib/generators/templates/very_thin/README +4 -4
  14. data/lib/merb-slices.rb +3 -3
  15. data/lib/merb-slices/controller_mixin.rb +34 -7
  16. data/lib/merb-slices/merbtasks.rb +5 -34
  17. data/lib/merb-slices/module.rb +9 -3
  18. data/lib/merb-slices/module_mixin.rb +19 -31
  19. data/lib/merb-slices/router_ext.rb +10 -7
  20. data/spec/full_slice_generator_spec.rb +31 -2
  21. data/spec/full_slice_spec.rb +162 -0
  22. data/spec/merb-slice_spec.rb +97 -3
  23. data/spec/slice_generator_spec.rb +15 -3
  24. data/spec/slices/full-test-slice/LICENSE +20 -0
  25. data/spec/slices/full-test-slice/README +170 -0
  26. data/spec/slices/full-test-slice/Rakefile +67 -0
  27. data/spec/slices/full-test-slice/TODO +15 -0
  28. data/spec/slices/full-test-slice/app/controllers/application.rb +5 -0
  29. data/spec/slices/full-test-slice/app/controllers/main.rb +7 -0
  30. data/spec/slices/full-test-slice/app/helpers/application_helper.rb +64 -0
  31. data/spec/slices/full-test-slice/app/views/layout/full_test_slice.html.erb +16 -0
  32. data/spec/slices/full-test-slice/app/views/main/index.html.erb +1 -0
  33. data/spec/slices/full-test-slice/config/init.rb +43 -0
  34. data/spec/slices/full-test-slice/lib/full-test-slice.rb +80 -0
  35. data/spec/slices/full-test-slice/lib/full-test-slice/merbtasks.rb +103 -0
  36. data/spec/slices/full-test-slice/lib/full-test-slice/slicetasks.rb +18 -0
  37. data/spec/slices/full-test-slice/lib/full-test-slice/spectasks.rb +65 -0
  38. data/spec/slices/full-test-slice/public/javascripts/master.js +0 -0
  39. data/spec/slices/full-test-slice/public/stylesheets/master.css +2 -0
  40. data/spec/slices/full-test-slice/stubs/app/controllers/application.rb +2 -0
  41. data/spec/slices/full-test-slice/stubs/app/controllers/main.rb +2 -0
  42. data/spec/slices/thin-test-slice/LICENSE +20 -0
  43. data/spec/slices/thin-test-slice/README +130 -0
  44. data/spec/slices/thin-test-slice/Rakefile +43 -0
  45. data/spec/slices/thin-test-slice/TODO +7 -0
  46. data/spec/slices/thin-test-slice/application.rb +36 -0
  47. data/spec/slices/thin-test-slice/lib/thin-test-slice.rb +93 -0
  48. data/spec/slices/thin-test-slice/lib/thin-test-slice/merbtasks.rb +103 -0
  49. data/spec/slices/thin-test-slice/lib/thin-test-slice/slicetasks.rb +18 -0
  50. data/spec/slices/thin-test-slice/public/javascripts/master.js +0 -0
  51. data/spec/slices/thin-test-slice/public/stylesheets/master.css +2 -0
  52. data/spec/slices/thin-test-slice/stubs/application.rb +9 -0
  53. data/spec/slices/thin-test-slice/views/layout/thin_test_slice.html.erb +16 -0
  54. data/spec/slices/thin-test-slice/views/main/index.html.erb +1 -0
  55. data/spec/slices/very-thin-test-slice/LICENSE +20 -0
  56. data/spec/slices/very-thin-test-slice/README +110 -0
  57. data/spec/slices/very-thin-test-slice/Rakefile +43 -0
  58. data/spec/slices/very-thin-test-slice/TODO +7 -0
  59. data/spec/slices/very-thin-test-slice/application.rb +36 -0
  60. data/spec/slices/very-thin-test-slice/lib/very-thin-test-slice.rb +89 -0
  61. data/spec/slices/very-thin-test-slice/lib/very-thin-test-slice/merbtasks.rb +103 -0
  62. data/spec/slices/very-thin-test-slice/lib/very-thin-test-slice/slicetasks.rb +18 -0
  63. data/spec/spec_helper.rb +27 -2
  64. data/spec/thin_slice_generator_spec.rb +24 -2
  65. data/spec/thin_slice_spec.rb +139 -0
  66. data/spec/very_thin_slice_generator_spec.rb +22 -2
  67. data/spec/very_thin_slice_spec.rb +119 -0
  68. metadata +79 -5
@@ -0,0 +1,43 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+
4
+ require 'merb-core'
5
+ require 'merb-core/tasks/merb'
6
+
7
+ GEM_NAME = "thin-test-slice"
8
+ AUTHOR = "Your Name"
9
+ EMAIL = "Your Email"
10
+ HOMEPAGE = "http://merbivore.com/"
11
+ SUMMARY = "Merb Slice that provides ..."
12
+ GEM_VERSION = "0.9.8"
13
+
14
+ spec = Gem::Specification.new do |s|
15
+ s.rubyforge_project = 'merb'
16
+ s.name = GEM_NAME
17
+ s.version = GEM_VERSION
18
+ s.platform = Gem::Platform::RUBY
19
+ s.has_rdoc = true
20
+ s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
21
+ s.summary = SUMMARY
22
+ s.description = s.summary
23
+ s.author = AUTHOR
24
+ s.email = EMAIL
25
+ s.homepage = HOMEPAGE
26
+ s.add_dependency('merb-slices', '>= 0.9.8')
27
+ s.require_path = 'lib'
28
+ s.files = %w(LICENSE README Rakefile TODO application.rb) + Dir.glob("{lib,spec,app,public,stubs,views}/**/*")
29
+ end
30
+
31
+ Rake::GemPackageTask.new(spec) do |pkg|
32
+ pkg.gem_spec = spec
33
+ end
34
+
35
+ desc "Install the gem"
36
+ task :install do
37
+ Merb::RakeHelper.install(GEM_NAME, :version => GEM_VERSION)
38
+ end
39
+
40
+ desc "Uninstall the gem"
41
+ task :uninstall do
42
+ Merb::RakeHelper.uninstall(GEM_NAME, :version => GEM_VERSION)
43
+ end
@@ -0,0 +1,7 @@
1
+ TODO:
2
+
3
+ - Fix ThinTestSlice.description and ThinTestSlice.version
4
+ - Fix LICENSE with your name
5
+ - Fix Rakefile with your name and contact info
6
+ - Add your code to lib/thin-test-slice.rb
7
+ - Add your Merb rake tasks to lib/thin-test-slice/merbtasks.rb
@@ -0,0 +1,36 @@
1
+ module ThinTestSlice
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
+ ::ThinTestSlice.public_path_for(type, *segments)
14
+ end
15
+
16
+ # Construct an app-level path.
17
+ def app_path_for(type, *segments)
18
+ ::ThinTestSlice.app_path_for(type, *segments)
19
+ end
20
+
21
+ # Construct a slice-level path
22
+ def slice_path_for(type, *segments)
23
+ ::ThinTestSlice.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,93 @@
1
+ if defined?(Merb::Plugins)
2
+
3
+ $:.unshift File.dirname(__FILE__)
4
+
5
+ load_dependency 'merb-slices'
6
+ Merb::Plugins.add_rakefiles "thin-test-slice/merbtasks", "thin-test-slice/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[:thin_test_slice][:layout] ||= :thin_test_slice
14
+
15
+ # All Slice code is expected to be namespaced inside a module
16
+ module ThinTestSlice
17
+
18
+ # Slice metadata
19
+ self.description = "ThinTestSlice 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(ThinTestSlice)
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 :thin_test_slice_
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 ThinTestSlice
77
+ #
78
+ # Use ThinTestSlice.push_path and ThinTestSlice.push_app_path
79
+ # to set paths to thin-test-slice-level and app-level paths. Example:
80
+ #
81
+ # ThinTestSlice.push_path(:application, ThinTestSlice.root)
82
+ # ThinTestSlice.push_app_path(:application, Merb.root / 'slices' / 'thin-test-slice')
83
+ # ...
84
+ #
85
+ # Any component path that hasn't been set will default to ThinTestSlice.root
86
+ #
87
+ # For a thin slice we just add application.rb, :view and :public locations.
88
+ ThinTestSlice.setup_default_structure!
89
+
90
+ # Add dependencies for other ThinTestSlice classes below. Example:
91
+ # dependency "thin-test-slice/other"
92
+
93
+ end
@@ -0,0 +1,103 @@
1
+ namespace :slices do
2
+ namespace :thin_test_slice do
3
+
4
+ desc "Install ThinTestSlice"
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
+ ThinTestSlice.mirrored_components.each do |type|
15
+ if File.directory?(ThinTestSlice.dir_for(type))
16
+ if !File.directory?(dst_path = ThinTestSlice.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 ThinTestSlice - resolves any collisions"
28
+ copied, preserved = ThinTestSlice.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 ThinTestSlice - resolves any collisions"
40
+ copied, preserved = ThinTestSlice.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 ThinTestSlice into your app (only thin-test-slice/app)"
51
+ task :freeze => [ "freeze:app" ]
52
+
53
+ namespace :freeze do
54
+
55
+ desc "Freezes ThinTestSlice by installing the gem into application/gems"
56
+ task :gem do
57
+ ENV["GEM"] ||= "thin-test-slice"
58
+ Rake::Task['slices:install_as_gem'].invoke
59
+ end
60
+
61
+ desc "Freezes ThinTestSlice by copying all files from thin-test-slice/app to your application"
62
+ task :app do
63
+ puts "Copying all thin-test-slice/app files to your application - resolves any collisions"
64
+ copied, preserved = ThinTestSlice.mirror_app!
65
+ puts "- no files to copy" if copied.empty? && preserved.empty?
66
+ copied.each { |f| puts "- copied #{f}" }
67
+ preserved.each { |f| puts "! preserved override as #{f}" }
68
+ end
69
+
70
+ desc "Freeze all views into your application for easy modification"
71
+ task :views do
72
+ puts "Copying all view templates to your application - resolves any collisions"
73
+ copied, preserved = ThinTestSlice.mirror_files_for :view
74
+ puts "- no files to copy" if copied.empty? && preserved.empty?
75
+ copied.each { |f| puts "- copied #{f}" }
76
+ preserved.each { |f| puts "! preserved override as #{f}" }
77
+ end
78
+
79
+ desc "Freeze all models into your application for easy modification"
80
+ task :models do
81
+ puts "Copying all models to your application - resolves any collisions"
82
+ copied, preserved = ThinTestSlice.mirror_files_for :model
83
+ puts "- no files to copy" if copied.empty? && preserved.empty?
84
+ copied.each { |f| puts "- copied #{f}" }
85
+ preserved.each { |f| puts "! preserved override as #{f}" }
86
+ end
87
+
88
+ desc "Freezes ThinTestSlice as a gem and copies over thin-test-slice/app"
89
+ task :app_with_gem => [:gem, :app]
90
+
91
+ desc "Freezes ThinTestSlice by unpacking all files into your application"
92
+ task :unpack do
93
+ puts "Unpacking ThinTestSlice files to your application - resolves any collisions"
94
+ copied, preserved = ThinTestSlice.unpack_slice!
95
+ puts "- no files to copy" if copied.empty? && preserved.empty?
96
+ copied.each { |f| puts "- copied #{f}" }
97
+ preserved.each { |f| puts "! preserved override as #{f}" }
98
+ end
99
+
100
+ end
101
+
102
+ end
103
+ end
@@ -0,0 +1,18 @@
1
+ namespace :slices do
2
+ namespace :thin_test_slice do
3
+
4
+ # add your own thin-test-slice 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 ThinTestSlice
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 ThinTestSlice 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/thin-test-slice/views/layout/thin_test_slice.html.erb -->
10
+ <body class="thin-test-slice-slice">
11
+ <div id="container">
12
+ <h1>ThinTestSlice 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) 2008 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
+ VeryThinTestSlice
2
+ =================
3
+
4
+ A very thin slice for the Merb framework.
5
+
6
+ ------------------------------------------------------------------------------
7
+
8
+ very-thin-test-slice
9
+ |-- LICENSE
10
+ |-- README
11
+ |-- Rakefile [1]
12
+ |-- TODO
13
+ |-- application.rb [2]
14
+ `-- lib
15
+ |-- very-thin-test-slice
16
+ | |-- merbtasks.rb [3]
17
+ | `-- slicetasks.rb [4]
18
+ `-- very-thin-test-slice.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 VeryThinTestSlice run:
29
+
30
+ rake -T slices:very_thin_test_slice
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 'very-thin-test-slice'
41
+
42
+ # if needed, configure which slices to load and in which order
43
+
44
+ Merb::Plugins.config[:merb_slices] = { :queue => ["VeryThinTestSlice", ...] }
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[:very_thin_test_slice][:option] = value
51
+
52
+ end
53
+
54
+ file: config/router.rb
55
+
56
+ # example: /very-thin-test-slice/:controller/:action/:id
57
+
58
+ add_slice(:VeryThinTestSlice)
59
+
60
+ # example: /foo/:controller/:action/:id
61
+
62
+ add_slice(:VeryThinTestSlice, 'foo') # same as :path => 'foo'
63
+
64
+ # example: /:lang/:controller/:action/:id (with :a param set)
65
+
66
+ add_slice(:VeryThinTestSlice, :path => ':lang', :params => { :a => 'b' })
67
+
68
+ # example: /:controller/:action/:id
69
+
70
+ slice(:VeryThinTestSlice)
71
+
72
+ Normally you should also run the following rake task:
73
+
74
+ rake slices:very_thin_test_slice: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>