Module: Flattener::ResourceGenerator

Included in:
Resource
Defined in:
lib/has_helpers/flattener/resource_generator.rb

Instance Method Summary collapse

Instance Method Details

#generate_resources(for_associations = associations) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/has_helpers/flattener/resource_generator.rb', line 6

def generate_resources(for_associations = associations)
  for_associations.flat_map do |a|
    args = { parent: self, association: a }
    [].tap do |genrated_resources|
      if !a.is_pivot?
        genrated_resources.push self.class.new(**args) if a.count.zero?
        # rubocop:disable Performance/TimesMap
        genrated_resources.push(*a.expand_on.flat_map { |t| a.count.times.map { |i| self.class.new(index: i + 1, type: t, **args) } }) if a.expand_on
        genrated_resources.push(*a.count.times.map { |i| self.class.new(index: i + 1, **args) }) if a.count && !a.expand_on
        # rubocop:enable Performance/TimesMap
      end
    end
  end
end