lib/generators/components/orms/mongomapper_gen.rb in padrino-gen-0.1.2 vs lib/generators/components/orms/mongomapper_gen.rb in padrino-gen-0.1.3

- old
+ new

@@ -1,10 +1,10 @@ module Padrino module Generators module Components module Orms - + module MongomapperGen MONGO = (<<-MONGO).gsub(/^ {10}/, '') class MongoDBConnectionFailure < RuntimeError; end @@ -44,11 +44,34 @@ def setup_orm require_dependencies 'mongo_mapper' create_file("config/database.rb", MONGO) create_file("lib/ext/mongo_mapper.rb", CONCERNED) end + + MM_MODEL = (<<-MODEL).gsub(/^ {10}/, '') + class !NAME! + include MongoMapper::Document + + # key <name>, <type> + !FIELDS! + end + MODEL + + def create_model_file(name, fields) + model_path = app_root_path('app/models/', "#{name.to_s.underscore}.rb") + return false if File.exist?(model_path) + field_tuples = fields.collect { |value| value.split(":") } + column_declarations = field_tuples.collect { |field, kind| "key :#{field}, #{kind.camelize}" }.join("\n ") + model_contents = MM_MODEL.gsub(/!NAME!/, name.to_s.camelize) + model_contents.gsub!(/!FIELDS!/, column_declarations) + create_file(model_path, model_contents) + end + + def create_migration_file(filename, name, fields) + # NO MIGRATION NEEDED + end end - + end end end -end \ No newline at end of file +end