/ruby-sheets-autodetect

LIbrary to auto-infer Google Sheets cells value types and construct rails scaffold accordingly

Primary LanguageRuby

Synopsis

This library tries to auto-detect Spreadsheet cells (using manual rudimentary inference! Hence totally error prone!) and autodetect

TL;DR

Given a spreadsheet with a tab "Cars" and these values:

Name	Description	isSportsCar	Brand	Modello	DoB	Num Wheels
Bolide	il mio gran ferro	TRUE	Ferrari	Testarossa	Fri, 5 May 	4
Fiona	la mia macchina bellissima	FALSE	Mercedes	C180 Avantgarde	Thu, 1 May 	4

Input Data

It will generate this string (note: still work in progress to detect Text vs String):

rails generate scaffold Car name:Text description:Text issportscar:Boolean brand:Text modello:Text dob:Text num_wheels:Integer discordanti:Text colore:Text

Ouput Data

Install

All you need to do is two things:

google-drive-ruby provides a good auth link too: https://github.com/gimite/google-drive-ruby/blob/master/doc/authorization.md

Run

After you install gems and Service Account, you can go on and use the script:

  • Create a Google Spreadsheet and note the URL. from it, note the last part of URL (like 1pWqRWW3qhPfzjdmN0_D9Cz_D3fi0qv_fb1z2NeckSt8)
  • Call the script in its magnificense, like: ruby sheet-autodetect-all.rb 1pWqRWW3qhPfzjdmN0_D9Cz_D3fi0qv_fb1z2NeckSt8

Notes

problems

Detect type from string seems the hardest part. Internet seems to suggest that any try to do it is futile and lame. Look at my implementation and you'll definitely agree :) Of course if I detect "2" as integer there's no way in the world you can have "2" as string. Unless the "MyColumn" has "2", "ciao", "mamma" -> then majority will be strings.

(Supported) Rails types

These are rails types (different from Ruby types, at times, eg with Booleans):

(*) integer
primary_key
(*) decimal # (for currency)
(*) float
(*) boolean # for TrueClass and FalseClass
binary
(*) string
(*) text    
(*) date
(*) time
(*) datetime

Note: for text/string discrimination, it uses a simple algorithm (# of strings above 10chars - if >30% then TEXT ). Note I dont want to use majority since empty strings count as 'short' and i wanted to keep it simple.

TODOs

  • [DONE] P1 infer String vs Text
  • P2 import data into Fixtures, with right types.
  • P3 create a script that launches rails generate and rake db:migrate, maybe allowing you to customize, like create proper singularization for your tabs or allowing you to adjust values.

Credits