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


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

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


19
20
21
# File 'lib/hanami/model/associations/has_one.rb', line 19

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


35
36
37
# File 'lib/hanami/model/associations/has_one.rb', line 35

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


23
24
25
# File 'lib/hanami/model/associations/has_one.rb', line 23

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


31
32
33
# File 'lib/hanami/model/associations/has_one.rb', line 31

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


27
28
29
# File 'lib/hanami/model/associations/has_one.rb', line 27

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


13
14
15
# File 'lib/hanami/model/associations/has_one.rb', line 13

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


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

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


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

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


75
76
77
# File 'lib/hanami/model/associations/has_one.rb', line 75

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


48
49
50
# File 'lib/hanami/model/associations/has_one.rb', line 48

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


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

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


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

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