Class: Lotus::Model::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/lotus/model/configuration.rb

Overview

Configuration for the framework, models and adapters.

Lotus::Model has its own global configuration that can be manipulated via ‘Lotus::Model.configure`.

Since:

  • 0.2.0

Constant Summary collapse

DEFAULT_MIGRATIONS_PATH =

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

Default migrations path

See Also:

Since:

  • 0.4.0

Pathname.new('db/migrations').freeze
DEFAULT_SCHEMA_PATH =

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

Default schema path

See Also:

Since:

  • 0.4.0

Pathname.new('db/schema.sql').freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLotus::Model::Configuration

Initialize a configuration instance

Since:

  • 0.2.0



49
50
51
# File 'lib/lotus/model/configuration.rb', line 49

def initialize
  reset!
end

Instance Attribute Details

#adapter_configLotus::Model::Config::Adapter (readonly)

An adapter configuration template

Returns:

Since:

  • 0.2.0



41
42
43
# File 'lib/lotus/model/configuration.rb', line 41

def adapter_config
  @adapter_config
end

#mapperLotus::Model::Mapper (readonly)

The persistence mapper

Returns:

Since:

  • 0.2.0



34
35
36
# File 'lib/lotus/model/configuration.rb', line 34

def mapper
  @mapper
end

Instance Method Details

#adapterLotus::Model::Config::Adapter, NilClass #adapterObject

Register adapter

There could only 1 adapter can be registered per application

Examples:

Register the adapter

require 'lotus/model'

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

Lotus::Model.configuration.adapter_config

Overloads:

  • #adapterLotus::Model::Config::Adapter, NilClass

    Retrieves the configured adapter

    Returns:

  • #adapterObject

    Register the adapter

    @param @options [Hash] A set of options to register an adapter
    @option options [Symbol] :type The adapter type. Eg. :sql, :memory
      (mandatory)
    @option options [String] :uri The database uri string (mandatory)
    

Returns:

  • void

Raises:

  • (ArgumentError)

    if one of the mandatory options is omitted

See Also:

Since:

  • 0.2.0



114
115
116
117
118
119
120
121
# File 'lib/lotus/model/configuration.rb', line 114

def adapter(options = nil)
  if options.nil?
    @adapter_config
  else
    _check_adapter_options!(options)
    @adapter_config ||= Lotus::Model::Config::Adapter.new(options)
  end
end

#duplicateLotus::Model::Configuration

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 by copying the settings in a new instance.

Returns:

Since:

  • 0.2.0



234
235
236
237
238
239
# File 'lib/lotus/model/configuration.rb', line 234

def duplicate
  Configuration.new.tap do |c|
    c.instance_variable_set(:@adapter_config, @adapter_config)
    c.instance_variable_set(:@mapper, @mapper)
  end
end

#load!Object

Load the configuration for the current framework

Returns:

  • void

Since:

  • 0.2.0



74
75
76
77
78
79
# File 'lib/lotus/model/configuration.rb', line 74

def load!
  _build_mapper
  _build_adapter

  mapper.load!(@adapter)
end

#mapping(blk) ⇒ Object #mapping(path) ⇒ Object

Set global persistence mapping

Examples:

Set global persistence mapper

require 'lotus/model'

Lotus::Model.configure do
  mapping do
    collection :users do
      entity User

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

Overloads:

  • #mapping(blk) ⇒ Object

    Specify a set of mapping in the given block

    Parameters:

    • blk (Proc)

      the mapping definitions

  • #mapping(path) ⇒ Object

    Specify a relative path where to find the mapping file

    Parameters:

    • path (String)

      the relative path

Returns:

  • void

See Also:

Since:

  • 0.2.0



153
154
155
# File 'lib/lotus/model/configuration.rb', line 153

def mapping(path=nil, &blk)
  @mapper_config = Lotus::Model::Config::Mapper.new(path, &blk)
end

#migrationsPathname #migrations(path) ⇒ Object

Migrations directory

It defaults to db/migrations.

Examples:

Set Custom Path

require 'lotus/model'

Lotus::Model.configure do
  # ...
  migrations 'path/to/migrations'
end

Overloads:

  • #migrationsPathname

    Get migrations directory

    Returns:

    • (Pathname)

      migrations directory

  • #migrations(path) ⇒ Object

    Set migrations directory

    Parameters:

    • path (String, Pathname)

      the path

    Raises:

    • (Errno::ENOENT)

      if the given path doesn’t exist

See Also:

  • Migrations::DEFAULT_MIGRATIONS_PATH

Since:

  • 0.4.0



181
182
183
184
185
186
187
# File 'lib/lotus/model/configuration.rb', line 181

def migrations(path = nil)
  if path.nil?
    @migrations
  else
    @migrations = root.join(path).realpath
  end
end

#reset!Object Also known as: unload!

Reset all the values to the defaults

Returns:

  • void

Since:

  • 0.2.0



58
59
60
61
62
63
64
65
# File 'lib/lotus/model/configuration.rb', line 58

def reset!
  @adapter = nil
  @adapter_config = nil
  @mapper = NullMapper.new
  @mapper_config = nil
  @migrations = DEFAULT_MIGRATIONS_PATH
  @schema = DEFAULT_SCHEMA_PATH
end

#rootObject

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.

Root directory

Since:

  • 0.4.0



224
225
226
# File 'lib/lotus/model/configuration.rb', line 224

def root
  Lotus.respond_to?(:root) ? Lotus.root : Pathname.pwd
end

#schemaPathname #schema(path) ⇒ Object

Schema

It defaults to db/schema.sql.

Examples:

Set Custom Path

require 'lotus/model'

Lotus::Model.configure do
  # ...
  schema 'path/to/schema.sql'
end

Overloads:

  • #schemaPathname

    Get schema path

    Returns:

    • (Pathname)

      schema path

  • #schema(path) ⇒ Object

    Set schema path

    Parameters:

    • path (String, Pathname)

      the path

See Also:

  • Migrations::DEFAULT_SCHEMA_PATH

Since:

  • 0.4.0



212
213
214
215
216
217
218
# File 'lib/lotus/model/configuration.rb', line 212

def schema(path = nil)
  if path.nil?
    @schema
  else
    @schema = root.join(path)
  end
end