Class: NaranyaEcm::Rest::Associations::HasOne

Inherits:
SingularAssociation show all
Defined in:
lib/naranya_ecm/rest/associations.rb

Instance Attribute Summary

Attributes inherited from Association

#associated_class, #klass, #name, #options

Instance Method Summary collapse

Methods inherited from Association

#resolve_class_name

Constructor Details

#initialize(klass, name, options = {}) ⇒ HasOne

Returns a new instance of HasOne.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/naranya_ecm/rest/associations.rb', line 71

def initialize(klass, name, options = {})
  super
  # Set the Getter:
  klass.send :define_method, name do
    value = self.instance_variable_get "@#{name}".to_sym
    if value.nil?
      reflection = self.class.reflect_on_association name
      foreign_key = (self.class.name.demodulize.underscore + '_id').to_sym
      value = self.instance_variable_set(
        "@#{name}".to_sym,
        reflection.associated_class.where(foreign_key => self.id).first
      )
    end
    value
  end
end

Instance Method Details

#macroObject



87
# File 'lib/naranya_ecm/rest/associations.rb', line 87

def macro; :has_one; end