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

table "preferences", :embed_in => "users" do               # As of version 0.2, embedded tables with a before_save will take an
  before_save do |pref_row, user_row|                      # extra argument which is the parent row of the embedded table.
    user_row.email_me = pref_row.delete('email_me')        # This gives you the ability to move things from an embedded table row
  end                                                      # to the parent row.
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Table.



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mongify/database/table.rb', line 64

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.



62
63
64
# File 'lib/mongify/database/table.rb', line 62

def columns
  @columns
end

#nameObject

Returns the no_sql collection name



78
79
80
# File 'lib/mongify/database/table.rb', line 78

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



62
63
64
# File 'lib/mongify/database/table.rb', line 62

def options
  @options
end

#sql_nameObject

Returns the value of attribute sql_name.



61
62
63
# File 'lib/mongify/database/table.rb', line 61

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



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

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



169
170
171
# File 'lib/mongify/database/table.rb', line 169

def before_save(&block)
  @before_save_callback = block
end

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

Lets you build a column in the table



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

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]



146
147
148
149
150
# File 'lib/mongify/database/table.rb', line 146

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



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

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



163
164
165
166
# File 'lib/mongify/database/table.rb', line 163

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)


158
159
160
# File 'lib/mongify/database/table.rb', line 158

def embedded?
  embed_in.present?
end

#embedded_as_object?Boolean

Returns true if table is being embed as an object

Returns:

  • (Boolean)


153
154
155
# File 'lib/mongify/database/table.rb', line 153

def embedded_as_object?
  embed_as == 'object'
end

#find_column(name) ⇒ Object

Returns the column if found by the sql_name



113
114
115
116
# File 'lib/mongify/database/table.rb', line 113

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)


84
85
86
# File 'lib/mongify/database/table.rb', line 84

def ignored?
  @options['ignore']
end

#key_columnObject

Returns the column of type :key



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

def key_column
  @columns.find{ |c| c.type == :key }
end

#polymorphic?Boolean

Returns true if table is marked as polymorphic

Returns:

  • (Boolean)


89
90
91
# File 'lib/mongify/database/table.rb', line 89

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

#polymorphic_asObject

Returns the name of the polymorphic association



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

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

#reference_columnsObject

Returns a array of Columns which reference other columns



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

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

#remove_before_save_filter!Object

Used to remove any before save filter



174
175
176
# File 'lib/mongify/database/table.rb', line 174

def remove_before_save_filter!
  @before_save_callback = nil
end

#translate(row, parent = nil) ⇒ Object

Returns a translated row Takes in a hash of values



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

def translate(row, parent=nil)
  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, parent)
end