Class: Lotus::Model::Mapper

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

Overview

A persistence mapper that keeps entities independent from database details.

This is database independent. It can work with SQL, document, and even with key/value stores.

Examples:

require 'lotus/model'

mapper = Lotus::Model::Mapper.new do
  collection :users do
    entity User

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

# This guarantees thread-safety and should happen as last thing before
# to start the app code.
mapper.load!

See Also:

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(coercer = nil, &blk) ⇒ Lotus::Model::Mapper

Instantiate a mapper.

It accepts an optional argument (‘coercer`) a class that defines the policies for entities translations from/to the database.

If provided, this class must implement the following interface:

* #initialize(collection) # Lotus::Model::Mapping::Collection
* #to_record(entity)      # translates an entity to the database type
* #from_record(record)    # translates a record into an entity
* #deserialize_*(value)   # a set of methods, one for each database column.

If not given, it uses ‘Lotus::Model::Mapping::Coercer`, by default.

Parameters:

  • coercer (Class) (defaults to: nil)

    an optional class that defines the policies for entity translations from/to the database.

  • blk (Proc)

    an optional block of code that gets evaluated in the context of the current instance

Since:

  • 0.1.0



81
82
83
84
85
86
# File 'lib/lotus/model/mapper.rb', line 81

def initialize(coercer = nil, &blk)
  @coercer     = coercer || Mapping::Coercer
  @collections = {}

  instance_eval(&blk) if block_given?
end

Instance Attribute Details

#collectionsObject (readonly)

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.

Since:

  • 0.1.0



54
55
56
# File 'lib/lotus/model/mapper.rb', line 54

def collections
  @collections
end

Instance Method Details

#collection(name, &blk) ⇒ Object

Maps a collection.

A collection is a set of homogeneous records. Think of a table of a SQL database or about collection of MongoDB.

Parameters:

  • name (Symbol)

    the name of the mapped collection. If used with a SQL database it’s the table name.

  • blk (Proc)

    the block that maps the attributes of that collection.

See Also:

Since:

  • 0.1.0



101
102
103
104
105
106
107
# File 'lib/lotus/model/mapper.rb', line 101

def collection(name, &blk)
  if block_given?
    @collections[name] = Mapping::Collection.new(name, @coercer, &blk)
  else
    @collections[name] or raise Mapping::UnmappedCollectionError.new(name)
  end
end

#load!(adapter = nil) ⇒ Object

Loads the internals of the mapper, in order to guarantee thread safety.

This method MUST be invoked as the last thing before of start using the application.

Since:

  • 0.1.0



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

def load!(adapter = nil)
  @collections.each_value do |collection|
    collection.adapter = adapter
    collection.load!
  end
  self
end