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



42
43
44
45
46
47
48
49
# File 'lib/hanami/model/associations/has_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?
  @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



22
23
24
# File 'lib/hanami/model/associations/has_many.rb', line 22

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



38
39
40
# File 'lib/hanami/model/associations/has_many.rb', line 38

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



26
27
28
# File 'lib/hanami/model/associations/has_many.rb', line 26

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



34
35
36
# File 'lib/hanami/model/associations/has_many.rb', line 34

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



30
31
32
# File 'lib/hanami/model/associations/has_many.rb', line 30

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



15
16
17
18
# File 'lib/hanami/model/associations/has_many.rb', line 15

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



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

def add(data)
  command(:create, relation(target), use: [:timestamps])
    .call(associate(serialize(data)))
rescue => exception
  raise Hanami::Model::Error.for(exception)
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



109
110
111
# File 'lib/hanami/model/associations/has_many.rb', line 109

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



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

def create(data)
  entity.new(command(:create, aggregate(target), mapper: nil, use: [:timestamps])
    .call(serialize(data)))
rescue => exception
  raise Hanami::Model::Error.for(exception)
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



79
80
81
# File 'lib/hanami/model/associations/has_many.rb', line 79

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



85
86
87
# File 'lib/hanami/model/associations/has_many.rb', line 85

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



91
92
93
# File 'lib/hanami/model/associations/has_many.rb', line 91

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



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

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



97
98
99
# File 'lib/hanami/model/associations/has_many.rb', line 97

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



103
104
105
# File 'lib/hanami/model/associations/has_many.rb', line 103

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