Class: Saruman::ModelBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/saruman.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ ModelBuilder

Returns a new instance of ModelBuilder.



392
393
394
395
# File 'lib/saruman.rb', line 392

def initialize(options)
  @options = options
  ask_questions
end

Instance Method Details

#ask_questionsObject



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'lib/saruman.rb', line 397

def ask_questions
  model = Saruman::Model.new(@options)
  
  model.name = ask("Enter Name of model") { |q| q.default = "Post" }
  model.table_name = ask("Enter Table Name of model") { |q| q.default = "blog_post" }
  model_fields_input = ask("Enter Model Fields") { |q| q.default = "title:string active:boolean blog_id:integer:index" }

  model.sql = parse_model_fields(model_fields_input, model.table_name)
  # @question_answer = {:model_name => model_name, :model_name_lower => model_name.downcase, :model_table_name => model_table_name, :sql => model_sql}
  
  say("Would you like to create a collection model?")
  choose do |menu|
    menu.choice(:yes) { model.collection = true }
    menu.choice(:no) { model.collection  = false }
  end
  
  @question_answer = model
end

#outputObject



416
417
418
# File 'lib/saruman.rb', line 416

def output
  return @question_answer
end

#parse_model_fields(model_fields, model_table_name) ⇒ Object



420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/saruman.rb', line 420

def parse_model_fields(model_fields, model_table_name)
  sql = ""
  index_sql = ""
  model_fields.split(" ").each do |field|
    field = Saruman::ModelBuilderField.new(field, model_table_name)
    sql << field.sql + ",\n"
    index_sql << field.index_sql if field.index?
  end
  sql << index_sql 
  sql << "  PRIMARY KEY (`id`) \n"
  sql
end