Class: Diametric::Associations::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/diametric/associations/collection.rb

Instance Method Summary collapse

Constructor Details

#initialize(base, name, enum = nil, &block) ⇒ Collection


4
5
6
7
8
# File 'lib/diametric/associations/collection.rb', line 4

def initialize(base, name, enum=nil, &block)
  @base = base
  @attribute_name = name
  @data = Set.new(enum, &block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object


90
91
92
93
94
95
96
97
98
99
100
# File 'lib/diametric/associations/collection.rb', line 90

def method_missing(method, *args, &block)
  if !args.empty? && block
    @data.send(method, args, &block)
  elsif !args.empty?
    @data.send(method, args)
  elsif block
    @data.send(method, &block)
  else
    @data.send(method)
  end
end

Instance Method Details

#&(enum) ⇒ Object Also known as: intersection


65
66
67
68
69
70
71
# File 'lib/diametric/associations/collection.rb', line 65

def &(enum)
  # this method is used to test the given class is Set or not.
  # don't delete this method.
  n = self.class.new
  @data.each { |o| n.add(o) if include?(o) }
  n
end

#add(*entities) ⇒ Object Also known as: <<


10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/diametric/associations/collection.rb', line 10

def add(*entities)
  old_data = @data.dup
  entities.each do |entity|
    if entity.dbid.nil? || entity.dbid.to_i < 0
      entity.save
    end
    @data << entity.dbid
  end
  if old_data != @data
    @base.send("#{@attribute_name}=", self)
  end
end

#add_reified_entities(*entities) ⇒ Object


24
25
26
27
28
29
# File 'lib/diametric/associations/collection.rb', line 24

def add_reified_entities(*entities)
  entities.each do |entity|
    @data << entity
  end
  @base.send("clean_#{@attribute_name}=", self)
end

#delete(*entities) ⇒ Object


31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/diametric/associations/collection.rb', line 31

def delete(*entities)
  entities.each do |entity|
    return unless self.include?(entity)
    if entity.is_a?(Fixnum) || entity.respond_to?(:to_i)
      # dbid or dbid object
      @data.delete_if {|e| e.to_i == entity.to_i}
    elsif entity.respond_to?(:dbid)
      # reified or created entity
      @data.delete_if {|e| e.dbid == entity.dbid}
    end
  end
end

#destroy(*entities) ⇒ Object


44
45
46
47
48
49
50
51
52
# File 'lib/diametric/associations/collection.rb', line 44

def destroy(*entities)
  entities.each do |entity|
    return unless self.include?(entity)
    if entity.respond_to? :destroy
      entity.destroy
    end
    self.delete(entity)
  end
end

#include?(o) ⇒ Boolean Also known as: member?


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

def include?(o)
  if o.is_a?(Fixnum) || o.respond_to?(:to_i)
    @data.find_all {|e| e.respond_to?(:to_i)}.collect(&:to_i).include?(o.to_i)
  elsif o.respond_to?(:dbid)
    @data.find_all {|e| e.respond_to?(:dbid)}.collect(&:dbid).include?(o.dbid)
  else
    false
  end
end

#inspectObject


74
75
76
# File 'lib/diametric/associations/collection.rb', line 74

def inspect
  return sprintf('#<%s: {%s}>', self.class, @data.to_a.inspect[1..-2])
end

#replace(entities) ⇒ Object


78
79
80
81
82
83
84
85
86
87
88
# File 'lib/diametric/associations/collection.rb', line 78

def replace(entities)
  @data = Set.new
  entities.each do |entity|
    if entity.dbid.nil? || entity.dbid.to_i < 0
      @data << entity.dbid if entity.save
    else
      @data << entity
    end
  end
  self
end