Class: Hanami::Model::Associations::HasOne Private

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

Since:

  • 1.1.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

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

Since:

  • 1.1.0



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

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:

  • 1.1.0



21
22
23
# File 'lib/hanami/model/associations/has_one.rb', line 21

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:

  • 1.1.0



37
38
39
# File 'lib/hanami/model/associations/has_one.rb', line 37

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:

  • 1.1.0



25
26
27
# File 'lib/hanami/model/associations/has_one.rb', line 25

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:

  • 1.1.0



33
34
35
# File 'lib/hanami/model/associations/has_one.rb', line 33

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:

  • 1.1.0



29
30
31
# File 'lib/hanami/model/associations/has_one.rb', line 29

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:

  • 1.1.0



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

def self.schema_type(entity)
  Sql::Types::Schema::AssociationType.new(entity)
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:

  • 1.1.0



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

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

  • 1.1.0



54
55
56
57
58
59
60
# File 'lib/hanami/model/associations/has_one.rb', line 54

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

#deleteObject Also known as: remove

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:

  • 1.1.0



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

def delete
  scope.delete
end

#oneObject

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:

  • 1.1.0



50
51
52
# File 'lib/hanami/model/associations/has_one.rb', line 50

def one
  scope.one
end

#replace(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:

  • 1.1.0



82
83
84
85
86
87
# File 'lib/hanami/model/associations/has_one.rb', line 82

def replace(data)
  repository.transaction do
    delete
    add(serialize(data))
  end
end

#update(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:

  • 1.1.0



68
69
70
71
72
73
74
75
# File 'lib/hanami/model/associations/has_one.rb', line 68

def update(data)
  command(:update, relation(target), mapper: nil)
    .by_pk(
      one.public_send(relation(target).primary_key)
    ).call(serialize(data))
rescue => exception
  raise Hanami::Model::Error.for(exception)
end