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
-
#attributes(*attributes) ⇒ Object
Maps the given attributes to and from the domain object and the DB.
-
#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.
- #build_class_config(domain_class, options, &block) ⇒ Object
-
#define(options = {}, &block) ⇒ Engine
Configures and creates a Engine instance.
-
#has_many(name, options = {}) ⇒ Object
Defines a one-to-many association to another type where the foreign key is stored on the associated table.
-
#has_one(name, options = {}) ⇒ Object
Defines a one-to-one association to another type where the foreign key is stored on the associated table.
-
#map(domain_class, options = {}, &block) ⇒ Object
Maps a domain class to a relational table.
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.
149 150 151 |
# File 'lib/vorpal/dsl/configuration.rb', line 149 def belongs_to(name, ={}) @builder.belongs_to(name, ) 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, , &block) @builder = ConfigBuilder.new(domain_class, , 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.
39 40 41 42 43 44 45 46 47 |
# File 'lib/vorpal/dsl/configuration.rb', line 39 def define(={}, &block) @main_config = Config::MainConfig.new instance_exec(&block) @main_config.initialize_association_configs db_driver = .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.
105 106 107 |
# File 'lib/vorpal/dsl/configuration.rb', line 105 def has_many(name, ={}) @builder.has_many(name, ) 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.
125 126 127 |
# File 'lib/vorpal/dsl/configuration.rb', line 125 def has_one(name, ={}) @builder.has_one(name, ) 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.
69 70 71 72 73 |
# File 'lib/vorpal/dsl/configuration.rb', line 69 def map(domain_class, ={}, &block) class_config = build_class_config(domain_class, , &block) @main_config.add_class_config(class_config) class_config end |