Module: FeatureMap::CodeFeatures

Defined in:
lib/feature_map/code_features.rb,
lib/feature_map/code_features/plugin.rb,
lib/feature_map/code_features/plugins/identity.rb

Defined Under Namespace

Modules: Plugins Classes: Feature, IncorrectPublicApiUsageError, Plugin

Constant Summary collapse

NON_BREAKING_SPACE =
65_279.chr(Encoding::UTF_8)

Class Method Summary collapse

Class Method Details

.allObject



17
18
19
20
# File 'lib/feature_map/code_features.rb', line 17

def self.all
  @all ||= from_csv('.feature_map/feature_definitions.csv')
  @all ||= for_directory('.feature_map/definitions')
end

.bust_caches!Object

Generally, you should not ever need to do this, because once your ruby process loads, cached content should not change. Namely, the YML files that are the source of truth for features should not change, so we should not need to look at the YMLs again to verify. The primary reason this is helpful is for tests where each context is testing against a different set of features



69
70
71
72
73
# File 'lib/feature_map/code_features.rb', line 69

def self.bust_caches!
  Plugin.bust_caches!
  @all = nil
  @index_by_name = nil
end

.find(name) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/feature_map/code_features.rb', line 22

def self.find(name)
  @index_by_name ||= begin
    result = {}
    all.each { |t| result[t.name] = t }
    result
  end

  @index_by_name[name]
end

.for_directory(dir) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/feature_map/code_features.rb', line 48

def self.for_directory(dir)
  Pathname.new(dir).glob('**/*.yml').map do |path|
    Feature.from_yml(path.to_s)
  rescue Psych::SyntaxError
    raise IncorrectPublicApiUsageError, "The YML in #{path} has a syntax error!"
  end
end

.from_csv(file_path) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/feature_map/code_features.rb', line 32

def self.from_csv(file_path)
  return nil if !File.exist?(file_path)

  file_lines = File.readlines(file_path)
  # Remove any non-breaking space characters, as these can throw off the comment handling
  # and/or attribute key values.
  csv_content = file_lines.map { |line| line.gsub(NON_BREAKING_SPACE, '') }
                          .reject { |line| line.start_with?('#') }
                          .join.strip

  CSV.parse(csv_content, headers: true).map do |csv_row|
    feature_data = csv_row.to_h.transform_keys { |column_name| tag_value_for(column_name) }
    Feature.from_hash(feature_data)
  end
end

.tag_value_for(string) ⇒ Object



62
63
64
# File 'lib/feature_map/code_features.rb', line 62

def self.tag_value_for(string)
  string.tr('&', ' ').gsub(/\s+/, '_').downcase
end

.validation_errors(features) ⇒ Object



56
57
58
59
60
# File 'lib/feature_map/code_features.rb', line 56

def self.validation_errors(features)
  Plugin.all_plugins.flat_map do |plugin|
    plugin.validation_errors(features)
  end
end