Class: RDFMapper::Associations::HasMany

Inherits:
Base
  • Object
show all
Defined in:
lib/lib/associations/has_many.rb

Overview

-

Instance Method Summary collapse

Methods inherited from Base

#initialize, #inspect, #object, #to_statements

Methods included from Logger

#debug, #fatal, #warn

Constructor Details

This class inherits a constructor from RDFMapper::Associations::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RDFMapper::Associations::Base

Instance Method Details

#<<(*objects) ⇒ Object Also known as: push

Adds one or more objects to the collection by setting their foreign keys to the collection’s primary key.



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/lib/associations/has_many.rb', line 36

def <<(*objects)
  objects.to_a.select do |child|
    child.kind_of? RDFMapper::Model
  end.each do |child|
    unless include?(child)
      child[reverse] = @instance
      @value << child
    end
  end
  self
end

#build(attributes) ⇒ Object

Returns a new object of the collection type that has been instantiated with attributes and linked to this object, but not yet saved.



84
85
86
87
# File 'lib/lib/associations/has_many.rb', line 84

def build(attributes)
  obj = @association.new(attributes)
  (self << obj).last
end

#clearObject

Removes every object from the collection.



67
68
69
# File 'lib/lib/associations/has_many.rb', line 67

def clear
  delete(@value)
end

#create(attributes = {}) ⇒ Object

Returns a new object of the collection type that has been instantiated with attributes, linked to this object through a foreign key, and that has already been saved.



94
95
96
97
# File 'lib/lib/associations/has_many.rb', line 94

def create(attributes = {})
  obj = @association.create(attributes.merge({ reverse => @instance }))
  (self << obj).last
end

#delete(*objects) ⇒ Object

Removes one or more objects from the collection by removing the association between objects



54
55
56
57
58
59
60
61
62
# File 'lib/lib/associations/has_many.rb', line 54

def delete(*objects)
  objects.each do |child|
    if include?(child)
      child[reverse] = nil
      @value.delete(child)
    end
  end
  self
end

#find(*args) ⇒ Object

Finds an associated object according to the same rules as RDFMapper::Model.find.



75
76
77
# File 'lib/lib/associations/has_many.rb', line 75

def find(*args)
  @association.find(*args).from(value)
end

#find_or_create(attributes = {}) ⇒ Object

Either finds or creates a new object in the collection.



102
103
104
105
# File 'lib/lib/associations/has_many.rb', line 102

def find_or_create(attributes = {})
  obj = attributes[:id].nil? ? nil : find(attributes[:id])
  obj.nil? ? create(attributes) : obj
end

#include?(object) ⇒ Boolean

Returns true if a given object is present in the collection

Returns:

  • (Boolean)


110
111
112
# File 'lib/lib/associations/has_many.rb', line 110

def include?(object)
  value.include?(object)
end

#kind_of?(cls) ⇒ Boolean

-

Returns:

  • (Boolean)


124
125
126
# File 'lib/lib/associations/has_many.rb', line 124

def kind_of?(cls)
  cls == self.class || cls == Enumerable || cls == Array
end

#replace(objects) ⇒ Object

Replaces the collections content by deleting and adding objects as appropriate.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/lib/associations/has_many.rb', line 12

def replace(objects)
  unless objects.kind_of? Array
    objects = [objects]
  end

  new_objects = filter(objects)
  return self if new_objects.empty?
  
  new_objects.each do |child|
    self << child
  end
  
  @value ||= []
  @value.each do |child|
    delete(child) unless new_objects.include?(child)
  end
  
  self
end

#to_aObject

-


117
118
119
# File 'lib/lib/associations/has_many.rb', line 117

def to_a
  value
end