Class: Hanami::Model::Associations::ManyToMany Private
- Inherits:
-
Object
- Object
- Hanami::Model::Associations::ManyToMany
- Defined in:
- lib/hanami/model/associations/many_to_many.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Many-To-Many association
Instance Attribute Summary collapse
- #repository ⇒ Object readonly private
- #scope ⇒ Object readonly private
- #source ⇒ Object readonly private
- #subject ⇒ Object readonly private
- #target ⇒ Object readonly private
- #through ⇒ Object readonly private
Class Method Summary collapse
- .schema_type(entity) ⇒ Object private
Instance Method Summary collapse
-
#add(*data) ⇒ Object
private
Return the association table object.
- #count ⇒ Object private
- #delete ⇒ Object private
- #each(&blk) ⇒ Object private
-
#initialize(repository, source, target, subject, scope = nil) ⇒ ManyToMany
constructor
private
A new instance of ManyToMany.
- #map(&blk) ⇒ Object private
-
#remove(target_id) ⇒ Object
private
rubocop:disable Metrics/AbcSize.
- #to_a ⇒ Object private
- #where(condition) ⇒ Object private
Constructor Details
#initialize(repository, source, target, subject, scope = nil) ⇒ ManyToMany
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.
Returns a new instance of ManyToMany.
42 43 44 45 46 47 48 49 50 |
# File 'lib/hanami/model/associations/many_to_many.rb', line 42 def initialize(repository, source, target, subject, scope = nil) @repository = repository @source = source @target = target @subject = subject.to_hash unless subject.nil? @through = relation(source).associations[target].through.to_sym @scope = scope || _build_scope freeze end |
Instance Attribute Details
#repository ⇒ Object (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.
20 21 22 |
# File 'lib/hanami/model/associations/many_to_many.rb', line 20 def repository @repository end |
#scope ⇒ Object (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.
36 37 38 |
# File 'lib/hanami/model/associations/many_to_many.rb', line 36 def scope @scope end |
#source ⇒ Object (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.
24 25 26 |
# File 'lib/hanami/model/associations/many_to_many.rb', line 24 def source @source end |
#subject ⇒ Object (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.
32 33 34 |
# File 'lib/hanami/model/associations/many_to_many.rb', line 32 def subject @subject end |
#target ⇒ Object (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.
28 29 30 |
# File 'lib/hanami/model/associations/many_to_many.rb', line 28 def target @target end |
#through ⇒ Object (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.
40 41 42 |
# File 'lib/hanami/model/associations/many_to_many.rb', line 40 def through @through end |
Class Method Details
.schema_type(entity) ⇒ Object
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.
13 14 15 16 |
# File 'lib/hanami/model/associations/many_to_many.rb', line 13 def self.schema_type(entity) type = Sql::Types::Schema::AssociationType.new(entity) Types::Strict::Array.member(type) end |
Instance Method Details
#add(*data) ⇒ Object
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.
Return the association table object. Would need an aditional query to return the entity
76 77 78 79 80 81 |
# File 'lib/hanami/model/associations/many_to_many.rb', line 76 def add(*data) command(:create, relation(through), use: [:timestamps]) .call(associate(serialize(data))) rescue => e raise Hanami::Model::Error.for(e) end |
#count ⇒ Object
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.
64 65 66 |
# File 'lib/hanami/model/associations/many_to_many.rb', line 64 def count scope.count end |
#delete ⇒ Object
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.
85 86 87 |
# File 'lib/hanami/model/associations/many_to_many.rb', line 85 def delete relation(through).where(source_foreign_key => subject.fetch(source_primary_key)).delete end |
#each(&blk) ⇒ Object
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.
60 61 62 |
# File 'lib/hanami/model/associations/many_to_many.rb', line 60 def each(&blk) scope.each(&blk) end |
#map(&blk) ⇒ Object
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.
56 57 58 |
# File 'lib/hanami/model/associations/many_to_many.rb', line 56 def map(&blk) to_a.map(&blk) end |
#remove(target_id) ⇒ Object
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.
rubocop:disable Metrics/AbcSize
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/hanami/model/associations/many_to_many.rb', line 92 def remove(target_id) association_record = relation(through) .where(target_foreign_key => target_id, source_foreign_key => subject.fetch(source_primary_key)) .one return if association_record.nil? ar_id = association_record.public_send relation(through).primary_key command(:delete, relation(through)).by_pk(ar_id).call end |
#to_a ⇒ Object
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.
52 53 54 |
# File 'lib/hanami/model/associations/many_to_many.rb', line 52 def to_a scope.to_a end |
#where(condition) ⇒ Object
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.
68 69 70 |
# File 'lib/hanami/model/associations/many_to_many.rb', line 68 def where(condition) __new__(scope.where(condition)) end |