Class: ActiveData::Model::Associations::Reflections::Base
- Inherits:
-
Object
- Object
- ActiveData::Model::Associations::Reflections::Base
show all
- Defined in:
- lib/active_data/model/associations/reflections/base.rb
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.
50
51
52
|
# File 'lib/active_data/model/associations/reflections/base.rb', line 50
def initialize name, options = {}
@name, @options = name.to_sym, options
end
|
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
9
10
11
|
# File 'lib/active_data/model/associations/reflections/base.rb', line 9
def name
@name
end
|
#options ⇒ Object
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_reflection ⇒ Object
11
12
13
|
# File 'lib/active_data/model/associations/reflections/base.rb', line 11
def parent_reflection
@parent_reflection
end
|
Class Method Details
.association_class ⇒ Object
46
47
48
|
# File 'lib/active_data/model/associations/reflections/base.rb', line 46
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
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/active_data/model/associations/reflections/base.rb', line 14
def self.build target, generated_methods, name, options = {}, &block
if block
options[:class] = proc do |reflection|
superclass = reflection.options[:class_name].to_s.presence.try(:constantize)
klass = Class.new(superclass || Object) do
include ActiveData::Model
include ActiveData::Model::Associations
include ActiveData::Model::Lifecycle
include ActiveData::Model::Primary
end
target.const_set(name.to_s.classify, klass)
klass.class_eval(&block)
klass
end
end
generate_methods name, generated_methods
target.validates_nested name if options.delete(:validate) && target.respond_to?(:validates_nested)
new(name, options)
end
|
.generate_methods(name, target) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/active_data/model/associations/reflections/base.rb', line 34
def self.generate_methods name, target
target.class_eval " def \#{name} force_reload = false\n association(:\#{name}).reader(force_reload)\n end\n\n def \#{name}= value\n association(:\#{name}).writer(value)\n end\n RUBY\nend\n", __FILE__, __LINE__ + 1
|
Instance Method Details
#belongs_to? ⇒ Boolean
64
65
66
|
# File 'lib/active_data/model/associations/reflections/base.rb', line 64
def belongs_to?
false
end
|
#build_association(object) ⇒ Object
68
69
70
|
# File 'lib/active_data/model/associations/reflections/base.rb', line 68
def build_association object
self.class.association_class.new object, self
end
|
#default(object) ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/active_data/model/associations/reflections/base.rb', line 80
def default object
defaultizer = options[:default]
if defaultizer.is_a?(Proc)
if defaultizer.arity != 0
defaultizer.call(object)
else
object.instance_exec(&defaultizer)
end
else
defaultizer
end
end
|
#inspect ⇒ Object
93
94
95
|
# File 'lib/active_data/model/associations/reflections/base.rb', line 93
def inspect
"#{self.class.name.demodulize}(#{klass})"
end
|
#klass ⇒ Object
58
59
60
61
62
|
# File 'lib/active_data/model/associations/reflections/base.rb', line 58
def klass
@klass ||= options[:class] ?
options[:class].call(self) :
(options[:class_name].presence || name.to_s.classify).to_s.constantize
end
|
#macro ⇒ Object
54
55
56
|
# File 'lib/active_data/model/associations/reflections/base.rb', line 54
def macro
self.class.name.demodulize.underscore.to_sym
end
|
#read_source(object) ⇒ Object
72
73
74
|
# File 'lib/active_data/model/associations/reflections/base.rb', line 72
def read_source object
(options[:read] || READ).call(self, object)
end
|
#write_source(object, value) ⇒ Object
76
77
78
|
# File 'lib/active_data/model/associations/reflections/base.rb', line 76
def write_source object, value
(options[:write] || WRITE).call(self, object, value)
end
|