Class: Pinspec::FactoryIndex

Inherits:
Data
  • Object
show all
Defined in:
lib/pinspec/types.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#factoriesObject (readonly)

Returns the value of attribute factories

Returns:

  • (Object)

    the current value of factories



353
354
355
# File 'lib/pinspec/types.rb', line 353

def factories
  @factories
end

#legacy_dslObject (readonly)

Returns the value of attribute legacy_dsl

Returns:

  • (Object)

    the current value of legacy_dsl



353
354
355
# File 'lib/pinspec/types.rb', line 353

def legacy_dsl
  @legacy_dsl
end

#skippedObject (readonly)

Returns the value of attribute skipped

Returns:

  • (Object)

    the current value of skipped



353
354
355
# File 'lib/pinspec/types.rb', line 353

def skipped
  @skipped
end

Instance Method Details

#ancestry(name) ⇒ Object



373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/pinspec/types.rb', line 373

def ancestry(name)
  chain = []
  seen  = []
  current = factory(name)

  while current && !seen.include?(current.name)
    seen << current.name
    chain.unshift(current)
    current = current.parent && factory(current.parent)
  end

  chain
end

#attributes_for(name, traits: []) ⇒ Object



387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/pinspec/types.rb', line 387

def attributes_for(name, traits: [])
  chain = ancestry(name)
  merged = {}

  chain.each { |f| f.attributes.each { |a| merged[a.name] = a } }

  Array(traits).each do |trait_name|
    found = chain.reverse.filter_map { |f| f.trait(trait_name) }.first
    found&.attributes&.each { |a| merged[a.name] = a }
  end

  merged.values
end

#dsl_moduleObject



365
366
367
# File 'lib/pinspec/types.rb', line 365

def dsl_module
  legacy_dsl ? "FactoryGirl" : "FactoryBot"
end

#factory(name) ⇒ Object



354
355
356
357
358
359
# File 'lib/pinspec/types.rb', line 354

def factory(name)
  wanted = name.to_sym

  factories.find { |f| f.name == wanted } ||
    factories.find { |f| f.aliases.include?(wanted) }
end

#for_model(model) ⇒ Object



361
362
363
# File 'lib/pinspec/types.rb', line 361

def for_model(model)
  factories.select { |f| f.model == model.to_s }
end

#traits_for(name) ⇒ Object



369
370
371
# File 'lib/pinspec/types.rb', line 369

def traits_for(name)
  ancestry(name).flat_map(&:traits)
end