class AppRail::Airtable::Generator

Public Class Methods

exit_on_failure?() click to toggle source
# File lib/app_rail/airtable/generator.rb, line 27
def self.exit_on_failure?
  true
end
source_root() click to toggle source
# File lib/app_rail/airtable/generator.rb, line 18
def self.source_root
  File.join(File.dirname(__FILE__), '..', '..', '..')
end

Public Instance Methods

create_project() click to toggle source
# File lib/app_rail/airtable/generator.rb, line 22
def create_project
  validate_airtable_schema
  directory('templates/project', output_directory)
end

Private Instance Methods

compare_fields(table_fields, airtable_table) click to toggle source
# File lib/app_rail/airtable/generator.rb, line 51
def compare_fields(table_fields, airtable_table)
  airtable_table.records.first.fields.each_key do |key|
    next if table_fields.include? key

    raise "ERROR! The key \"#{key}\" is not present in the schema definition."
  end
end
fields_as_params_examples(fields) click to toggle source
# File lib/app_rail/airtable/generator.rb, line 59
def fields_as_params_examples(fields)
  fields.each_with_object({}) do |field, hsh|
    hsh[field['name']] = case field['type']
                         when 'date'
                           '01/01/2022'
                         when 'string', 'text'
                           'MyString'
                         when 'integer'
                           10
                         else
                           'MyString'
                         end
  end
end
find_table(table_name) click to toggle source
# File lib/app_rail/airtable/generator.rb, line 47
def find_table(table_name)
  Airrecord.table(api_key, base_id, table_name)
end
tables() click to toggle source
# File lib/app_rail/airtable/generator.rb, line 43
def tables
  @tables ||= schema ? JSON.parse(schema)['tables'] : []
end
validate_airtable_schema() click to toggle source
# File lib/app_rail/airtable/generator.rb, line 33
def validate_airtable_schema
  tables.each do |table|
    table_name = table['name']
    table_fields = table['fields'].concat(table['associations']).map { |field| field['name'] }.compact

    airtable_table = find_table(table_name)
    compare_fields(table_fields, airtable_table)
  end
end