Module: Tennpipes::Generators::Components::Actions
- Included in:
- Tennpipes::Generators::Component, Tennpipes::Generators::Controller, Helper, Mailer, Migration, Model, Project, Task
- Defined in:
- lib/tennpipes-init/generators/components/actions.rb
Instance Method Summary collapse
-
#controller_actions(fields) ⇒ Object
Takes in fields for routes in the form of get:index post:test delete:yada.
- #create_helper_files(app, name) ⇒ Object
-
#current_migration_number ⇒ Object
Returns timestamp instead if :migration_format: in .components is “timestamp”.
-
#indent_spaces(count) ⇒ Object
Returns space characters of given count.
-
#insert_mocking_include(library_name, options = {}) ⇒ Object
Injects the mock library include into the test class in test_config for setting up mock gen.
-
#insert_test_suite_setup(suite_text, options = {}) ⇒ Object
Injects the test class text into the test_config file for setting up the test gen.
-
#migration_exist?(filename) ⇒ Boolean
Return true if the migration already exist.
-
#output_migration_file(filename, name, columns, options = {}) ⇒ Object
Generates a standalone migration file based on the given options and columns.
-
#output_model_migration(filename, name, columns, options = {}) ⇒ Object
Generates the model migration file created when generating a new model.
-
#remove_migration(name) ⇒ Object
Removes the migration file based on the migration name.
-
#return_last_migration_number ⇒ Object
Returns the number of the latest(most current) migration file.
Instance Method Details
#controller_actions(fields) ⇒ Object
Takes in fields for routes in the form of get:index post:test delete:yada.
189 190 191 192 193 194 195 |
# File 'lib/tennpipes-init/generators/components/actions.rb', line 189 def controller_actions(fields) field_tuples = fields.map { |value| value.split(":") } action_declarations = field_tuples.map do |request, name| "#{request} :#{name} do\n\nend\n" end action_declarations.join("\n").gsub(/^/, " " * 2).gsub(/^\s*$/, "") end |
#create_helper_files(app, name) ⇒ Object
197 198 199 200 201 202 203 204 |
# File 'lib/tennpipes-init/generators/components/actions.rb', line 197 def create_helper_files(app, name) @helper_name = "#{name.to_s.underscore.camelize}Helper" template 'templates/helper.rb.tt', destination_root(app, 'helpers', "#{name.to_s.underscore}_helper.rb") if test? include_component_module_for(:test) generate_helper_test(@helper_name, @project_name, @app_name) end end |
#current_migration_number ⇒ Object
Returns timestamp instead if :migration_format: in .components is “timestamp”
100 101 102 103 104 105 106 |
# File 'lib/tennpipes-init/generators/components/actions.rb', line 100 def current_migration_number if fetch_component_choice(:migration_format).to_s == 'timestamp' Time.now.utc.strftime("%Y%m%d%H%M%S") else return_last_migration_number + 1 end.to_s end |
#indent_spaces(count) ⇒ Object
Returns space characters of given count.
176 177 178 |
# File 'lib/tennpipes-init/generators/components/actions.rb', line 176 def indent_spaces(count) ' ' * count end |
#insert_mocking_include(library_name, options = {}) ⇒ Object
Injects the mock library include into the test class in test_config for setting up mock gen
163 164 165 166 167 168 |
# File 'lib/tennpipes-init/generators/components/actions.rb', line 163 def insert_mocking_include(library_name, ={}) .reverse_merge!(:indent => 2, :after => /class.*?\n/, :path => "test/test_config.rb") return unless File.exist?(destination_root([:path])) include_text = indent_spaces(2) + "include #{library_name}\n" inject_into_file([:path], include_text, :after => [:after]) end |
#insert_test_suite_setup(suite_text, options = {}) ⇒ Object
Injects the test class text into the test_config file for setting up the test gen.
146 147 148 149 |
# File 'lib/tennpipes-init/generators/components/actions.rb', line 146 def insert_test_suite_setup(suite_text, ={}) .reverse_merge!(:path => "test/test_config.rb") create_file([:path], suite_text.gsub(/CLASS_NAME/, "#{@project_name}::#{@app_name}")) end |
#migration_exist?(filename) ⇒ Boolean
Return true if the migration already exist.
114 115 116 |
# File 'lib/tennpipes-init/generators/components/actions.rb', line 114 def migration_exist?(filename) Dir[destination_root("db/migrate/*_#{filename.underscore}.rb")].size > 0 end |
#output_migration_file(filename, name, columns, options = {}) ⇒ Object
Generates a standalone migration file based on the given options and columns.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/tennpipes-init/generators/components/actions.rb', line 64 def output_migration_file(filename, name, columns, ={}) if behavior == :revoke remove_migration(name) else return if migration_exist?(filename) change_format = [:change_format] migration_scan = filename.underscore.camelize.scan(/(Add|Remove).*?(?:(?:To|From).+?)*(?:To|From)((?:To|From)?.*?)$/).flatten direction, table_name = migration_scan[0].downcase, migration_scan[1].downcase.pluralize if migration_scan.any? tuples = direction ? columns.map { |value| value.split(":") } : [] tuples.map! { |field, kind| kind =~ /datetime/i ? [field, 'DateTime'] : [field, kind] } add_columns = tuples.map(&[:add]).join("\n ") remove_columns = tuples.map(&[:remove]).join("\n ") forward_text = change_format.gsub(/!TABLE!/, table_name).gsub(/!COLUMNS!/, add_columns) if tuples.any? back_text = change_format.gsub(/!TABLE!/, table_name).gsub(/!COLUMNS!/, remove_columns) if tuples.any? contents = [:base].dup.gsub(/\s{4}!UP!\n/m, (direction == 'add' ? forward_text.to_s : back_text.to_s)) contents.gsub!(/\s{4}!DOWN!\n/m, (direction == 'add' ? back_text.to_s : forward_text.to_s)) contents = contents.gsub(/!FILENAME!/, filename.underscore).gsub(/!FILECLASS!/, filename.underscore.camelize) migration_number = current_migration_number contents.gsub!(/!VERSION!/, migration_number) migration_filename = "#{format("%03d", migration_number)}_#{filename.underscore}.rb" create_file(destination_root('db/migrate/', migration_filename), contents, :skip => true) end end |
#output_model_migration(filename, name, columns, options = {}) ⇒ Object
Generates the model migration file created when generating a new model.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/tennpipes-init/generators/components/actions.rb', line 24 def output_model_migration(filename, name, columns, ={}) if behavior == :revoke remove_migration(filename) else return if migration_exist?(filename) model_name = name.to_s.pluralize field_tuples = columns.map { |value| value.split(":") } field_tuples.map! { |field, kind| kind =~ /datetime/i ? [field, 'DateTime'] : [field, kind] } column_declarations = field_tuples.map(&[:column_format]).join("\n ") contents = [:base].dup.gsub(/\s{4}!UP!\n/m, [:up]).gsub(/!DOWN!\n/m, [:down]) contents = contents.gsub(/!NAME!/, model_name.underscore.camelize).gsub(/!TABLE!/, model_name.underscore) contents = contents.gsub(/!FILENAME!/, filename.underscore).gsub(/!FILECLASS!/, filename.underscore.camelize) migration_number = current_migration_number contents = contents.gsub(/!FIELDS!/, column_declarations).gsub(/!VERSION!/, migration_number) migration_filename = "#{format("%03d", migration_number)}_#{filename.underscore}.rb" create_file(destination_root('db/migrate/', migration_filename), contents, :skip => true) end end |
#remove_migration(name) ⇒ Object
Removes the migration file based on the migration name.
124 125 126 127 128 129 130 131 132 |
# File 'lib/tennpipes-init/generators/components/actions.rb', line 124 def remove_migration(name) migration_path = Dir[destination_root('db/migrate/*.rb')].find do |f| File.basename(f) =~ /#{name.to_s.underscore}/ end return unless migration_path if behavior == :revoke create_file migration_path # we use create to reverse the operation of a revoke end end |
#return_last_migration_number ⇒ Object
Returns the number of the latest(most current) migration file.
91 92 93 94 95 |
# File 'lib/tennpipes-init/generators/components/actions.rb', line 91 def return_last_migration_number Dir[destination_root('db/migrate/*.rb')].map { |f| File.basename(f).match(/^(\d+)/)[0].to_i }.max.to_i || 0 end |