Class: ActiveData::Model::Associations::Reflections::Base
- Inherits:
-
Object
- Object
- ActiveData::Model::Associations::Reflections::Base
- Defined in:
- lib/active_data/model/associations/reflections/base.rb
Direct Known Subclasses
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
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parent_reflection ⇒ Object
AR compatibility.
Class Method Summary collapse
- .association_class ⇒ Object
- .build(target, generated_methods, name, options = {}, &_block) ⇒ Object
- .generate_methods(name, target) ⇒ Object
Instance Method Summary collapse
-
#belongs_to? ⇒ Boolean
AR compatibility.
- #build_association(object) ⇒ Object
- #collection? ⇒ Boolean
- #default(object) ⇒ Object
-
#initialize(name, options = {}) ⇒ Base
constructor
A new instance of Base.
- #klass ⇒ Object
- #macro ⇒ Object
- #read_source(object) ⇒ Object
- #write_source(object, value) ⇒ Object
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, = {}) @name = name.to_sym @options = end |
Instance Attribute Details
#name ⇒ Object (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 |
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/active_data/model/associations/reflections/base.rb', line 9 def @options end |
#parent_reflection ⇒ Object
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_class ⇒ Object
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, = {}, &_block) generate_methods name, generated_methods if .delete(:validate) && target.respond_to?(:validates_nested) && !target.validates_nested?(name) target.validates_nested name end new(name, ) 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
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
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 = [:default] if defaultizer.is_a?(Proc) if defaultizer.arity.nonzero? defaultizer.call(object) else object.instance_exec(&defaultizer) end else defaultizer end end |
#klass ⇒ Object
49 50 51 |
# File 'lib/active_data/model/associations/reflections/base.rb', line 49 def klass @klass ||= ([:class_name].presence || name.to_s.classify).to_s.constantize end |
#macro ⇒ Object
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) ([: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) ([:write] || WRITE).call(self, object, value) end |