class RailsZen::GivenModelGen

Attributes

name[RW]
raw_attributes[RW]
simple_attributes[RW]

Public Class Methods

new(name, raw_attrs) click to toggle source
# File lib/rails_zen/given_model_gen.rb, line 9
def initialize(name, raw_attrs)
  @name = name
  @raw_attributes = raw_attrs
  @simple_attributes = []
end

Public Instance Methods

ask_for_has_many_relations() click to toggle source
# File lib/rails_zen/given_model_gen.rb, line 49
def ask_for_has_many_relations
  say "\nDo you have any has_many relations? Enter the name if so, otherwise leave it. eg: posts,comments"

  @has_many_relations =  ask("Enter (comma sep list)  ", lambda { |str| str.split(/,\s*/) })

  @has_many_relations.each do |rel|

    m = RailsZen::WriteToModel.new
    m.model_name = @name
    m.adding_to_file!("  has_many :#{rel}\n")

    s = RailsZen::WriteToSpec.new
    s.model_name = @name
    s.adding_to_file!("it { is_expected.to have_many(:#{rel}) }")
  end

end
attrs() click to toggle source
# File lib/rails_zen/given_model_gen.rb, line 66
def attrs
  raw_attributes.scan(/\w+(?=:)/)
end
chosen_attrs() click to toggle source
# File lib/rails_zen/given_model_gen.rb, line 34
def chosen_attrs

  attr_hash = attrs_with_types
  final_attr_objs = []
  i = -1
  attr_hash.each do |attr, type|
    i += 1
    unless simple_attributes.include? "#{i}"
      final_attr_objs << RailsZen::ChosenAttr.new(attr, type)
    end
  end
  final_attr_objs
end
step_by_step() click to toggle source
# File lib/rails_zen/given_model_gen.rb, line 16
def step_by_step
  say "\nThese are your attributes"
  say "---------------------------\n"

  attrs.each_with_index { |attr, i| puts "#{i} #{attr}" }
  say "\n\nChoose the one that don't require 'presence true' or 'validations' or uniqueness.\n Enter like this eg: 0,1. or just enter "
  say "\n----------------------------------\n\n$.> "

  @simple_attributes =  ask("Enter (comma sep list)  ", lambda { |str| str.split(/,\s*/) })

  chosen_attrs.each do |attr_obj|
    attr_obj.get_user_inputs
    RailsZen::WriteToFiles.new(attr_obj, name).write
  end

  ask_for_has_many_relations
end

Private Instance Methods

attrs_with_types() click to toggle source
# File lib/rails_zen/given_model_gen.rb, line 70
def attrs_with_types
  raw_attributes.scan(/(\w+):(\w+)/).to_h
end