Permalink
Browse files

Add generators for contexts.

  • Loading branch information...
1 parent 853fb81 commit 3dbfb6437ab44310960dc9199dcfc036a272e5a6 @midas committed Sep 19, 2010
@@ -0,0 +1,13 @@
+require 'rails/generators'
+
+class GenesisContextsGenerator < Rails::Generators::Base
+ argument :contexts, :type => :string, :default => []
+
+ def self.source_root
+ File.join( File.dirname(__FILE__), 'templates' )
+ end
+
+ def install_contexts
+ @contexts.each { |context| empty_directory "#{Genesis::SEEDS_ROOT}/contexts/#{context}" }
+ end
+end
@@ -0,0 +1,44 @@
+class GenesisContextsGenerator < Rails::Generator::Base
+ attr_accessor :opts
+ attr_accessor :environments
+
+ def initialize( runtime_args, runtime_options={} )
+ super
+ @opts = {}
+ @contexts = []
+ parse_args( args )
+ end
+
+ def manifest
+ record do |m|
+ m.directory Genesis::SEEDS_ROOT
+ @contexts.each { |context| m.directory "#{Genesis::SEEDS_ROOT}/contexts/#{context}" }
+ end
+ end
+
+ private
+
+ def parse_args( arguments )
+ arguments.each do |arg|
+ arg_parts = arg.split( ':' )
+ if arg_parts[0] == 'contexts'
+ handle_env_arg( arg_parts[1] )
+ else
+ opts[arg_parts[0].to_sym] = arg_parts[1]
+ end
+ end
+ end
+
+ def handle_contexts_arg( val )
+ if val.include?( '[' ) && val.include?( ']')
+ val.gsub!( /\[/, '' ).gsub!( /\]/, '' )
+ val.split( ',' ).each { |v| @contexts << v.trim.gsub( /,/, '' ) }
+ elsif val.include?( '[' ) || val.include?( ']' )
+ raise 'Error The contexts option must be formatted without any spaces in the array. ie. contexts:[accounts,users]'
+ elsif val.include?( ',' )
+ raise 'Error The contexts option must be formatted with braces at the beginning and end of the list. ie. contexts:[accounts,users]'
+ else
+ @contexts << val
+ end
+ end
+end
@@ -1,7 +1,7 @@
class PrepareSeedingGenerator < Rails::Generator::Base
attr_accessor :opts
attr_accessor :environments
-
+
def initialize( runtime_args, runtime_options = {} )
super
@opts = {}
@@ -17,9 +17,9 @@ def manifest
m.file 'genesis_callbacks.rb', 'db/seeds/genesis_callbacks.rb'
end
end
-
+
private
-
+
def parse_args( arguments )
arguments.each do |arg|
arg_parts = arg.split( ':' )
@@ -29,10 +29,10 @@ def parse_args( arguments )
opts[arg_parts[0].to_sym] = arg_parts[1]
end
end
-
+
validate_env_args
end
-
+
def handle_env_arg( val )
if val.include?( '[' ) && val.include?( ']')
val.gsub!( /\[/, '' ).gsub!( /\]/, '' )
@@ -45,7 +45,7 @@ def handle_env_arg( val )
@environments << val
end
end
-
+
def validate_env_args
@environments += %w(development production) if @environments.empty?
end

0 comments on commit 3dbfb64

Please sign in to comment.