Module: Lotus::Model

Includes:
Utils::ClassAttribute
Defined in:
lib/lotus/model.rb,
lib/lotus/model/mapper.rb,
lib/lotus/model/mapping.rb,
lib/lotus/model/version.rb,
lib/lotus/model/migrator.rb,
lib/lotus/model/config/mapper.rb,
lib/lotus/model/configuration.rb,
lib/lotus/model/config/adapter.rb,
lib/lotus/model/mapping/coercer.rb,
lib/lotus/model/migrator/adapter.rb,
lib/lotus/model/adapters/abstract.rb,
lib/lotus/model/mapping/coercions.rb,
lib/lotus/model/adapters/sql/query.rb,
lib/lotus/model/mapping/collection.rb,
lib/lotus/model/adapters/sql/command.rb,
lib/lotus/model/adapters/sql/console.rb,
lib/lotus/model/adapters/sql_adapter.rb,
lib/lotus/model/adapters/memory/query.rb,
lib/lotus/model/adapters/null_adapter.rb,
lib/lotus/model/migrator/mysql_adapter.rb,
lib/lotus/model/adapters/implementation.rb,
lib/lotus/model/adapters/memory/command.rb,
lib/lotus/model/adapters/memory_adapter.rb,
lib/lotus/model/adapters/sql/collection.rb,
lib/lotus/model/migrator/sqlite_adapter.rb,
lib/lotus/model/migrator/postgres_adapter.rb,
lib/lotus/model/adapters/memory/collection.rb,
lib/lotus/model/adapters/sql/consoles/mysql.rb,
lib/lotus/model/adapters/file_system_adapter.rb,
lib/lotus/model/adapters/sql/consoles/sqlite.rb,
lib/lotus/model/adapters/sql/consoles/postgresql.rb

Overview

Model

Since:

  • 0.1.0

Defined Under Namespace

Modules: Adapters, Config, Mapping, Migrator Classes: Configuration, InvalidMappingError, InvalidQueryError, Mapper, MigrationError, NoMappingError, NonPersistedEntityError, NullMapper

Constant Summary collapse

VERSION =

Defines the version

Since:

  • 0.1.0

'0.4.1'.freeze

Class Method Summary collapse

Class Method Details

.configure(&blk) ⇒ Object

Configure the framework. It yields the given block in the context of the configuration

Adapter MUST follow the convention in which adapter class is inflection of adapter name The above example has name :sql, thus derived class will be ‘Lotus::Model::Adapters::SqlAdapter`

Examples:

require 'lotus/model'

Lotus::Model.configure do
  adapter type: :sql, uri: 'postgres://localhost/database'

  mapping do
    collection :users do
      entity User

      attribute :id,   Integer
      attribute :name, String
    end
  end
end

Parameters:

  • blk (Proc)

    the configuration block

See Also:

Since:

  • 0.2.0



77
78
79
80
# File 'lib/lotus/model.rb', line 77

def self.configure(&blk)
  configuration.instance_eval(&blk)
  self
end

.dupeModule

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Duplicate Lotus::Model in order to create a new separated instance of the framework.

The new instance of the framework will be completely decoupled from the original. It will inherit the configuration, but all the changes that happen after the duplication, won’t be reflected on the other copies.

 @since 0.2.0

Examples:

Basic usage

require 'lotus/model'

module MyApp
  Model = Lotus::Model.dupe
end

MyApp::Model == Lotus::Model # => false

MyApp::Model.configuration ==
  Lotus::Model.configuration # => false

Inheriting configuration

require 'lotus/model'

Lotus::Model.configure do
  adapter type: :sql, uri: 'sqlite3://uri'
end

module MyApp
  Model = Lotus::Model.dupe
end

module MyApi
  Model = Lotus::Model.dupe
  Model.configure do
    adapter type: :sql, uri: 'postgresql://uri'
  end
end

Lotus::Model.configuration.adapter_config.uri # => 'sqlite3://uri'
MyApp::Model.configuration.adapter_config.uri # => 'sqlite3://uri'
MyApi::Model.configuration.adapter_config.uri # => 'postgresql://uri'

Returns:

  • (Module)

    a copy of Lotus::Model

Since:

  • 0.1.0



143
144
145
146
147
# File 'lib/lotus/model.rb', line 143

def self.dupe
  dup.tap do |duplicated|
    duplicated.configuration = Configuration.new
  end
end

.duplicate(mod, &blk) ⇒ Module

Duplicate the framework and generate modules for the target application

 @since 0.2.0

Examples:

Basic usage

require 'lotus/model'

module MyApp
  Model = Lotus::Model.dupe
end

# It will:
#
# 1. Generate MyApp::Model
# 2. Generate MyApp::Entity
# 3. Generate MyApp::Repository

MyApp::Model      == Lotus::Model # => false
MyApp::Repository == Lotus::Repository # => false

Block usage

require 'lotus/model'

module MyApp
  Model = Lotus::Model.duplicate(self) do
    adapter type: :memory, uri: 'memory://localhost'
  end
end

Lotus::Model.configuration.adapter_config # => nil
MyApp::Model.configuration.adapter_config # => #<Lotus::Model::Config::Adapter:0x007ff0ff0244f8 @type=:memory, @uri="memory://localhost", @class_name="MemoryAdapter">

Parameters:

  • mod (Module)

    the Ruby namespace of the application

  • blk (Proc)

    an optional block to configure the framework

Returns:

  • (Module)

    a copy of Lotus::Model

See Also:

Since:

  • 0.1.0



188
189
190
191
192
193
194
195
196
197
# File 'lib/lotus/model.rb', line 188

def self.duplicate(mod, &blk)
  dupe.tap do |duplicated|
    mod.module_eval %{
      Entity = Lotus::Entity.dup
      Repository = Lotus::Repository.dup
    }

    duplicated.configure(&blk) if block_given?
  end
end

.load!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Load the framework

Since:

  • 0.2.0



86
87
88
# File 'lib/lotus/model.rb', line 86

def self.load!
  configuration.load!
end

.migration(&blk) ⇒ Object

Define a migration

It must define an up/down strategy to write schema changes (up) and to rollback them (down).

We can use up and down blocks for custom strategies, or only one change block that automatically implements “down” strategy.

Examples:

Use up/down blocks

Lotus::Model.migration do
  up do
    create_table :books do
      primary_key :id
      column :book, String
    end
  end

  down do
    drop_table :books
  end
end

Use change block

Lotus::Model.migration do
  change do
    create_table :books do
      primary_key :id
      column :book, String
    end
  end

  # DOWN strategy is automatically generated
end

Parameters:

  • blk (Proc)

    a block that defines up/down or change database migration

Since:

  • 0.4.0



50
51
52
# File 'lib/lotus/model/migrator.rb', line 50

def self.migration(&blk)
  Sequel.migration(&blk)
end

.unload!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Unload the framework

Since:

  • 0.2.0



94
95
96
# File 'lib/lotus/model.rb', line 94

def self.unload!
  configuration.unload!
end