Class: Hanami::Model::Associations::HasMany Private

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/model/associations/has_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.

One-To-Many association

Since:

  • 0.7.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository, source, target, subject, scope = nil) ⇒ HasMany

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 HasMany.

Since:

  • 0.7.0



40
41
42
43
44
45
46
47
# File 'lib/hanami/model/associations/has_many.rb', line 40

def initialize(repository, source, target, subject, scope = nil)
  @repository = repository
  @source     = source
  @target     = target
  @subject    = subject.to_hash unless subject.nil?
  @scope      = scope || _build_scope
  freeze
end

Instance Attribute Details

#repositoryObject (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.

Since:

  • 0.7.0



20
21
22
# File 'lib/hanami/model/associations/has_many.rb', line 20

def repository
  @repository
end

#scopeObject (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.

Since:

  • 0.7.0



36
37
38
# File 'lib/hanami/model/associations/has_many.rb', line 36

def scope
  @scope
end

#sourceObject (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.

Since:

  • 0.7.0



24
25
26
# File 'lib/hanami/model/associations/has_many.rb', line 24

def source
  @source
end

#subjectObject (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.

Since:

  • 0.7.0



32
33
34
# File 'lib/hanami/model/associations/has_many.rb', line 32

def subject
  @subject
end

#targetObject (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.

Since:

  • 0.7.0



28
29
30
# File 'lib/hanami/model/associations/has_many.rb', line 28

def target
  @target
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.

Since:

  • 0.7.0



13
14
15
16
# File 'lib/hanami/model/associations/has_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.

Since:

  • 0.7.0



60
61
62
63
64
65
# File 'lib/hanami/model/associations/has_many.rb', line 60

def add(data)
  command(:create, relation(target), use: [:timestamps])
    .call(associate(serialize(data)))
rescue => e
  raise Hanami::Model::Error.for(e)
end

#countObject

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.

Since:

  • 0.7.0



107
108
109
# File 'lib/hanami/model/associations/has_many.rb', line 107

def count
  scope.count
end

#create(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.

Since:

  • 0.7.0



51
52
53
54
55
56
# File 'lib/hanami/model/associations/has_many.rb', line 51

def create(data)
  entity.new(command(:create, aggregate(target), mapper: nil, use: [:timestamps])
    .call(serialize(data)))
rescue => e
  raise Hanami::Model::Error.for(e)
end

#deleteObject

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.

Since:

  • 0.7.0



77
78
79
# File 'lib/hanami/model/associations/has_many.rb', line 77

def delete
  scope.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.

Since:

  • 0.7.0



83
84
85
# File 'lib/hanami/model/associations/has_many.rb', line 83

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.

Since:

  • 0.7.0



89
90
91
# File 'lib/hanami/model/associations/has_many.rb', line 89

def map(&blk)
  to_a.map(&blk)
end

#remove(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.

Since:

  • 0.7.0



69
70
71
72
73
# File 'lib/hanami/model/associations/has_many.rb', line 69

def remove(id)
  command(:update, relation(target), use: [:timestamps])
    .by_pk(id)
    .call(unassociate)
end

#to_aObject

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.

Since:

  • 0.7.0



95
96
97
# File 'lib/hanami/model/associations/has_many.rb', line 95

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.

Since:

  • 0.7.0



101
102
103
# File 'lib/hanami/model/associations/has_many.rb', line 101

def where(condition)
  __new__(scope.where(condition))
end