Class: Mongify::Translation

Inherits:
Object
  • Object
show all
Includes:
Printer, Process, ProcessorCommon, Sync
Defined in:
lib/mongify/translation.rb,
lib/mongify/translation/sync.rb,
lib/mongify/translation/printer.rb,
lib/mongify/translation/process.rb,
lib/mongify/translation/processor_common.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, :references => :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, ProcessorCommon, Sync

Constant Summary

Constants included from Sync

Sync::DRAFT_KEY, Sync::SYNC_HELPER_TABLE

Instance Attribute Summary

Attributes included from ProcessorCommon

#no_sql_connection, #sql_connection

Attributes included from Sync

#max_updated_at

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Sync

#sync

Methods included from Process

#process

Methods included from Printer

#print

Constructor Details

#initializeTranslation

Returns a new instance of Translation.



82
83
84
# File 'lib/mongify/translation.rb', line 82

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



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

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



60
61
62
63
64
# File 'lib/mongify/translation.rb', line 60

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



98
99
100
101
# File 'lib/mongify/translation.rb', line 98

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

#all_tablesObject

Returns an array of all tables in the translation



104
105
106
# File 'lib/mongify/translation.rb', line 104

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



114
115
116
# File 'lib/mongify/translation.rb', line 114

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



124
125
126
# File 'lib/mongify/translation.rb', line 124

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

#find(name) ⇒ Object

finds table by name



87
88
89
# File 'lib/mongify/translation.rb', line 87

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

#polymorphic_tablesObject

Returns an array of all tables that have a polymorphic relationship



119
120
121
# File 'lib/mongify/translation.rb', line 119

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



92
93
94
95
# File 'lib/mongify/translation.rb', line 92

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



109
110
111
# File 'lib/mongify/translation.rb', line 109

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