module SchemaReader::ClassMethods

Public Instance Methods

attr_schema(options = {}) click to toggle source
# File lib/schema_reader.rb, line 29
def attr_schema(options = {})
  table = options.fetch(:table)
  file = options.fetch(:file)
  self.send(:attr_accessor, *read_schema(table, file))
end
pass_attributes_to_new(bool=true) click to toggle source
# File lib/schema_reader.rb, line 21
def pass_attributes_to_new(bool=true)
  if bool
    define_method("initialize") do |options|
      update(options)
    end
  end
end
read_schema(table_selected, path) click to toggle source
# File lib/schema_reader.rb, line 35
def read_schema(table_selected, path)
  tables = parse_for_tables(path)
  table = find_table(tables, table_selected)
  field_names = get_table_field_name(table)
  add_associations(field_names)
end

Private Instance Methods

add_associations(field_names) click to toggle source
# File lib/schema_reader.rb, line 44
def add_associations(field_names)
  association_fields = field_names.select {|field| field =~ /_id/}
  field_names += association_fields.map {|field| field.to_s.sub('_id', '').to_sym}
  field_names
end
find_table(tables, table_selected) click to toggle source
# File lib/schema_reader.rb, line 54
def find_table(tables, table_selected)
  table = tables.select do |table|
    table.name == table_selected
  end
  raise "Table Name not Found!" if table.first.nil?
  table.first
end
get_table_field_name(table) click to toggle source
# File lib/schema_reader.rb, line 62
def get_table_field_name(table)
  table.attributes.map do |attribute|
    attribute.name.to_sym
  end
end
parse_for_tables(path) click to toggle source
# File lib/schema_reader.rb, line 50
def parse_for_tables(path)
  File.read(path).split(/^\s*create_/)[1..-1].map {|table_data| Table.parse table_data }
end