Class: Spyke::Associations::Association

Inherits:
Relation
  • Object
show all
Defined in:
lib/spyke/associations/association.rb

Direct Known Subclasses

BelongsTo, HasMany, HasOne

Instance Attribute Summary collapse

Attributes inherited from Relation

#klass, #params

Instance Method Summary collapse

Methods inherited from Relation

#each, #find, #find_one, #find_some, #uri, #where, #with, #with_fallback

Constructor Details

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

Returns a new instance of Association.



9
10
11
12
# File 'lib/spyke/associations/association.rb', line 9

def initialize(klass, parent, name, options = {})
  super(klass, options)
  @parent, @name = parent, name
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Spyke::Relation

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/spyke/associations/association.rb', line 7

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



7
8
9
# File 'lib/spyke/associations/association.rb', line 7

def parent
  @parent
end

Instance Method Details

#assign_nested_attributes(attributes) ⇒ Object



18
19
20
# File 'lib/spyke/associations/association.rb', line 18

def assign_nested_attributes(attributes)
  update_parent new(attributes)
end

#create(attributes = {}) ⇒ Object



22
23
24
# File 'lib/spyke/associations/association.rb', line 22

def create(attributes = {})
  add_to_parent super
end

#loadObject



14
15
16
# File 'lib/spyke/associations/association.rb', line 14

def load
  find_one # Override for plural associations that return an association object
end

#new(*args) ⇒ Object Also known as: build



26
27
28
# File 'lib/spyke/associations/association.rb', line 26

def new(*args)
  add_to_parent super
end