-
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
630 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
module Lotus | ||
module Model | ||
module Associations | ||
# Entities Association ManyToOne relationship | ||
class ManyToOne | ||
# @attr_reader related collection (the other side of relationship) | ||
# | ||
# @since x.x.x | ||
attr_reader :collection | ||
|
||
# @attr_reader Repository responsible will roots this association | ||
# | ||
# @since x.x.x | ||
attr_accessor :repository | ||
|
||
# Collects initial data to build ManyToOne Association | ||
# | ||
# @since x.x.x | ||
# | ||
# @example | ||
# | ||
# collection: :articles, foreign_key: :article_id, repository: ArticlesRepository | ||
def initialize(opts) | ||
@name = opts.fetch(:name) | ||
@collection = opts.fetch(:collection) | ||
@foreign_key = opts.fetch(:foreign_key) {default_foreign_key} | ||
@repository = nil | ||
end | ||
|
||
# @since x.x.x | ||
# @api private | ||
def associate_entities!(entities) | ||
entities.map do |entity| | ||
entity.send("#{@name}=", @repository.find(entity.send(@foreign_key))) | ||
entity | ||
end | ||
end | ||
|
||
private | ||
# Default Foreign key's name | ||
# | ||
# It's possible to overwrite this through foreign_key attribute | ||
# inside mapping via association method. | ||
# | ||
# @api private | ||
# @since x.x.x | ||
def default_foreign_key | ||
"#{@name}_id" | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.