Class: RailsZen::GivenModelGen

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, raw_attrs) ⇒ GivenModelGen

Returns a new instance of GivenModelGen.



9
10
11
12
13
# File 'lib/rails_zen/given_model_gen.rb', line 9

def initialize(name, raw_attrs)
  @name = name
  @raw_attributes = raw_attrs
  @simple_attributes = []
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/rails_zen/given_model_gen.rb', line 7

def name
  @name
end

#raw_attributesObject

Returns the value of attribute raw_attributes.



7
8
9
# File 'lib/rails_zen/given_model_gen.rb', line 7

def raw_attributes
  @raw_attributes
end

#simple_attributesObject

Returns the value of attribute simple_attributes.



7
8
9
# File 'lib/rails_zen/given_model_gen.rb', line 7

def simple_attributes
  @simple_attributes
end

Instance Method Details

#ask_for_has_many_relationsObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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

#attrsObject



66
67
68
# File 'lib/rails_zen/given_model_gen.rb', line 66

def attrs
  raw_attributes.scan(/\w+(?=:)/)
end

#chosen_attrsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
# 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_stepObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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