Class: Mongify::Translation

Inherits:
Object
  • Object
show all
Includes:
Printer, Process
Defined in:
lib/mongify/translation.rb,
lib/mongify/translation/printer.rb,
lib/mongify/translation/process.rb

Overview

Actually runs the translation from sql to no sql

Basic translation file should look like this:

table "users" do
  column "id", :key
  column "first_name", :string
  column "last_name", :string
  column "created_at", :datetime
  column "updated_at", :datetime
end

table "posts" do
  column "id", :key
  column "title", :string
  column "owner_id", :integer, :references => :users
  column "body", :text
  column "published_at", :datetime
  column "created_at", :datetime
  column "updated_at", :datetime
end

table "comments", :embed_in => :posts, :on => :post_id do
  column "id", :key
  column "body", :text
  column "post_id", :integer, :referneces => :posts
  column "user_id", :integer, :references => :users
  column "created_at", :datetime
  column "updated_at", :datetime
end

table "preferences", :embed_in => :users, :as => :object do
  column "id", :key
  column "user_id", :integer, :references => "users"
  column "notify_by_email", :boolean
end

table "notes", :embed_in => true, :polymorphic => 'notable' do
  column "id", :key
  column "user_id", :integer, :references => "users"
  column "notable_id", :integer
  column "notable_type", :string
  column "body", :text
  column "created_at", :datetime
  column "updated_at", :datetime
end

Defined Under Namespace

Modules: Printer, Process

Instance Attribute Summary

Attributes included from Process

#no_sql_connection, #sql_connection

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Process

#process

Methods included from Printer

#print

Constructor Details

#initializeTranslation

Returns a new instance of Translation.



80
81
82
# File 'lib/mongify/translation.rb', line 80

def initialize
  @all_tables = []
end

Class Method Details

.load(connection) ⇒ Object

Returns an instence of a translation object with a given sql connection layout loaded



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mongify/translation.rb', line 65

def load(connection)
  raise Mongify::SqlConnectionRequired, "Can only read from Mongify::Database::SqlConnection" unless connection.is_a?(Mongify::Database::SqlConnection)
  return unless connection.valid? && connection.has_connection?
  translation = self.new
  connection.tables.each do |t|
    columns = []
    connection.columns_for(t).each do |ar_col|
      columns << Mongify::Database::Column.new(ar_col.name, ar_col.type, :auto_detect => true)
    end
    translation.table(t, :columns => columns)
  end
  translation
end

.parse(file_name) ⇒ Object

Returns an instance of a translation object Takes a location of a translation file



58
59
60
61
62
# File 'lib/mongify/translation.rb', line 58

def parse(file_name)
  translation = self.new
  translation.instance_eval(File.read(file_name))
  translation
end

Instance Method Details

#add_table(table) ⇒ Object

Adds a Database::Table to the list of tables



96
97
98
99
# File 'lib/mongify/translation.rb', line 96

def add_table(table)
  @all_tables << table
  table
end

#all_tablesObject

Returns an array of all tables in the translation



102
103
104
# File 'lib/mongify/translation.rb', line 102

def all_tables
  @all_tables
end

#copy_tablesObject

Returns an array of all tables that have not been ignored and are just straight copy tables



112
113
114
# File 'lib/mongify/translation.rb', line 112

def copy_tables
  tables.reject{|t| t.embedded?}
end

#embed_tablesObject

Returns an array of all tables that have not been ignored and are to be embedded



122
123
124
# File 'lib/mongify/translation.rb', line 122

def embed_tables
  tables.reject{|t| !t.embedded?}
end

#find(name) ⇒ Object

finds table by name



85
86
87
# File 'lib/mongify/translation.rb', line 85

def find(name)
  all_tables.find{ |t| t.name == name }
end

#polymorphic_tablesObject

Returns an array of all tables that have a polymorphic relationship



117
118
119
# File 'lib/mongify/translation.rb', line 117

def polymorphic_tables
  all_tables.reject{ |t| t.ignored? || !t.polymorphic? }
end

#table(table_name, options = {}, &block) ⇒ Object

Creates a Database::Table from the given input and adds it to the list of tables



90
91
92
93
# File 'lib/mongify/translation.rb', line 90

def table(table_name, options={}, &block)
  table = Mongify::Database::Table.new(table_name, options, &block)
  add_table(table)
end

#tablesObject

Returns an array of all tables that have not been ingored



107
108
109
# File 'lib/mongify/translation.rb', line 107

def tables
  all_tables.reject{ |t| t.ignored? || t.polymorphic? }
end