Class: Apiify::Scaffolder
- Inherits:
-
Object
- Object
- Apiify::Scaffolder
- Defined in:
- lib/apiify/scaffolder.rb
Instance Method Summary collapse
- #confirm(csv_path, index_col) ⇒ Object
- #create_scaffold(csv_path, index_col) ⇒ Object
- #find_class(path) ⇒ Object
- #generate(csv_path, index_col) ⇒ Object
- #get_file_name(csv_path) ⇒ Object
- #hash_to_string(hash, index_col) ⇒ Object
- #run_scaffold(csv_path, index_col) ⇒ Object
Instance Method Details
#confirm(csv_path, index_col) ⇒ Object
55 56 57 58 |
# File 'lib/apiify/scaffolder.rb', line 55 def confirm(csv_path, index_col) puts "Created #{get_file_name(csv_path).capitalize} model with properties #{hash_to_string(find_class(csv_path),index_col)}" puts "Please run `bin/rake db:migrate`" end |
#create_scaffold(csv_path, index_col) ⇒ Object
45 46 47 48 49 |
# File 'lib/apiify/scaffolder.rb', line 45 def create_scaffold(csv_path, index_col) model_name = get_file_name(csv_path).capitalize hash_result = find_class(csv_path) "bin/rails g scaffold #{model_name} #{hash_to_string(hash_result, index_col)}" end |
#find_class(path) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/apiify/scaffolder.rb', line 16 def find_class(path) table = CSV.table(path) headers = table.headers result = {} headers.each do |header| table.each do |row| if row[header].class != Float result[header] = row[header].class else result[header] = row[header].class break end end end result end |
#generate(csv_path, index_col) ⇒ Object
3 4 5 6 |
# File 'lib/apiify/scaffolder.rb', line 3 def generate(csv_path, index_col) run_scaffold(csv_path, index_col) confirm(csv_path, index_col) end |
#get_file_name(csv_path) ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/apiify/scaffolder.rb', line 8 def get_file_name(csv_path) if is_a_csv?(csv_path) get_file(csv_path).first else raise "Error: wrong file type. Please supply a .csv file" end end |
#hash_to_string(hash, index_col) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/apiify/scaffolder.rb', line 33 def hash_to_string(hash, index_col) output_str = "" hash.each do |key, value| if !index_col.nil? && key.to_s == index_col output_str += "#{key}:#{value.to_s.downcase}:index " else output_str += "#{key}:#{value.to_s.downcase} " end end output_str.strip end |
#run_scaffold(csv_path, index_col) ⇒ Object
51 52 53 |
# File 'lib/apiify/scaffolder.rb', line 51 def run_scaffold(csv_path, index_col) system(create_scaffold(csv_path, index_col)) end |