Sha256: 3958360fa8499a368e7fdc7c657c7417d0c0cc788c97163e33190bf7dbf865b7

Contents?: true

Size: 1.67 KB

Versions: 9

Compression:

Stored size: 1.67 KB

Contents

module ActiveData
  module Model
    module Associations
      module Reflections
        class ReferencesAny < Base
          def self.build(_target, generated_methods, name, *args)
            reflection = new(name, *args)
            generate_methods name, generated_methods
            reflection
          end

          def self.persistence_adapter(klass)
            adapter = klass.active_data_persistence_adapter if klass.respond_to?(:active_data_persistence_adapter)
            adapter or raise PersistenceAdapterMissing, klass
          end

          delegate :primary_key, to: :persistence_adapter

          def initialize(name, *args)
            @options = args.extract_options!
            @scope_proc = args.first
            @name = name.to_sym
          end

          def klass
            @klass ||= if options[:data_source].present?
              options[:data_source].to_s.constantize
            else
              super
            end
          end
          alias_method :data_source, :klass

          def persistence_adapter
            @persistence_adapter ||= self.class.persistence_adapter(klass)
              .new(data_source, options[:primary_key], @scope_proc)
          end

          def read_source(object)
            object.read_attribute(reference_key)
          end

          def write_source(object, value)
            object.write_attribute(reference_key, value)
          end

          def embedded?
            false
          end

          def inspect
            "#{self.class.name.demodulize}(#{persistence_adapter.data_type})"
          end

          def autosave?
            !!options[:autosave]
          end
        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/reflections/references_any.rb
active_data-1.1.7 lib/active_data/model/associations/reflections/references_any.rb
active_data-1.1.6 lib/active_data/model/associations/reflections/references_any.rb
active_data-1.1.5 lib/active_data/model/associations/reflections/references_any.rb
active_data-1.1.4 lib/active_data/model/associations/reflections/references_any.rb
active_data-1.1.3 lib/active_data/model/associations/reflections/references_any.rb
active_data-1.1.2 lib/active_data/model/associations/reflections/references_any.rb
active_data-1.1.1 lib/active_data/model/associations/reflections/references_any.rb
active_data-1.1.0 lib/active_data/model/associations/reflections/references_any.rb