Class: Binder::AR
- Inherits:
-
Object
- Object
- Binder::AR
- Defined in:
- lib/active_record_binder.rb
Overview
Public: Active Record Binder class. You need to inherit from it to create your Plug or Adaptor.
Examples
class ARSqlitePlug < Binder::AR
database 'db.sqlite3'
adapter :sqlite3
# Your methods
end
#
# Or
class ARMySqlPlug < Binder::AR
database 'db'
adapter :mysql
connect_with user: 'Foo', password: 'Bar', host: 'localhost'
# Your methods
end
# You need to initialize the plug by passing it a table to handle
plug = ARSqlitePlug.new :table_for_foo
foo = plug.table # => the TableForFoo class, which inherit from ActiveRecord::Base.
# Foo is an active record object linked to a table, you can use it as you would usually.
foo.first
# Furthermore, the Binder::AR class, has some interesting class methods :
#
Binder::AR::default_database # => ENV['APP_DB'] # This value would be used if no database had been set during the plus creation.
Binder::AR::database # => Returns the current database used by the Binder in general (Would default to ENV['APP_DB'] if not specified)
ARSqlitePlug::database # => :db (Works the same and returns this Plug Class's database parameters)
#
# The same goes for the adapters :
Binder::AR::default_adapter # => :sqlite3
ARMySqlPlug::adapter # => :mysql
#
# And connection parameters
Binder::AR::connection # => {}
ARMySqlPlug::connection # => { :user => 'Foo', :password => 'Bar', :host => 'localhost' }
Instance Attribute Summary collapse
-
#table ⇒ Object
readonly
Returns the value of attribute table.
-
#table_name ⇒ Object
readonly
Returns the value of attribute table_name.
Class Method Summary collapse
-
.__check_migration_version(version) ⇒ Object
Private : checks the migration version number.
-
.__create_meta_data_table_for(schema) ⇒ Object
Private : Create a table for the current meta schema.
-
.__create_meta_schema_class(klass) ⇒ Object
Private: Creates the meta schema class, and binds it to the current Namespace.
-
.adapter(adpt = default_adapter) ⇒ Object
(also: adaptor)
Public: Set the current adapter.
-
.connection(opts = {}) ⇒ Object
(also: connect_with)
Public: Set the current connection parameters.
-
.database(db = default_database) ⇒ Object
Public: Set the current database.
-
.default_adapter ⇒ Object
Public: Retrieves de default database.
-
.default_database ⇒ Object
Public: Retrieves de default database.
-
.meta_schema ⇒ Object
Private: Get the Class holding the current migration metadatas.
-
.migrate(version = nil) ⇒ Object
Public: Executes a migration.
-
.Version(n) ⇒ Object
Public: Creates a new migration version through subclassing.
Instance Method Summary collapse
-
#initialize(table_name) ⇒ AR
constructor
Public: Returns a new instance of the binder’s class.
Constructor Details
#initialize(table_name) ⇒ AR
Public: Returns a new instance of the binder’s class. It also automaticaly establish a connection with the database throught the specified adapter, but prevents trying to reconnect if the connection is already established.
table_name - A String or Symbol representing the table to which we want to be bound.
Examples
class Plug < Binder::AR; end
plug = Plug.new :articles
plug.table.find # [<Article#1>, <Article#2>] # Works, the connection has been established.
Returns an instance of the binder’s class.
66 67 68 69 70 71 72 |
# File 'lib/active_record_binder.rb', line 66 def initialize table_name @table_name = table_name this = self.class table = (this.database, this.adapter, this.connection) table.connect unless table.connected? end |
Instance Attribute Details
#table ⇒ Object (readonly)
Returns the value of attribute table.
51 52 53 |
# File 'lib/active_record_binder.rb', line 51 def table @table end |
#table_name ⇒ Object (readonly)
Returns the value of attribute table_name.
51 52 53 |
# File 'lib/active_record_binder.rb', line 51 def table_name @table_name end |
Class Method Details
.__check_migration_version(version) ⇒ Object
Private : checks the migration version number
Raises MigrationVersionError if the required version does not exist.
Returns the version as a Float.
311 312 313 314 |
# File 'lib/active_record_binder.rb', line 311 def __check_migration_version version raise MigrationVersionError if not version.nil? and @last_version < version version ||= @last_version end |
.__create_meta_data_table_for(schema) ⇒ Object
Private : Create a table for the current meta schema.
schema - The schema Class for which we want to create the table.
Examples
MyPlug # Will create a my_plug_meta_schemas table.
Returns Nothing.
293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/active_record_binder.rb', line 293 def schema # Clears the table cache for the schema (remove TableDoesNotExists if a table actually exists) schema.clear_cache! unless schema.table_exists? ActiveRecord::Schema.define do create_table schema.table_name do |t| t.column :version, :float end end end end |
.__create_meta_schema_class(klass) ⇒ Object
Private: Creates the meta schema class, and binds it to the current Namespace.
Examples
class Plug < Binder::AR; end
"MetaSchema"
# => Plug::MetaSchema
Returns a new Class inheriting from ActiveRecord::Base.
325 326 327 328 329 330 331 332 333 |
# File 'lib/active_record_binder.rb', line 325 def klass binder = self.name self.const_set(klass, Class.new(ActiveRecord::Base) do # class MetaSchema < ActiveRecord::Base singleton_class.send(:define_method, :table_name_prefix) do # def self.table_name_prefix "#{binder.underscore}_" # "foo_binder" # If we have a class FooBinder < Binder::AR end # end end) # end end |
.adapter(adpt = default_adapter) ⇒ Object Also known as: adaptor
Public: Set the current adapter
adpt - the adapter, as a String or Symbol. (default: Binder::AR.default_adapter)
Examples
class Plug < Binder::AR
adapter :foobar
end
Returns the current database.
136 137 138 139 140 141 142 |
# File 'lib/active_record_binder.rb', line 136 def adapter adpt = default_adapter if adpt == default_adapter @adapter ||= adpt else @adapter = adpt end end |
.connection(opts = {}) ⇒ Object Also known as: connect_with
Public: Set the current connection parameters
opts - A Hash, containing options to pass to the ActiveRecord::Base.establish_connection call (default: {})
user - A user name as a String.
password - A password as a String.
host - A host String.
Examples
class Plug < Binder::AR
adapter :foobar
end
Returns the current database.
159 160 161 |
# File 'lib/active_record_binder.rb', line 159 def connection opts = {} @connection ||= opts end |
.database(db = default_database) ⇒ Object
Public: Set the current database
db - the database, as a String or Symbol. (default: Binder::AR.default_database)
Examples
class Plug < Binder::AR
database :foobar
end
Returns the current database.
117 118 119 120 121 122 123 |
# File 'lib/active_record_binder.rb', line 117 def database db = default_database if db == default_database @database ||= db else @database = db end end |
.default_adapter ⇒ Object
Public: Retrieves de default database
Returns a the content of :sqlite3, as a Symbol.
172 |
# File 'lib/active_record_binder.rb', line 172 def default_adapter; :sqlite3 end |
.default_database ⇒ Object
Public: Retrieves de default database
Returns a the content of ENV.
167 |
# File 'lib/active_record_binder.rb', line 167 def default_database; ENV['APP_DB'] end |
.meta_schema ⇒ Object
Private: Get the Class holding the current migration metadatas.
Returns an ActiveRecord::Base subclass.
272 273 274 275 276 277 278 279 280 |
# File 'lib/active_record_binder.rb', line 272 def klass = :MetaSchema ||= if self.const_defined?(klass) self.const_get(klass) else self.(klass) end end |
.migrate(version = nil) ⇒ Object
Public: Executes a migration.
version - A Float or Numeric indicating the number towards which execute the migration.
If none specified it will migrate to the latest migration.
Examples
# (See the Binder::AR::Version examples for the classes definition.)
Plug.migrate 1.0 # Creates table bars.
# => 1.0
Plug.migrate 0 # Drop the table bars.
# => 0.0
Plug.migrate # Creates tables bars and foos.
# => 1.1
Returns the version we migrated to as a Float.
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/active_record_binder.rb', line 245 def migrate version = nil if @migrations schema = version = __check_migration_version(version) (schema) s = schema.first || schema.new(version: 0) unless s.version == version @migrations.sort_by { |migration| migration.version }.each do |m| m.migrate(:up) if s.version < m.version and m.version <= version if s.version >= m.version and m.version > version m.migrate(:down) else # Handle migrate(0) m.migrate(:down) if s.version >= m.version and version.zero? end end s.update_attribute :version, version end version = s.version end version end |
.Version(n) ⇒ Object
Public: Creates a new migration version through subclassing.
n - A migration version number as Float.
Examples
class Plug < Binder::AR; end
class CreateFooTable < Plug::Version 1.0
def self.up
create_table :foos do |t|
t.string :name
t.
end
end
def self.down
drop_table :foos
end
end
class CreateBarTable < Plug::Version 1.1
def self.up
create_table :bars do |t|
t.string :title
t.
end
end
def self.down
drop_table :bars
end
end
# You can get the migration version for a pecular migration class.
CreateFooTable.version # => 1.0
CreateBarTable.version # => 1.1
Returns a new Class, subclassing ActiveRecord::Migration.
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/active_record_binder.rb', line 213 def Version n @last_version = [n, @last_version.to_f].max # Assign or retrieves the last migration version number # migrations is a pointer to the instance variable migrations = (@migrations ||= []) Class.new(ActiveRecord::Migration) do singleton_class.send(:define_method, :version) do # def self.version n # n end # end # Callback invoked whenever a subclass of the current class is created singleton_class.send(:define_method, :inherited) do |s| # def self.inherited subclass migrations << s # migrations << subclass end # end end end |