Class: ActiveData::Model::Associations::ReferencesOne

Inherits:
ReferencesAny show all
Defined in:
lib/active_data/model/associations/references_one.rb

Instance Attribute Summary

Attributes inherited from Base

#owner, #reflection

Instance Method Summary collapse

Methods inherited from ReferencesAny

#scope

Methods inherited from Base

#apply_changes!, #callback, #evar_loaded?, #initialize, #inspect, #loaded!, #loaded?, #reload, #reset, #target, #transaction

Constructor Details

This class inherits a constructor from ActiveData::Model::Associations::Base

Instance Method Details

#apply_changesObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/active_data/model/associations/references_one.rb', line 19

def apply_changes
  if target
    if target.marked_for_destruction? && reflection.autosave?
      target.destroy
    elsif target.new_record? || (reflection.autosave? && target.changed?)
      persist_object(target)
    else
      true
    end
  else
    true
  end
end

#build(attributes = {}) ⇒ Object



5
6
7
# File 'lib/active_data/model/associations/references_one.rb', line 5

def build(attributes = {})
  replace(build_object(attributes))
end

#create(attributes = {}) ⇒ Object



9
10
11
12
# File 'lib/active_data/model/associations/references_one.rb', line 9

def create(attributes = {})
  persist_object(build(attributes))
  target
end

#create!(attributes = {}) ⇒ Object



14
15
16
17
# File 'lib/active_data/model/associations/references_one.rb', line 14

def create!(attributes = {})
  persist_object(build(attributes), raise_error: true)
  target
end

#defaultObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/active_data/model/associations/references_one.rb', line 43

def default
  return if evar_loaded?

  default = reflection.default(owner)

  return unless default

  case default
  when reflection.persistence_adapter.data_type
    default
  when Hash
    build_object(default)
  else
    reflection.persistence_adapter.find_one(owner, default)
  end
end

#identifyObject



79
80
81
# File 'lib/active_data/model/associations/references_one.rb', line 79

def identify
  reflection.persistence_adapter.identify(target)
end

#load_targetObject



38
39
40
41
# File 'lib/active_data/model/associations/references_one.rb', line 38

def load_target
  source = read_source
  source ? reflection.persistence_adapter.find_one(owner, source) : default
end

#reader(force_reload = false) ⇒ Object



60
61
62
63
# File 'lib/active_data/model/associations/references_one.rb', line 60

def reader(force_reload = false)
  reset if force_reload
  target
end

#replace(object) ⇒ Object Also known as: writer



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/active_data/model/associations/references_one.rb', line 65

def replace(object)
  raise_type_mismatch(object) unless object.nil? || matches_type?(object)

  transaction do
    attribute.pollute do
      self.target = object
      write_source identify
    end
  end

  target
end

#target=(object) ⇒ Object



33
34
35
36
# File 'lib/active_data/model/associations/references_one.rb', line 33

def target=(object)
  loaded!
  @target = object
end