Class: FeatureMap::Private::AssignmentMappers::FeatureGlobs

Inherits:
Object
  • Object
show all
Includes:
Mapper, Validator
Defined in:
lib/feature_map/private/assignment_mappers/feature_globs.rb

Defined Under Namespace

Classes: GlobOverlap, MappingContext

Constant Summary collapse

@@map_files_to_features =

rubocop:disable Style/ClassVars

{}

Instance Method Summary collapse

Methods included from Validator

all, included

Methods included from Mapper

all, included, to_glob_cache

Instance Method Details

#bust_caches!Object



79
80
81
# File 'lib/feature_map/private/assignment_mappers/feature_globs.rb', line 79

def bust_caches!
  @@map_files_to_features = {} # rubocop:disable Style/ClassVars
end

#descriptionObject



83
84
85
# File 'lib/feature_map/private/assignment_mappers/feature_globs.rb', line 83

def description
  'Feature-specific assigned globs'
end

#find_overlapping_globsObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/feature_map/private/assignment_mappers/feature_globs.rb', line 38

def find_overlapping_globs
  mapped_files = {}
  FeatureMap::CodeFeatures.all.each_with_object({}) do |feature, _map|
    FeaturePlugins::Assignment.for(feature).assigned_globs.each do |glob|
      Dir.glob(glob).each do |filename|
        mapped_files[filename] ||= []
        mapped_files[filename] << MappingContext.new(glob: glob, feature: feature)
      end
    end
  end

  overlaps = []
  mapped_files.each_value do |mapping_contexts|
    if mapping_contexts.count > 1
      overlaps << GlobOverlap.new(mapping_contexts: mapping_contexts)
    end
  end

  overlaps.uniq do |glob_overlap|
    glob_overlap.mapping_contexts.map do |context|
      [context.glob, context.feature.name]
    end
  end
end

#globs_to_feature(files) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/feature_map/private/assignment_mappers/feature_globs.rb', line 71

def globs_to_feature(files)
  FeatureMap::CodeFeatures.all.each_with_object({}) do |feature, map|
    FeaturePlugins::Assignment.for(feature).assigned_globs.each do |assigned_glob|
      map[assigned_glob] = feature
    end
  end
end

#map_file_to_feature(file) ⇒ Object



63
64
65
# File 'lib/feature_map/private/assignment_mappers/feature_globs.rb', line 63

def map_file_to_feature(file)
  map_files_to_features([file])[file]
end

#map_files_to_features(files) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/feature_map/private/assignment_mappers/feature_globs.rb', line 13

def map_files_to_features(files)
  return @@map_files_to_features if @@map_files_to_features&.keys && @@map_files_to_features.keys.count.positive?

  @@map_files_to_features = FeatureMap::CodeFeatures.all.each_with_object({}) do |feature, map| # rubocop:disable Style/ClassVars
    FeaturePlugins::Assignment.for(feature).assigned_globs.each do |glob|
      Dir.glob(glob).each do |filename|
        map[filename] = feature
      end
    end
  end
end

#update_cache(cache, files) ⇒ Object



67
68
69
# File 'lib/feature_map/private/assignment_mappers/feature_globs.rb', line 67

def update_cache(cache, files)
  globs_to_feature(files)
end

#validation_errors(files:, autocorrect: true, stage_changes: true) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/feature_map/private/assignment_mappers/feature_globs.rb', line 87

def validation_errors(files:, autocorrect: true, stage_changes: true)
  overlapping_globs = AssignmentMappers::FeatureGlobs.new.find_overlapping_globs

  errors = []

  if overlapping_globs.any?
    errors << <<~MSG
      `assigned_globs` cannot overlap between features. The following globs overlap:

      #{overlapping_globs.map { |overlap| "- #{overlap.description}" }.join("\n")}
    MSG
  end

  errors
end