- Model =
Postmod::Action.new(:model_path, :options) do
def call
create_ruby_file
create_migration
end
private
def create_ruby_file
File.open(model_filename, 'w') do |file|
file.puts model_content
end
end
def create_migration
Rails::Generators.invoke(
"active_record:migration",
[migration_name] + migration_options,
destination_root: Pathname.new(".")
)
end
def migration_options
[options, "created_at:timestamps", "updated_at:timestamps"].flatten
end
def model_content
"require 'active_record'\n\nclass \#{model_name} < ActiveRecord::Base\n\nend\n"
end
def migration_name
"create_#{model_path.split('/').last}"
end
def model_name
model_path.gsub(/^.*lib\//, '').camelize
end
def model_filename
model_path + '.rb'
end
end
- Action =
Postmod::Action.new(:action_path) do
def call
File.open(action_filename, 'w') do |file|
file.puts action_content
end
end
private
def action_content
"class \#{action_name}\n\ndef self.call\nend\n\nend\n"
end
def action_name
action_path.gsub(/^.*lib\//, '').camelize
end
def action_filename
action_path + '.rb'
end
end
- Module =
Postmod::Action.new(:module_path) do
def call
FileUtils.mkdir_p(module_path)
File.open(module_filename, 'w') do |file|
file.puts module_content
end
end
private
def module_content
"module \#{module_name}\nDir[\"\\\#{__FILE__.gsub(/\\.rb$/, '')}/*.rb\"].each {|file| require file }\nend\n"
end
def module_name
module_path.split('/').last.camelize
end
def module_filename
module_path + '.rb'
end
end