Module: Morpheus::Client::Associations::ClassMethods

Defined in:
lib/morpheus/client/associations.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to_resource(association, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/morpheus/client/associations.rb', line 11

def belongs_to_resource(association, options = {})
  define_method association do
    instance_variable_get("@#{association}") ||
      instance_variable_set("@#{association}", Morpheus::Mixins::Associations::BelongsToAssociation.new(self, association, :options => options))
  end

  define_method "#{association}=" do |target|
    instance_variable_set("@#{association}", Morpheus::Mixins::Associations::BelongsToAssociation.new(self, association, :target => target, :options => options))
    send("#{association}_id=", target.id)
  end

end

#has_many_resources(association, options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/morpheus/client/associations.rb', line 24

def has_many_resources(association, options = {})
  define_method association do
    instance_variable_get("@#{association}") ||
      instance_variable_set("@#{association}", Morpheus::Mixins::Associations::HasManyAssociation.new(self, association, :options => options))
  end

  define_method "#{association}=" do |target|
    instance_variable_set("@#{association}", Morpheus::Mixins::Associations::HasManyAssociation.new(self, association, :target => target, :options => options))
  end
end

#has_one_resource(association, options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/morpheus/client/associations.rb', line 35

def has_one_resource(association, options = {})
  define_method association do
    instance_variable_get("@#{association}") ||
      instance_variable_set("@#{association}", Morpheus::Mixins::Associations::HasOneAssociation.new(self, association, :options => options))
  end

  define_method "#{association}=" do |target|
    instance_variable_set("@#{association}", Morpheus::Mixins::Associations::HasOneAssociation.new(self, association, :target => target, :options => options))
  end
end