Skip to content

Commit

Permalink
Add generators for contexts.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Harrelson committed Sep 19, 2010
1 parent 853fb81 commit 3dbfb64
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 6 deletions.
13 changes: 13 additions & 0 deletions lib/generators/genesis_contexts/genesis_contexts_generator.rb
@@ -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
Empty file.
44 changes: 44 additions & 0 deletions rails_generators/genesis_contexts/genesis_contexts_generator.rb
@@ -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
12 changes: 6 additions & 6 deletions rails_generators/prepare_seeding/prepare_seeding_generator.rb
@@ -1,7 +1,7 @@
class PrepareSeedingGenerator < Rails::Generator::Base
attr_accessor :opts
attr_accessor :environments

def initialize( runtime_args, runtime_options = {} )
super
@opts = {}
Expand All @@ -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( ':' )
Expand All @@ -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!( /\]/, '' )
Expand All @@ -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
Expand Down

0 comments on commit 3dbfb64

Please sign in to comment.