Class: Vorpal::Dsl::ConfigBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/vorpal/dsl/config_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(clazz, options, db_driver) ⇒ ConfigBuilder

Returns a new instance of ConfigBuilder.



9
10
11
12
13
14
15
16
17
# File 'lib/vorpal/dsl/config_builder.rb', line 9

def initialize(clazz, options, db_driver)
  @domain_class = clazz
  @class_options = options
  @has_manys = []
  @has_ones = []
  @belongs_tos = []
  @attributes = []
  @defaults_generator = DefaultsGenerator.new(clazz, db_driver)
end

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.



21
22
23
# File 'lib/vorpal/dsl/config_builder.rb', line 21

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

#attributes_with_idObject



92
93
94
# File 'lib/vorpal/dsl/config_builder.rb', line 92

def attributes_with_id
  [:id].concat @attributes
end

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

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

This association can be polymorphic. I.E. children 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. They end that defines the association is called the ‘Parent’ and the end that is associated is called the ‘Child’.

Parameters:

  • name (String)

    Name of the association getter.

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

Options Hash (options):

  • :owned (Boolean) — default: True

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

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

    The name of the DB column on the parent that contains the foreign key reference to the child.

  • :fk_type (String)

    The name of the DB column on the parent that contains the child class name. Only needed when the association is polymorphic.

  • :child_class (Class) — default: name converted to a Class

    The child class.

  • :child_classes ([Class])

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



77
78
79
# File 'lib/vorpal/dsl/config_builder.rb', line 77

def belongs_to(name, options={})
  @belongs_tos << {name: name}.merge(options)
end

#buildObject



82
83
84
85
86
87
88
89
# File 'lib/vorpal/dsl/config_builder.rb', line 82

def build
  class_config = build_class_config
  class_config.has_manys = build_has_manys
  class_config.has_ones = build_has_ones
  class_config.belongs_tos = build_belongs_tos

  class_config
end

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

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

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. They end that defines the association is called the ‘Parent’ and the end that is associated is called the ‘Child’.

Parameters:

  • name (String)

    Name of the association getter.

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

Options Hash (options):

  • :owned (Boolean) — default: True

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

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

    The name of the DB column on the child that contains the foreign key reference to the parent.

  • :fk_type (String)

    The name of the DB column on the child that contains the parent class name. Only needed when there is an association from the child side that is polymorphic.

  • :child_class (Class) — default: name converted to a Class

    The child class.



38
39
40
# File 'lib/vorpal/dsl/config_builder.rb', line 38

def has_many(name, options={})
  @has_manys << {name: name}.merge(options)
end

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

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

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. They end that defines the association is called the ‘Parent’ and the end that is associated is called the ‘Child’.

Parameters:

  • name (String)

    Name of the association getter.

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

Options Hash (options):

  • :owned (Boolean) — default: True

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

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

    The name of the DB column on the child that contains the foreign key reference to the parent.

  • :fk_type (String)

    The name of the DB column on the child that contains the parent class name. Only needed when there is an association from the child side that is polymorphic.

  • :child_class (Class) — default: name converted to a Class

    The child class.



56
57
58
# File 'lib/vorpal/dsl/config_builder.rb', line 56

def has_one(name, options={})
  @has_ones << {name: name}.merge(options)
end