Class: Pinspec::Analyzer::FactoryRegistry

Inherits:
Object
  • Object
show all
Includes:
Source
Defined in:
lib/pinspec/analyzer/factory_registry.rb

Constant Summary collapse

SEARCH_PATHS =
[
  "spec/factories.rb",
  "spec/factories/**/*.rb",
  "test/factories.rb",
  "test/factories/**/*.rb"
].freeze
DSL_MODULES =
%w[FactoryBot FactoryGirl Factory].freeze
LEGACY_MODULES =
%w[FactoryGirl Factory].freeze
CALLBACK_HOOKS =
%i[after before].freeze
CALLBACK_STAGES =
%i[build create stub].freeze
HAZARD_CALLS =
%i[skip_create initialize_with to_create].freeze
BARE_NON_ATTRIBUTES =
(HAZARD_CALLS + %i[sequence]).freeze
STRUCTURAL_CALLS =
%i[trait factory transient association sequence].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_root) ⇒ FactoryRegistry

Returns a new instance of FactoryRegistry.



39
40
41
# File 'lib/pinspec/analyzer/factory_registry.rb', line 39

def initialize(app_root)
  @app_root = app_root
end

Class Method Details

.parse(path) ⇒ Object



34
35
36
# File 'lib/pinspec/analyzer/factory_registry.rb', line 34

def parse(path)
  new(File.dirname(path)).parse_files([path])
end

.read(app_root = ".") ⇒ Object



30
31
32
# File 'lib/pinspec/analyzer/factory_registry.rb', line 30

def read(app_root = ".")
  new(app_root).read
end

Instance Method Details

#factory_filesObject



62
63
64
65
66
67
68
# File 'lib/pinspec/analyzer/factory_registry.rb', line 62

def factory_files
  SEARCH_PATHS
    .flat_map { |pattern| Dir[File.join(@app_root, pattern)] }
    .select { |path| File.file?(path) }
    .uniq
    .sort
end

#parse_files(paths) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pinspec/analyzer/factory_registry.rb', line 47

def parse_files(paths)
  @factories  = []
  @skipped    = []
  @legacy_dsl = false

  paths.each { |path| parse_file(path) }
  resolve_models

  FactoryIndex.new(
    factories:  @factories,
    legacy_dsl: @legacy_dsl,
    skipped:    @skipped
  )
end

#readObject



43
44
45
# File 'lib/pinspec/analyzer/factory_registry.rb', line 43

def read
  parse_files(factory_files)
end