Sha256: 96a0de22796f691924d4ed25fc17046152a430f43f6029dfd22ba4febc9800a5

Contents?: true

Size: 1.96 KB

Versions: 9

Compression:

Stored size: 1.96 KB

Contents

module ActiveData
  module Model
    module Associations
      class ReferencesOne < ReferencesAny
        def build(attributes = {})
          replace(build_object(attributes))
        end

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

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

        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

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

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

        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

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

        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
        alias_method :writer, :replace

        def identify
          reflection.persistence_adapter.identify(target)
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
active_data-1.2.0 lib/active_data/model/associations/references_one.rb
active_data-1.1.7 lib/active_data/model/associations/references_one.rb
active_data-1.1.6 lib/active_data/model/associations/references_one.rb
active_data-1.1.5 lib/active_data/model/associations/references_one.rb
active_data-1.1.4 lib/active_data/model/associations/references_one.rb
active_data-1.1.3 lib/active_data/model/associations/references_one.rb
active_data-1.1.2 lib/active_data/model/associations/references_one.rb
active_data-1.1.1 lib/active_data/model/associations/references_one.rb
active_data-1.1.0 lib/active_data/model/associations/references_one.rb