Class: ReeDao::Association

Inherits:
Object
  • Object
show all
Includes:
Ree::LinkDSL, AssociationMethods
Defined in:
lib/ree_lib/packages/ree_dao/package/ree_dao/association.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AssociationMethods

extended, included

Constructor Details

#initialize(parent, parent_dao, list, **global_opts) ⇒ Association

Returns a new instance of Association.



12
13
14
15
16
17
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/association.rb', line 12

def initialize(parent, parent_dao, list, **global_opts)
  @parent = parent
  @parent_dao = parent_dao
  @list = list
  @global_opts = global_opts
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



350
351
352
353
354
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/association.rb', line 350

def method_missing(method, *args, &block)
  return super if !parent.agg_caller.private_methods(false).include?(method)

  parent.agg_caller.send(method, *args, &block)
end

Instance Attribute Details

#global_optsObject (readonly)

Returns the value of attribute global_opts.



9
10
11
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/association.rb', line 9

def global_opts
  @global_opts
end

#listObject (readonly)

Returns the value of attribute list.



9
10
11
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/association.rb', line 9

def list
  @list
end

#parentObject (readonly)

Returns the value of attribute parent.



9
10
11
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/association.rb', line 9

def parent
  @parent
end

#parent_daoObject (readonly)

Returns the value of attribute parent_dao.



9
10
11
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/association.rb', line 9

def parent_dao
  @parent_dao
end

Instance Method Details

#add_scopes(scope, named_scope) ⇒ Object



340
341
342
343
344
345
346
347
348
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/association.rb', line 340

def add_scopes(scope, named_scope)
  res = if named_scope
    named_scope.call(scope)
  else
    scope
  end

  res.all
end

#handle_field(field_proc) ⇒ Object



29
30
31
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/association.rb', line 29

def handle_field(field_proc)
  field_proc.call
end

#load(assoc_type, assoc_name, **__opts, &block) ⇒ Object



25
26
27
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/association.rb', line 25

def load(assoc_type, assoc_name, **__opts, &block)
  load_association(assoc_type, assoc_name, **__opts, &block)
end

#load_association(assoc_type, assoc_name, **__opts, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/association.rb', line 39

def load_association(assoc_type, assoc_name, **__opts, &block)
  __opts[:autoload_children] ||= false

  assoc_index = load_association_by_type(
    assoc_type,
    assoc_name,
    **__opts
  )

  scope = __opts[:scope]

  dao = if scope.is_a?(Array)
    return [] if scope.empty?
    nil
  else
    find_dao(assoc_name, parent, scope)
  end

  process_block(assoc_index, __opts[:autoload_children], __opts[:to_dto], dao, &block) if block_given?

  list
end

#load_association_by_type(type, assoc_name, **__opts) ⇒ Object



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
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/association.rb', line 67

def load_association_by_type(type, assoc_name, **__opts)
  case type
  when :belongs_to
    one_to_one(
      parent_dao,
      assoc_name,
      list,
      scope: __opts[:scope],
      primary_key: __opts[:primary_key],
      foreign_key: __opts[:foreign_key],
      setter: __opts[:setter],
      reverse: false
    )
  when :has_one
    one_to_one(
      parent_dao,
      assoc_name,
      list,
      scope: __opts[:scope],
      primary_key: __opts[:primary_key],
      foreign_key: __opts[:foreign_key],
      to_dto: __opts[:to_dto],
      setter: __opts[:setter],
      reverse: true
    )
  when :has_many
    one_to_many(
      parent_dao,
      assoc_name,
      list,
      scope: __opts[:scope],
      primary_key: __opts[:primary_key],
      foreign_key: __opts[:foreign_key],
      to_dto: __opts[:to_dto],
      setter: __opts[:setter]
    )
  end
end

#one_to_many(parent_dao, assoc_name, list, primary_key: nil, foreign_key: nil, scope: nil, setter: nil, to_dto: nil) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/association.rb', line 217

def one_to_many(parent_dao, assoc_name, list, primary_key: nil, foreign_key: nil, scope: nil, setter: nil, to_dto: nil)
  return {} if list.empty?

  primary_key ||= :id

  if scope.is_a?(Array)
    items = scope
  else
    assoc_dao = nil
    assoc_dao = find_dao(assoc_name, parent, scope)

    if !foreign_key
      if parent_dao.nil?
        raise ArgumentError.new("foreign_key should be provided for :#{assoc_name} association")
      end

      foreign_key = foreign_key_from_dao(parent_dao)
    end

    root_ids = list.map(&primary_key)

    scope ||= assoc_dao
    scope = scope.where(foreign_key => root_ids)

    items = add_scopes(scope, global_opts[assoc_name])
  end

  assoc = if foreign_key
    group_by(items) { _1.send(foreign_key) }
  else
    items
  end

  populate_association(
    list,
    assoc,
    assoc_name,
    setter: setter,
    primary_key: primary_key,
    foreign_key: foreign_key,
    to_dto: to_dto,
    multiple: true
  )

  assoc
end

#one_to_one(parent_dao, assoc_name, list, scope: nil, primary_key: :id, foreign_key: nil, setter: nil, to_dto: nil, reverse: true) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/association.rb', line 153

def one_to_one(parent_dao, assoc_name, list, scope: nil, primary_key: :id, foreign_key: nil, setter: nil, to_dto: nil, reverse: true)
  return {} if list.empty?

  primary_key ||= :id

  if scope.is_a?(Array)
    items = scope
  else
    assoc_dao = find_dao(assoc_name, parent, scope)

    if reverse
      # has_one
      if !foreign_key
        if parent_dao.nil?
          raise ArgumentError.new("foreign_key should be provided for :#{assoc_name} association")
        end

        foreign_key = foreign_key_from_dao(parent_dao)
      end

      root_ids = list.map(&:id).uniq
    else
      # belongs_to
      if !foreign_key
        foreign_key = :"#{assoc_name}_id"
      end

      root_ids = list.map(&:"#{foreign_key}").compact
    end

    scope ||= assoc_dao
    scope = scope.where((reverse ? foreign_key : :id) => root_ids)

    items = add_scopes(scope, global_opts[assoc_name])
  end

  assoc = index_by(items) { _1.send(reverse ? foreign_key : :id) }

  populate_association(
    list,
    assoc,
    assoc_name,
    setter: setter,
    reverse: reverse,
    primary_key: primary_key,
    foreign_key: foreign_key,
    to_dto: to_dto
  )

  assoc
end

#populate_association(list, association_index, assoc_name, primary_key: nil, foreign_key: nil, reverse: nil, setter: nil, to_dto: nil, multiple: false) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/association.rb', line 277

def populate_association(list, association_index, assoc_name, primary_key: nil, foreign_key: nil, reverse: nil, setter: nil, to_dto: nil, multiple: false)
  assoc_setter = if setter
    setter
  else
    "#{assoc_name}="
  end

  fallback_assoc_setter = nil
  fallback_fk = nil

  list.each do |item|
    if setter && setter.is_a?(Proc)
      if to_dto
        assoc_index = {}

        association_index.each do |key, value|
          if value.is_a?(Array)
            assoc_index[key] = value.map { to_dto.call(_1) }
          else
            assoc_index[key] = to_dto.call(value)
          end
        end

        self.instance_exec(item, assoc_index, &assoc_setter)
      else
        self.instance_exec(item, association_index, &assoc_setter)
      end
    else
      key = if reverse.nil?
        primary_key
      else
        if reverse
          primary_key
        else
          foreign_key ? foreign_key : (fallback_fk ||= "#{assoc_name}_id")
        end
      end

      value = association_index[item.send(key)]

      if to_dto && !value.nil?
        value = if value.is_a?(Array)
          value.map { to_dto.call(_1) }
        else
          to_dto.call(value)
        end
      end

      value = [] if value.nil? && multiple

      if item.respond_to?(assoc_setter)
        item.send(assoc_setter, value)
      else
        item.send(
          fallback_assoc_setter ||= "set_#{assoc_name}", 
          value
        )
      end
    end
  end
end

#process_block(assoc, autoload_children, to_dto, parent_dao, &block) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/association.rb', line 107

def process_block(assoc, autoload_children, to_dto, parent_dao, &block)
  assoc_list = assoc.is_a?(Array) ? assoc : assoc.values.flatten

  if to_dto
    assoc_list = assoc_list.map do |item|
      to_dto.call(item)
    end
  end

  associations = ReeDao::Associations.new(
    parent.agg_caller,
    assoc_list,
    parent.local_vars,
    parent_dao,
    autoload_children,
    **global_opts
  )

  if parent_dao.nil? || parent_dao.db.in_transaction? || ReeDao::Associations.sync_mode?
    associations.instance_exec(assoc_list, &block)
  else
    threads = associations.instance_exec(assoc_list, &block)

    threads[:association_threads].map do |association, assoc_type, assoc_name, __opts, block|
        association.load(assoc_type, assoc_name, **__opts, &block)
    end

    threads[:field_threads].map do |association, field_proc|
      association.handle_field(field_proc)
    end
  end
end