Class: RailsZen::GivenModelGen
- Inherits:
-
Object
- Object
- RailsZen::GivenModelGen
- Defined in:
- lib/rails_zen/given_model_gen.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#raw_attributes ⇒ Object
Returns the value of attribute raw_attributes.
-
#simple_attributes ⇒ Object
Returns the value of attribute simple_attributes.
Instance Method Summary collapse
- #ask_for_has_many_relations ⇒ Object
- #attrs ⇒ Object
- #chosen_attrs ⇒ Object
-
#initialize(name, raw_attrs) ⇒ GivenModelGen
constructor
A new instance of GivenModelGen.
- #step_by_step ⇒ Object
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
#name ⇒ Object
Returns the value of attribute name.
7 8 9 |
# File 'lib/rails_zen/given_model_gen.rb', line 7 def name @name end |
#raw_attributes ⇒ Object
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_attributes ⇒ Object
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_relations ⇒ Object
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 |
#attrs ⇒ Object
66 67 68 |
# File 'lib/rails_zen/given_model_gen.rb', line 66 def attrs raw_attributes.scan(/\w+(?=:)/) end |
#chosen_attrs ⇒ Object
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_step ⇒ Object
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 |