Skip to content

Instantly share code, notes, and snippets.

@postmodern
Created July 7, 2011 05:23
Show Gist options
  • Save postmodern/1068927 to your computer and use it in GitHub Desktop.
Save postmodern/1068927 to your computer and use it in GitHub Desktop.
half-assed attempt at rewriting dm-gen to use Thor
require 'thor'
require 'thor/actions'
module DataMapper
module Gen
class CLI < Thor
include Thor::Actions
DM_VERSION = '1.0'
desc "one_file NAME [MODEL]", %{
Generates a 'one file test' for DataMapper, including debug logging and sqlite3 memory DB
Use like 'dm-gen one_file filename [model] [model,attributes,here]'
For example:
dm-gen one_file validation_test Post title:string,body:text
generates a one file test called 'validation_test.rb' which will have a class
called Post in it, with 3 properties: a Serial id (added for free), and a title
and a body.
}
method_option :attributes, :type => :hash, :default => {}
def one_file(name,model='test_model')
@model_name = camelcase(model)
@file_name = name.sub(/\.rb\z/,'')
@properties = {}
options.attributes.each do |key,value|
# skip if the property is named id
next if key == 'id'
# convert to snake/camel case
@properties[underscore(key)] = camelcase(value)
end
template 'one_file.rb.erb', "#{name}.rb", :dry_run => true
end
protected
def underscore(str)
Inflector.underscore(str)
end
def camelcase(str)
Inflector.camelize(str)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment