Class: Mongify::Database::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/mongify/database/table.rb

Overview

A representation of a sql table and how it should map to a no_sql collection

Structure

Structure for defining a table is as follows:

table "table_name", {options} do
  # columns go here...
end

Options

Table Options are as follow:

table "table_name"                                        # Does a straight copy of the table
table "table_name", :embed_in => 'users'                  # Embeds table_name into users, assuming a user_id is present in table_name.
                                                          # This will also assume you want the table embedded as an array.

table "table_name",                                       # Embeds table_name into users, linking it via a owner_id
      :embed_in => 'users',                               # This will also assume you want the table embedded as an array.
      :on => 'owner_id'                                   

table "table_name",                                       # Embeds table_name into users as a one to one relationship
      :embed_in => 'users',                               # This also assumes you have a user_id present in table_name
      :on => 'owner_id',                                  # You can also specify both :on and :as options when embedding
      :as => 'object'                                     # NOTE: If you rename the owner_id column, make sure you 
                                                          # update the :on to the new column name

table "table_name", :rename_to => 'my_table'              # This will allow you to rename the table as it's getting process
                                                          # Just remember that columns that use :reference need to
                                                          # reference the new name.

table "table_name", :ignore => true                       # This will ignore the whole table (like it doesn't exist)
                                                          # This option is good for tables like: schema_migrations

table "table_name",                                       # This allows you to specify the table as being polymorphic
      :polymorphic => 'notable',                          # and provide the name of the polymorphic relationship.
      :embed_in => true                                   # Setting embed_in => true allows the relationship to be
                                                          # embedded directly into the parent class.
                                                          # If you do not embed it, the polymorphic table will be copied in to
                                                          # MongoDB and the notable_id will be updated to the new BSON::ObjectID

table "table_name" do                                     # A table can take a before_save block that will be called just
  before_save do |row|                                    # before the row is saved to the no sql database.
    row.admin = row.delete('permission').to_i > 50        # This gives you the ability to do very powerful things like:
  end                                                     # Moving records around, renaming records, changing values in row based on
end                                                       # some values! Checkout Mongify::Database::DataRow to learn more

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sql_name, options = {}, &block) ⇒ Table

Returns a new instance of Table.



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mongify/database/table.rb', line 56

def initialize(sql_name, options={}, &block)
  @columns = []
  @column_lookup = {}
  @options = options.stringify_keys
  self.sql_name = sql_name
  
  self.instance_exec(&block) if block_given?
  
  import_columns
  
  self
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



54
55
56
# File 'lib/mongify/database/table.rb', line 54

def columns
  @columns
end

#nameObject

Returns the no_sql collection name



70
71
72
# File 'lib/mongify/database/table.rb', line 70

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



54
55
56
# File 'lib/mongify/database/table.rb', line 54

def options
  @options
end

#sql_nameObject

Returns the value of attribute sql_name.



53
54
55
# File 'lib/mongify/database/table.rb', line 53

def sql_name
  @sql_name
end

Instance Method Details

#add_column(column) ⇒ Object

Add a Database Column to the table This expects to get a Column or it will raise Mongify::DatabaseColumnExpected otherwise



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

def add_column(column)
  raise Mongify::DatabaseColumnExpected, "Expected a Mongify::Database::Column" unless column.is_a?(Mongify::Database::Column)
  add_and_index_column(column)
end

#before_save(&block) ⇒ Object

Used to save a block to be ran after the row has been processed but before it’s saved to the no sql database



156
157
158
# File 'lib/mongify/database/table.rb', line 156

def before_save(&block)
  @before_save = block
end

#column(name, type = nil, options = {}) ⇒ Object

Lets you build a column in the table



98
99
100
101
102
# File 'lib/mongify/database/table.rb', line 98

def column(name, type=nil, options={})
  options, type = type, nil if type.is_a?(Hash)
  type = type.to_sym if type
  add_and_index_column(Mongify::Database::Column.new(name, type, options))
end

#embed_asObject

Returns the type of embed it will be [object or array]



133
134
135
136
137
# File 'lib/mongify/database/table.rb', line 133

def embed_as
  return nil unless embedded?
  return 'object' if @options['as'].to_s.downcase == 'object'
  'array'
end

#embed_inObject

Returns the name of the embed_in collection



128
129
130
# File 'lib/mongify/database/table.rb', line 128

def embed_in
  @options['embed_in'].to_s unless @options['embed_in'].nil?
end

#embed_onObject

Returns the name of the target column to embed on



150
151
152
153
# File 'lib/mongify/database/table.rb', line 150

def embed_on
  return nil unless embedded?
  (@options['on'] || "#{@options['embed_in'].to_s.singularize}_id").to_s
end

#embedded?Boolean

Returns true if this is an embedded table

Returns:

  • (Boolean)


145
146
147
# File 'lib/mongify/database/table.rb', line 145

def embedded?
  embed_in.present?
end

#embedded_as_object?Boolean

Returns true if table is being embed as an object

Returns:

  • (Boolean)


140
141
142
# File 'lib/mongify/database/table.rb', line 140

def embedded_as_object?
  embed_as == 'object'
end

#find_column(name) ⇒ Object

Returns the column if found by the sql_name



105
106
107
108
# File 'lib/mongify/database/table.rb', line 105

def find_column(name)
  return nil unless (index = @column_lookup[name.to_s.downcase.to_sym])
  @columns[index]
end

#ignored?Boolean

Returns true if table is ignored

Returns:

  • (Boolean)


76
77
78
# File 'lib/mongify/database/table.rb', line 76

def ignored?
  @options['ignore']
end

#polymorphic?Boolean

Returns true if table is marked as polymorphic

Returns:

  • (Boolean)


81
82
83
# File 'lib/mongify/database/table.rb', line 81

def polymorphic?
  !!@options['polymorphic']
end

#polymorphic_asObject

Returns the name of the polymorphic association



86
87
88
# File 'lib/mongify/database/table.rb', line 86

def polymorphic_as
  @options['polymorphic'].to_s
end

#reference_columnsObject

Returns a array of Columns which reference other columns



111
112
113
# File 'lib/mongify/database/table.rb', line 111

def reference_columns
  @columns.reject{ |c| !c.referenced? } 
end

#translate(row) ⇒ Object

Returns a translated row Takes in a hash of values



117
118
119
120
121
122
123
124
# File 'lib/mongify/database/table.rb', line 117

def translate(row)
  new_row = {}
  row.each do |key, value|
    c = find_column(key)
    new_row.merge!(c.translate(value)) if c.present?
  end
  run_before_save(new_row)
end