Module: Vorpal::Dsl::Configuration

Included in:
Vorpal
Defined in:
lib/vorpal/dsl/configuration.rb

Overview

Implements the Vorpal DSL.

“‘ruby engine = Vorpal.define do

map Tree do
  attributes :name
  belongs_to :trunk
  has_many :branches
end

map Trunk do
  attributes :length
  has_one :tree
end

map Branch do
  attributes :length
  belongs_to :tree
end

end

mapper = engine.mapper_for(Tree) “‘

Instance Method Summary collapse

Instance Method Details

#attributes(*attributes) ⇒ Object

Maps the given attributes to and from the domain object and the DB. Not needed if a serializer and deserializer were provided.



86
87
88
# File 'lib/vorpal/dsl/configuration.rb', line 86

def attributes(*attributes)
  @builder.attributes(*attributes)
end

#belongs_to(name, options = {}) ⇒ Object

Defines a one-to-one association with another type where the foreign key is stored on the table of the entity declaring the association.

This association can be polymorphic. I.E. associates can be of different types.

In Object-Oriented programming, associations are directed. This means that they can only be traversed in one direction: from the type that defines the association (the one with the getter) to the type that is associated.

Parameters:

  • name (String)

    Name of the association getter.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :owned (Boolean) — default: True

    True if the associated type belongs to the aggregate. Changes to any object belonging to the aggregate will be persisted when the aggregate is persisted.

  • :fk (String) — default: Associated class name converted to snakecase and appended with a '_id'

    The name of the DB column on the association-owning table that contains the foreign key reference to the associated table.

  • :fk_type (String)

    The name of the DB column on the association-owning table that contains the associated class name. Only needed when the association is polymorphic.

  • :unique_key_name (String) — default: "id"

    The name of the column on the associated table that the foreign key points to. Normally the primary key column.

  • :primary_key (String)

    Same as :unique_key_name. Exists for compatibility with Rails API.

  • :child_class (Class)

    DEPRECATED. Use ‘associated_class` instead. The associated class.

  • :associated_class (Class) — default: Name of the association converted to a Class

    The associated class.

  • :child_classes ([Class])

    DEPRECATED. Use ‘associated_classes` instead. The list of possible classes that can be associated. This is for polymorphic associations. Takes precedence over `:associated_class`.

  • :associated_classes ([Class]) — default: Name of the association converted to a Class

    The list of possible classes that can be associated. This is for polymorphic associations. Takes precedence over ‘:associated_class`.



149
150
151
# File 'lib/vorpal/dsl/configuration.rb', line 149

def belongs_to(name, options={})
  @builder.belongs_to(name, options)
end

#build_class_config(domain_class, options, &block) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/vorpal/dsl/configuration.rb', line 76

def build_class_config(domain_class, options, &block)
  @builder = ConfigBuilder.new(domain_class, options, Driver::Postgresql.new)
  instance_exec(&block) if block_given?
  class_config = @builder.build
  @builder = nil # make sure this ConfigBuilder is never re-used by accident.
  class_config
end

#define(options = {}, &block) ⇒ Engine

Configures and creates a Engine instance.

Parameters:

  • options (Hash) (defaults to: {})

    Global configuration options for the engine instance.

Options Hash (options):

  • :db_driver (Object) — default: Object that will be used to interact with the DB.

    Must be duck-type compatible with Postgresql.

Returns:

  • (Engine)

    Instance of the mapping engine.



39
40
41
42
43
44
45
46
47
# File 'lib/vorpal/dsl/configuration.rb', line 39

def define(options={}, &block)
  @main_config = Config::MainConfig.new
  instance_exec(&block)
  @main_config.initialize_association_configs
  db_driver = options.fetch(:db_driver, Driver::Postgresql.new)
  engine = Engine.new(db_driver, @main_config)
  @main_config = nil # make sure this MainConfig is never re-used by accident.
  engine
end

#has_many(name, options = {}) ⇒ Object

Defines a one-to-many association to another type where the foreign key is stored on the associated table.

In Object-Oriented programming, associations are directed. This means that they can only be traversed in one direction: from the type that defines the association (the one with the getter) to the type that is associated.

Parameters:

  • name (String)

    Name of the association getter.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :owned (Boolean) — default: True

    True if the associated type belongs to the aggregate. Changes to any object belonging to the aggregate will be persisted when the aggregate is persisted.

  • :fk (String) — default: Association-owning class name converted to snakecase and appended with a '_id'

    The name of the DB column on the associated table that contains the foreign key reference to the association owner.

  • :fk_type (String)

    The name of the DB column on the associated table that contains the association-owning class name. Only needed when the associated end is polymorphic.

  • :unique_key_name (String) — default: "id"

    The name of the column on the owning table that the foreign key points to. Normally the primary key column.

  • :primary_key (String)

    Same as :unique_key_name. Exists for compatibility with Rails API.

  • :child_class (Class)

    DEPRECATED. Use ‘associated_class` instead. The associated class.

  • :associated_class (Class) — default: Name of the association converted to a Class

    The associated class.



105
106
107
# File 'lib/vorpal/dsl/configuration.rb', line 105

def has_many(name, options={})
  @builder.has_many(name, options)
end

#has_one(name, options = {}) ⇒ Object

Defines a one-to-one association to another type where the foreign key is stored on the associated table.

In Object-Oriented programming, associations are directed. This means that they can only be traversed in one direction: from the type that defines the association (the one with the getter) to the type that is associated.

Parameters:

  • name (String)

    Name of the association getter.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :owned (Boolean) — default: True

    True if the associated type belongs to the aggregate. Changes to any object belonging to the aggregate will be persisted when the aggregate is persisted.

  • :fk (String) — default: Association-owning class name converted to snakecase and appended with a '_id'

    The name of the DB column on the associated table that contains the foreign key reference to the association owner.

  • :fk_type (String)

    The name of the DB column on the associated table that contains the association-owning class name. Only needed when the associated end is polymorphic.

  • :unique_key_name (String) — default: "id"

    The name of the column on the owning table that the foreign key points to. Normally the primary key column.

  • :primary_key (String)

    Same as :unique_key_name. Exists for compatibility with Rails API.

  • :child_class (Class)

    DEPRECATED. Use ‘associated_class` instead. The associated class.

  • :associated_class (Class) — default: Name of the association converted to a Class

    The associated class.



125
126
127
# File 'lib/vorpal/dsl/configuration.rb', line 125

def has_one(name, options={})
  @builder.has_one(name, options)
end

#map(domain_class, options = {}, &block) ⇒ Object

Maps a domain class to a relational table.

@option options [Symbol] :primary_key_type [:serial, :uuid] (:serial)
  The type of primary key for the class. :serial for auto-incrementing integer, :uuid for a UUID
@option options [Symbol] :id
  Same as :primary_key_type. Exists for compatibility with the Rails API.

Parameters:

  • domain_class (Class)

    Type of the domain model to be mapped

  • options (Hash) (defaults to: {})

    Configure how to map the domain model

Options Hash (options):

  • :to (String)

    Class of the ActiveRecord object that will map this domain class to the DB. Optional, if one is not specified, it will be generated.

  • :serializer (Object) — default: map the {ConfigBuilder#attributes} directly

    Object that will convert the domain objects into a hash.

    Must have a ‘(Hash) serialize(Object)` method.

  • :deserializer (Object) — default: map the {ConfigBuilder#attributes} directly

    Object that will set a hash of attribute_names->values onto a new domain object.

    Must have a ‘(Object) deserialize(Object, Hash)` method.



69
70
71
72
73
# File 'lib/vorpal/dsl/configuration.rb', line 69

def map(domain_class, options={}, &block)
  class_config = build_class_config(domain_class, options, &block)
  @main_config.add_class_config(class_config)
  class_config
end