Class: Association

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(edge) ⇒ Association

first_entity, association_block)



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/association.rb', line 8

def initialize(edge)
  @first_entity = Entity.find(edge['source'])
  @second_entity = Entity.find(edge['target'])
  @through_entity = nil

  @middle_entities_has_one = []
  @middle_entities_has_many = []

  @first_entity.associations << self
  @second_entity.parent_associations << self

  @name = nil
  case edge['type']
  when 'hasMany'
    @first_entity.children_has_many << @second_entity
    @second_entity.parents_has_many << @first_entity

    @name = 'has_many'
  when 'hasOne'
    @first_entity.children_has_one << @second_entity
    @second_entity.parents_has_one << @first_entity

    @name = 'has_one'
  when 'through'
    @first_entity.children_through << @second_entity
    @second_entity.parents_through << @first_entity

    @through_entity = Entity.find(edge['data']['throughNodeId'])

    @name = ':through'
  end
end

Instance Attribute Details

#first_entityObject

Returns the value of attribute first_entity.



4
5
6
# File 'lib/association.rb', line 4

def first_entity
  @first_entity
end

#middle_entities_has_manyObject

Returns the value of attribute middle_entities_has_many.



4
5
6
# File 'lib/association.rb', line 4

def middle_entities_has_many
  @middle_entities_has_many
end

#middle_entities_has_oneObject

Returns the value of attribute middle_entities_has_one.



4
5
6
# File 'lib/association.rb', line 4

def middle_entities_has_one
  @middle_entities_has_one
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/association.rb', line 4

def name
  @name
end

#second_entityObject

Returns the value of attribute second_entity.



4
5
6
# File 'lib/association.rb', line 4

def second_entity
  @second_entity
end

#through_entityObject

Returns the value of attribute through_entity.



4
5
6
# File 'lib/association.rb', line 4

def through_entity
  @through_entity
end

Class Method Details

.allObject



128
129
130
# File 'lib/association.rb', line 128

def self.all
  ObjectSpace.each_object(self).to_a
end

.check_middle_entities_include(entity) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/association.rb', line 41

def self.check_middle_entities_include(entity)
  associations = Association.all.select { |association| association.through_entity == entity }
  associations.each do |association|
    unless (association.middle_entities_has_many.include? entity) || (association.middle_entities_has_one.include? entity)
      association.set_middle_entity
    end
  end
end

Instance Method Details

#has_any?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/association.rb', line 120

def has_any?
  has_one? || has_many?
end

#has_many?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/association.rb', line 112

def has_many?
  name == 'has_many'
end

#has_one?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/association.rb', line 116

def has_one?
  name == 'has_one'
end

#set_middle_entityObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/association.rb', line 50

def set_middle_entity
  return unless through?

  source = first_entity
  target = second_entity

  if (
      source.parents_has_many +
      source.parents_has_many_through +
      source.parents_has_one +
      source.parents_has_one_through +
      source.children_has_many +
      source.children_has_many_through +
      source.children_has_one +
      source.children_has_one_through
    ).include?(through_entity) &&
     (
       (
         target.parents_has_many +
         target.parents_has_many_through +
         target.parents_has_one +
         target.parents_has_one_through +
         target.children_has_many +
         target.children_has_many_through +
         target.children_has_one +
         target.children_has_one_through
       ).include?(through_entity) || (
         through_entity.parents_has_many.map(&:table).include?(target.table) ||
         through_entity.parents_has_one.map(&:table).include?(target.table)
       )
     )

    if (
        source.children_has_many.include?(through_entity) ||
        source.children_has_many_through.include?(through_entity)
      ) || (
        target.parents_has_many.include?(through_entity) ||
        target.parents_has_many_through.include?(through_entity)
      )

      unless middle_entities_has_many.include? through_entity
        middle_entities_has_many << through_entity
        source.children_has_many_through << target
        target.parents_has_many_through << source
        Association.check_middle_entities_include(target)
      end
    else
      unless middle_entities_has_one.include? through_entity
        middle_entities_has_one << through_entity
        source.children_has_one_through << target
        target.parents_has_one_through << source
        Association.check_middle_entities_include(target)
      end
    end

  end
end

#through?Boolean

Returns:

  • (Boolean)


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

def through?
  name == ':through'
end

#update_model_from_entityObject



108
109
110
# File 'lib/association.rb', line 108

def update_model_from_entity
  first_entity.update_model(second_entity, self)
end