Class: ActiveData::Model::Associations::Reflections::Base

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

Direct Known Subclasses

EmbedsAny, ReferencesAny

Constant Summary collapse

READ =
->(reflection, object) { object.read_attribute reflection.name }
WRITE =
->(reflection, object, value) { object.write_attribute reflection.name, value }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Base

Returns a new instance of Base.



40
41
42
43
# File 'lib/active_data/model/associations/reflections/base.rb', line 40

def initialize(name, options = {})
  @name = name.to_sym
  @options = options
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/active_data/model/associations/reflections/base.rb', line 9

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/active_data/model/associations/reflections/base.rb', line 9

def options
  @options
end

#parent_reflectionObject

AR compatibility



11
12
13
# File 'lib/active_data/model/associations/reflections/base.rb', line 11

def parent_reflection
  @parent_reflection
end

Class Method Details

.association_classObject



36
37
38
# File 'lib/active_data/model/associations/reflections/base.rb', line 36

def self.association_class
  @association_class ||= "ActiveData::Model::Associations::#{name.demodulize}".constantize
end

.build(target, generated_methods, name, options = {}, &_block) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/active_data/model/associations/reflections/base.rb', line 14

def self.build(target, generated_methods, name, options = {}, &_block)
  generate_methods name, generated_methods
  if options.delete(:validate) &&
      target.respond_to?(:validates_nested) &&
      !target.validates_nested?(name)
    target.validates_nested name
  end
  new(name, options)
end

.generate_methods(name, target) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/active_data/model/associations/reflections/base.rb', line 24

def self.generate_methods(name, target)
  target.class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{name} force_reload = false
      association(:#{name}).reader(force_reload)
    end

    def #{name}= value
      association(:#{name}).writer(value)
    end
  RUBY
end

Instance Method Details

#belongs_to?Boolean

AR compatibility

Returns:



54
55
56
# File 'lib/active_data/model/associations/reflections/base.rb', line 54

def belongs_to?
  false
end

#build_association(object) ⇒ Object



58
59
60
# File 'lib/active_data/model/associations/reflections/base.rb', line 58

def build_association(object)
  self.class.association_class.new object, self
end

#collection?Boolean

Returns:



83
84
85
# File 'lib/active_data/model/associations/reflections/base.rb', line 83

def collection?
  true
end

#default(object) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/active_data/model/associations/reflections/base.rb', line 70

def default(object)
  defaultizer = options[:default]
  if defaultizer.is_a?(Proc)
    if defaultizer.arity.nonzero?
      defaultizer.call(object)
    else
      object.instance_exec(&defaultizer)
    end
  else
    defaultizer
  end
end

#klassObject



49
50
51
# File 'lib/active_data/model/associations/reflections/base.rb', line 49

def klass
  @klass ||= (options[:class_name].presence || name.to_s.classify).to_s.constantize
end

#macroObject



45
46
47
# File 'lib/active_data/model/associations/reflections/base.rb', line 45

def macro
  self.class.name.demodulize.underscore.to_sym
end

#read_source(object) ⇒ Object



62
63
64
# File 'lib/active_data/model/associations/reflections/base.rb', line 62

def read_source(object)
  (options[:read] || READ).call(self, object)
end

#write_source(object, value) ⇒ Object



66
67
68
# File 'lib/active_data/model/associations/reflections/base.rb', line 66

def write_source(object, value)
  (options[:write] || WRITE).call(self, object, value)
end