Class: ActiveData::Model::Associations::Reflections::ReferencesAny

Inherits:
Base
  • Object
show all
Defined in:
lib/active_data/model/associations/reflections/references_any.rb

Direct Known Subclasses

ReferencesMany, ReferencesOne

Constant Summary

Constants inherited from Base

Base::READ, Base::WRITE

Instance Attribute Summary

Attributes inherited from Base

#name, #options, #parent_reflection

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

association_class, #belongs_to?, #build_association, #collection?, #default, generate_methods, #macro

Constructor Details

#initialize(name, *args) ⇒ ReferencesAny

Returns a new instance of ReferencesAny.



19
20
21
22
23
# File 'lib/active_data/model/associations/reflections/references_any.rb', line 19

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

Class Method Details

.build(_target, generated_methods, name, *args) ⇒ Object



6
7
8
9
10
# File 'lib/active_data/model/associations/reflections/references_any.rb', line 6

def self.build(_target, generated_methods, name, *args)
  reflection = new(name, *args)
  generate_methods name, generated_methods
  reflection
end

.persistence_adapter(klass) ⇒ Object



12
13
14
15
# File 'lib/active_data/model/associations/reflections/references_any.rb', line 12

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

Instance Method Details

#autosave?Boolean

Returns:



55
56
57
# File 'lib/active_data/model/associations/reflections/references_any.rb', line 55

def autosave?
  !!options[:autosave]
end

#embedded?Boolean

Returns:



47
48
49
# File 'lib/active_data/model/associations/reflections/references_any.rb', line 47

def embedded?
  false
end

#inspectObject



51
52
53
# File 'lib/active_data/model/associations/reflections/references_any.rb', line 51

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

#klassObject Also known as: data_source



25
26
27
28
29
30
31
# File 'lib/active_data/model/associations/reflections/references_any.rb', line 25

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

#persistence_adapterObject



34
35
36
37
# File 'lib/active_data/model/associations/reflections/references_any.rb', line 34

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

#read_source(object) ⇒ Object



39
40
41
# File 'lib/active_data/model/associations/reflections/references_any.rb', line 39

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

#write_source(object, value) ⇒ Object



43
44
45
# File 'lib/active_data/model/associations/reflections/references_any.rb', line 43

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