lib/pliny/commands/generator.rb in pliny-0.0.3 vs lib/pliny/commands/generator.rb in pliny-0.0.4

- old
+ new

@@ -4,18 +4,19 @@ require "active_support/inflector" require "prmd" module Pliny::Commands class Generator - attr_accessor :args, :stream + attr_accessor :args, :opts, :stream - def self.run(args, stream=$stdout) - new(args).run! + def self.run(args, opts={}, stream=$stdout) + new(args, opts).run! end - def initialize(args={}, stream=$stdout) + def initialize(args={}, opts={}, stream=$stdout) @args = args + @opts = opts @stream = stream end def run! unless type @@ -37,12 +38,10 @@ create_migration when "model" create_model create_model_migration create_model_test - create_serializer - create_serializer_test when "scaffold" create_endpoint(scaffold: true) create_endpoint_test create_endpoint_acceptance_test(scaffold: true) create_model @@ -69,10 +68,14 @@ def name args[1] end + def paranoid + opts[:paranoid] + end + def singular_class_name name.gsub(/-/, '_').singularize.camelize end def plural_class_name @@ -90,11 +93,11 @@ def display(msg) stream.puts msg end def create_endpoint(options = {}) - endpoint = "./lib/endpoints/#{name.pluralize}.rb" + endpoint = "./lib/endpoints/#{table_name}.rb" template = options[:scaffold] ? "endpoint_scaffold.erb" : "endpoint.erb" render_template(template, endpoint, { plural_class_name: plural_class_name, singular_class_name: singular_class_name, field_name: field_name, @@ -104,21 +107,21 @@ display "add the following to lib/routes.rb:" display " mount Endpoints::#{plural_class_name}" end def create_endpoint_test - test = "./spec/endpoints/#{name.pluralize}_spec.rb" + test = "./spec/endpoints/#{table_name}_spec.rb" render_template("endpoint_test.erb", test, { plural_class_name: plural_class_name, singular_class_name: singular_class_name, url_path: url_path, }) display "created test #{test}" end def create_endpoint_acceptance_test(options = {}) - test = "./spec/acceptance/#{name.pluralize}_spec.rb" + test = "./spec/acceptance/#{table_name}_spec.rb" template = options[:scaffold] ? "endpoint_scaffold_acceptance_test.erb" : "endpoint_acceptance_test.erb" render_template(template, test, { plural_class_name: plural_class_name, field_name: field_name, @@ -127,17 +130,17 @@ }) display "created test #{test}" end def create_mediator - mediator = "./lib/mediators/#{name}.rb" + mediator = "./lib/mediators/#{field_name}.rb" render_template("mediator.erb", mediator, plural_class_name: plural_class_name) display "created mediator file #{mediator}" end def create_mediator_test - test = "./spec/mediators/#{name}_spec.rb" + test = "./spec/mediators/#{field_name}_spec.rb" render_template("mediator_test.erb", test, plural_class_name: plural_class_name) display "created test #{test}" end def create_migration @@ -145,29 +148,32 @@ render_template("migration.erb", migration) display "created migration #{migration}" end def create_model - model = "./lib/models/#{name}.rb" - render_template("model.erb", model, singular_class_name: singular_class_name) + model = "./lib/models/#{field_name}.rb" + render_template("model.erb", model, + singular_class_name: singular_class_name, + paranoid: paranoid) display "created model file #{model}" end def create_model_migration migration = "./db/migrate/#{Time.now.to_i}_create_#{table_name}.rb" render_template("model_migration.erb", migration, - table_name: table_name) + table_name: table_name, + paranoid: paranoid) display "created migration #{migration}" end def create_model_test - test = "./spec/models/#{name}_spec.rb" + test = "./spec/models/#{field_name}_spec.rb" render_template("model_test.erb", test, singular_class_name: singular_class_name) display "created test #{test}" end def create_schema - schema = "./docs/schema/schemata/#{name.singularize}.yaml" + schema = "./docs/schema/schemata/#{field_name}.yaml" write_file(schema) do Prmd.init(name.singularize, yaml: true) end display "created schema file #{schema}" end