Permalink
Browse files

Hacking out basic generator structure

  • Loading branch information...
1 parent 3674868 commit fe05138f02c23fef2190219b978755e22b21c352 @jcasimir jcasimir committed May 29, 2011
View
@@ -1,16 +0,0 @@
-ActionController::Dispatcher.to_prepare do
- models = Dir.glob(File.join(Rails.root, '/app/models/*.rb')).map do |file|
- file.match(/(\w*).rb/)[1].split('_').map do |class_part|
- class_part.capitalize
- end.join('')
- end
-
- models.each do |model|
- begin
- decorator = (model + "Decorator").constantize
- model.constantize.send :include, decorator
- rescue NameError
- # No Decorator Exists
- end
- end
-end
@@ -0,0 +1,15 @@
+require 'rails/generators/base'
+
+module Decorator
+ module Generators
+ class Base < Rails::Generators::Base #:nodoc:
+ def self.source_root
+ @_decorator_source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'decorator', generator_name, 'templates'))
+ end
+
+ def self.banner
+ "rails generate decorator:#{generator_name} #{self.arguments.map{ |a| a.usage }.join(' ')} [options]"
+ end
+ end
+ end
+end
@@ -0,0 +1,12 @@
+Description:
+ The decorators_setup generator creates an /app/decorators folder and
+ a /config/initializers/load_decorators.rb initializer.
+
+ Any decorator module in /app/decorators that matches a model name
+ will be included into that model.
+
+Examples:
+ rails generate decorators:setup
+
+ Folder: app/decorators
+ Initializer: config/initializers/load_decorators.rb
@@ -0,0 +1,12 @@
+require 'generators/decorator'
+
+module Decorator
+ module Generators
+ class SetupGenerator < Base
+ def create_setup
+ directory "app/decorators"
+ copy_file "load_decorators.rb", "config/initializers/load_decorators.rb"
+ end
+ end
+ end
+end
@@ -0,0 +1,16 @@
+ActionController::Dispatcher.to_prepare do
+ models = Dir.glob(File.join(Rails.root, '/app/models/*.rb')).map do |file|
+ file.match(/(\w*).rb/)[1].split('_').map do |class_part|
+ class_part.capitalize
+ end.join('')
+ end
+
+ models.each do |model|
+ begin
+ decorator = (model + "Decorator").constantize
+ model.constantize.send :include, decorator
+ rescue NameError
+ # No Decorator Exists
+ end
+ end
+end
View
@@ -0,0 +1,16 @@
+Gem::Specification.new do |s|
+ s.name = "rails_decorators"
+ s.version = "0.0.1"
+ s.author = "Jeff Casimir"
+ s.email = "jeff@jumpstartlab.com"
+ s.homepage = "http://github.com/jcasimir/rails_decorators"
+ s.summary = "Decorator pattern implmentation for Rails."
+ s.description = "Rails Decorators reimagines the role of helpers in the view layer of a Rails application, allowing an object-oriented approach rather than procedural."
+
+ s.files = Dir["{lib,rails_generators}/**/*", "[A-Z]*"]
+ s.require_path = "lib"
+
+ s.add_development_dependency 'rspec-rails', '~> 2.0.1'
+ s.rubyforge_project = s.name
+ s.required_rubygems_version = ">= 1.3.4"
+end

0 comments on commit fe05138

Please sign in to comment.