Module: Foobara::DetachedEntity::Concerns::Associations::ClassMethods

Defined in:
foobara-0.0.110/projects/detached_entity/src/concerns/associations.rb

Instance Method Summary collapse

Instance Method Details

#association(name, *association_identifiers) ⇒ Object

TODO: stamp this metadata out somewhere, preferably on deep_associations hash somehow



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'foobara-0.0.110/projects/detached_entity/src/concerns/associations.rb', line 49

def association(name, *association_identifiers)
  target_association_key = association_for(association_identifiers)

  is_many = target_association_key.include?("#")

  define_method name do
    # TODO: memoize but with some smart cache busting
    values = Foobara::DataPath.values_at(target_association_key, self)

    if is_many
      values
    else
      if values.size > 1
        # :nocov:
        raise "Multiple records found for #{name} association but only expected 0 or 1."
        # :nocov:
      end

      unless values.empty?
        values.first
      end
    end
  end
end

#association_for(association_filters) ⇒ Object



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
# File 'foobara-0.0.110/projects/detached_entity/src/concerns/associations.rb', line 74

def association_for(association_filters)
  if association_filters.size == 1
    data_path = association_filters.first.to_s

    if deep_associations.key?(data_path)
      return data_path
    end
  end

  result = association_filters.inject(deep_associations.keys) do |filtered, filter|
    filtered_associations(filter, filtered)
  end

  if result.empty?
    # :nocov:
    raise "Could not find association matching #{association_filters}"
    # :nocov:
  elsif result.size > 1
    # :nocov:
    raise "Multiple associations matched by #{association_filters}"
    # :nocov:
  else
    result.first
  end
end

#construct_associations(type = attributes_type, path = DataPath.new, result = {}, initial: true) ⇒ Object

TODO: this big switch is a problem. Hard to create new types in other projects without being able to modify this switch. Figure out what to do.



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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'foobara-0.0.110/projects/detached_entity/src/concerns/associations.rb', line 154

def construct_associations(
  type = attributes_type,
  path = DataPath.new,
  result = {},
  initial: true
)
  if initial && type.extends?(BuiltinTypes[:detached_entity])
    return construct_associations(type.element_types, path, result, initial: false)
  end

  remove_sensitive = TypeDeclarations.foobara_manifest_context_remove_sensitive?

  if type.extends?(BuiltinTypes[:entity])
    result[path.to_s] = type
  elsif type.extends?(BuiltinTypes[:tuple])
    element_types = type.element_types

    if remove_sensitive
      # TODO: test this code path
      # :nocov:
      element_types = element_types&.reject(&:sensitive?)
      # :nocov:
    end

    element_types&.each&.with_index do |element_type, index|
      construct_associations(element_type, path.append(index), result, initial: false)
    end
  elsif type.extends?(BuiltinTypes[:array])
    # TODO: what to do about an associative array type?? Unclear how to make a key from that...
    # TODO: raise if associative array contains a non-persisted record to handle this edge case for now.
    element_type = type.element_type

    if element_type && (!remove_sensitive || !element_type.sensitive?)
      construct_associations(element_type, path.append(:"#"), result, initial: false)
    end
  elsif type.extends?(BuiltinTypes[:attributes]) # TODO: matches attributes itself instead of only subtypes
    type.element_types.each_pair do |attribute_name, element_type|
      if remove_sensitive && element_type.sensitive?
        next
      end

      construct_associations(element_type, path.append(attribute_name), result, initial: false)
    end
  elsif type.extends?(BuiltinTypes[:model])
    target_class = type.target_class

    method = target_class.respond_to?(:foobara_attributes_type) ? :foobara_attributes_type : :attributes_type
    attributes_type = target_class.send(method)

    if !remove_sensitive || !attributes_type.sensitive?
      construct_associations(attributes_type, path, result, initial: false)
    end
  elsif type.extends?(BuiltinTypes[:associative_array])
    # not going to bother testing this for now
    # :nocov:
    if contains_associations?(type)
      raise "Associative array types with associations in them are not currently supported. " \
            "Use attributes type if you can or set the key_type and/or value_type to duck type"
    end
    # :nocov:
  end

  result
end

#construct_deep_associations(type = attributes_type, path = DataPath.new, result = {}) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'foobara-0.0.110/projects/detached_entity/src/concerns/associations.rb', line 130

def construct_deep_associations(
  type = attributes_type,
  path = DataPath.new,
  result = {}
)
  associations = construct_associations(type, path, result)

  deep = {}

  associations.each_pair do |data_path, association_type|
    deep[data_path] = association_type

    entity_class = association_type.target_class

    entity_class.deep_associations.each_pair do |sub_data_path, sub_type|
      deep["#{data_path}.#{sub_data_path}"] = sub_type
    end
  end

  deep
end

#contains_associations?(type = entity_type, initial = true) ⇒ Boolean

Returns:

  • (Boolean)


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
263
264
265
266
# File 'foobara-0.0.110/projects/detached_entity/src/concerns/associations.rb', line 219

def contains_associations?(type = entity_type, initial = true)
  remove_sensitive = TypeDeclarations.foobara_manifest_context_remove_sensitive?

  if type.extends?(BuiltinTypes[:detached_entity])
    if initial
      element_types = type.element_types

      if remove_sensitive
        # TODO: test this code path
        # :nocov:
        element_types = element_types&.reject(&:sensitive?)
        # :nocov:
      end

      contains_associations?(element_types, false)
    else
      true
    end
  elsif type.extends?(BuiltinTypes[:array])
    # TODO: what to do about an associative array type?? Unclear how to make a key from that...
    # TODO: raise if associative array contains a non-persisted record to handle this edge case for now.
    element_type = type.element_type

    if element_type && (!remove_sensitive || !element_type.sensitive?)
      contains_associations?(element_type, false)
    end
  elsif type.extends?(BuiltinTypes[:attributes])
    type.element_types.values.any? do |element_type|
      if !remove_sensitive || !element_type.sensitive?
        contains_associations?(element_type, false)
      end
    end
  elsif type.extends?(BuiltinTypes[:associative_array])
    element_types = type.element_types

    if element_types
      types = element_types

      if remove_sensitive
        types = types&.reject(&:sensitive?)
      end

      types.any? do |key_or_value_type|
        contains_associations?(key_or_value_type, false)
      end
    end
  end
end

#filtered_associations(filter, association_keys = deep_associations.keys) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'foobara-0.0.110/projects/detached_entity/src/concerns/associations.rb', line 100

def filtered_associations(filter, association_keys = deep_associations.keys)
  if filter.is_a?(::Symbol)
    filter = filter.to_s
  end

  if filter.is_a?(::String)
    if filter =~ /[A-Z]/
      association_keys.select do |key|
        type = deep_associations[key]
        entity_class = type.target_class
        entity_class.full_entity_name.include?(filter)
      end
    else
      association_keys.select do |key|
        key.include?(filter)
      end
    end
  elsif filter.is_a?(::Class) && filter < DetachedEntity
    association_keys.select do |key|
      type = deep_associations[key]
      entity_class = type.target_class
      entity_class == filter || entity_class < filter
    end
  else
    # :nocov:
    raise "Not sure how to apply filter #{filter}"
    # :nocov:
  end
end

#foobara_associationsObject Also known as: associations



8
9
10
11
12
13
14
15
16
17
# File 'foobara-0.0.110/projects/detached_entity/src/concerns/associations.rb', line 8

def foobara_associations
  remove_sensitive = TypeDeclarations.foobara_manifest_context_remove_sensitive?

  if defined?(@foobara_associations) && @foobara_associations.key?(remove_sensitive)
    return @foobara_associations[remove_sensitive]
  end

  @foobara_associations ||= {}
  @foobara_associations[remove_sensitive] = construct_associations
end

#foobara_deep_associationsObject Also known as: deep_associations



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'foobara-0.0.110/projects/detached_entity/src/concerns/associations.rb', line 21

def foobara_deep_associations
  remove_sensitive = TypeDeclarations.foobara_manifest_context_remove_sensitive?

  if defined?(@foobara_deep_associations) && @foobara_deep_associations.key?(remove_sensitive)
    return @foobara_deep_associations[remove_sensitive]
  end

  @foobara_deep_associations ||= {}
  @foobara_deep_associations[remove_sensitive] = begin
    deep = {}

    associations.each_pair do |data_path, type|
      deep[data_path] = type

      entity_class = type.target_class

      entity_class.deep_associations.each_pair do |sub_data_path, sub_type|
        deep["#{data_path}.#{sub_data_path}"] = sub_type
      end
    end

    deep
  end
end