Class: Mongify::Database::Table
- Inherits:
-
Object
- Object
- Mongify::Database::Table
- 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
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#name ⇒ Object
Returns the no_sql collection name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#sql_name ⇒ Object
Returns the value of attribute sql_name.
Instance Method Summary collapse
-
#add_column(column) ⇒ Object
Add a Database Column to the table This expects to get a Column or it will raise Mongify::DatabaseColumnExpected otherwise.
-
#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.
-
#column(name, type = nil, options = {}) ⇒ Object
Lets you build a column in the table.
-
#embed_as ⇒ Object
Returns the type of embed it will be [object or array].
-
#embed_in ⇒ Object
Returns the name of the embed_in collection.
-
#embed_on ⇒ Object
Returns the name of the target column to embed on.
-
#embedded? ⇒ Boolean
Returns true if this is an embedded table.
-
#embedded_as_object? ⇒ Boolean
Returns true if table is being embed as an object.
-
#find_column(name) ⇒ Object
Returns the column if found by the sql_name.
-
#ignored? ⇒ Boolean
Returns true if table is ignored.
-
#initialize(sql_name, options = {}, &block) ⇒ Table
constructor
A new instance of Table.
-
#key_column ⇒ Object
Returns the column of type :key.
-
#polymorphic? ⇒ Boolean
Returns true if table is marked as polymorphic.
-
#polymorphic_as ⇒ Object
Returns the name of the polymorphic association.
-
#reference_columns ⇒ Object
Returns a array of Columns which reference other columns.
-
#remove_before_save_filter! ⇒ Object
Used to remove any before save filter.
-
#translate(row, parent = nil) ⇒ Object
Returns a translated row Takes in a hash of values.
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, ={}, &block) @columns = [] @column_lookup = {} @options = .stringify_keys self.sql_name = sql_name self.instance_exec(&block) if block_given? import_columns self end |
Instance Attribute Details
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
62 63 64 |
# File 'lib/mongify/database/table.rb', line 62 def columns @columns end |
#name ⇒ Object
Returns the no_sql collection name
78 79 80 |
# File 'lib/mongify/database/table.rb', line 78 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
62 63 64 |
# File 'lib/mongify/database/table.rb', line 62 def @options end |
#sql_name ⇒ Object
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
99 100 101 102 |
# File 'lib/mongify/database/table.rb', line 99 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
168 169 170 |
# File 'lib/mongify/database/table.rb', line 168 def before_save(&block) @before_save_callback = block end |
#column(name, type = nil, options = {}) ⇒ Object
Lets you build a column in the table
105 106 107 108 109 |
# File 'lib/mongify/database/table.rb', line 105 def column(name, type=nil, ={}) , type = type, nil if type.is_a?(Hash) type = type.to_sym if type add_and_index_column(Mongify::Database::Column.new(name, type, )) end |
#embed_as ⇒ Object
Returns the type of embed it will be [object or array]
145 146 147 148 149 |
# File 'lib/mongify/database/table.rb', line 145 def return nil unless return 'object' if @options['as'].to_s.downcase == 'object' 'array' end |
#embed_in ⇒ Object
Returns the name of the embed_in collection
140 141 142 |
# File 'lib/mongify/database/table.rb', line 140 def @options['embed_in'].to_s unless @options['embed_in'].nil? end |
#embed_on ⇒ Object
Returns the name of the target column to embed on
162 163 164 165 |
# File 'lib/mongify/database/table.rb', line 162 def return nil unless (@options['on'] || "#{@options['embed_in'].to_s.singularize}_id").to_s end |
#embedded? ⇒ Boolean
Returns true if this is an embedded table
157 158 159 |
# File 'lib/mongify/database/table.rb', line 157 def .present? end |
#embedded_as_object? ⇒ Boolean
Returns true if table is being embed as an object
152 153 154 |
# File 'lib/mongify/database/table.rb', line 152 def == 'object' end |
#find_column(name) ⇒ Object
Returns the column if found by the sql_name
112 113 114 115 |
# File 'lib/mongify/database/table.rb', line 112 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
83 84 85 |
# File 'lib/mongify/database/table.rb', line 83 def ignored? @options['ignore'] end |
#key_column ⇒ Object
Returns the column of type :key
123 124 125 |
# File 'lib/mongify/database/table.rb', line 123 def key_column @columns.find{ |c| c.type == :key } end |
#polymorphic? ⇒ Boolean
Returns true if table is marked as polymorphic
88 89 90 |
# File 'lib/mongify/database/table.rb', line 88 def polymorphic? !!@options['polymorphic'] end |
#polymorphic_as ⇒ Object
Returns the name of the polymorphic association
93 94 95 |
# File 'lib/mongify/database/table.rb', line 93 def polymorphic_as @options['polymorphic'].to_s end |
#reference_columns ⇒ Object
Returns a array of Columns which reference other columns
118 119 120 |
# File 'lib/mongify/database/table.rb', line 118 def reference_columns @columns.reject{ |c| !c.referenced? } end |
#remove_before_save_filter! ⇒ Object
Used to remove any before save filter
173 174 175 |
# File 'lib/mongify/database/table.rb', line 173 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
129 130 131 132 133 134 135 136 |
# File 'lib/mongify/database/table.rb', line 129 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 |