Class: Feature::Definition

Inherits:
Object
  • Object
show all
Includes:
Shared
Defined in:
lib/feature/definition.rb

Constant Summary

Constants included from Shared

Shared::PARAMS, Shared::TYPES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, opts = {}) ⇒ Definition

Returns a new instance of Definition.


16
17
18
19
20
21
22
23
24
# File 'lib/feature/definition.rb', line 16

def initialize(path, opts = {})
  @path = path
  @attributes = {}

  # assign nil, for all unknown opts
  PARAMS.each do |param|
    @attributes[param] = opts[param]
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes


8
9
10
# File 'lib/feature/definition.rb', line 8

def attributes
  @attributes
end

#pathObject (readonly)

Returns the value of attribute path


7
8
9
# File 'lib/feature/definition.rb', line 7

def path
  @path
end

Class Method Details

.definitionsObject


86
87
88
# File 'lib/feature/definition.rb', line 86

def definitions
  @definitions ||= {}
end

.load_all!Object


90
91
92
93
94
95
96
97
98
# File 'lib/feature/definition.rb', line 90

def load_all!
  definitions.clear

  paths.each do |glob_path|
    load_all_from_path!(glob_path)
  end

  definitions
end

.pathsObject


82
83
84
# File 'lib/feature/definition.rb', line 82

def paths
  @paths ||= [Rails.root.join('config', 'feature_flags', '**', '*.yml')]
end

.register_hot_reloader!Object


110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/feature/definition.rb', line 110

def register_hot_reloader!
  # Reload feature flags on change of this file or any `.yml`
  file_watcher = Rails.configuration.file_watcher.new(reload_files, reload_directories) do
    # We use `Feature::Definition` as on Ruby code-reload
    # a new class definition is created
    Feature::Definition.load_all!
  end

  Rails.application.reloaders << file_watcher
  Rails.application.reloader.to_run { file_watcher.execute_if_updated }

  file_watcher
end

.valid_usage!(key, type:, default_enabled:) ⇒ Object


100
101
102
103
104
105
106
107
108
# File 'lib/feature/definition.rb', line 100

def valid_usage!(key, type:, default_enabled:)
  if definition = definitions[key.to_sym]
    definition.valid_usage!(type_in_code: type, default_enabled_in_code: default_enabled)
  elsif type_definition = self::TYPES[type]
    raise InvalidFeatureFlagError, "Missing feature definition for `#{key}`" unless type_definition[:optional]
  else
    raise InvalidFeatureFlagError, "Unknown feature flag type used: `#{type}`"
  end
end

Instance Method Details

#keyObject


26
27
28
# File 'lib/feature/definition.rb', line 26

def key
  name.to_sym
end

#to_hObject


77
78
79
# File 'lib/feature/definition.rb', line 77

def to_h
  attributes
end

#valid_usage!(type_in_code:, default_enabled_in_code:) ⇒ Object


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/feature/definition.rb', line 60

def valid_usage!(type_in_code:, default_enabled_in_code:)
  unless Array(type).include?(type_in_code.to_s)
    # Raise exception in test and dev
    raise Feature::InvalidFeatureFlagError, "The `type:` of `#{key}` is not equal to config: " \
      "#{type_in_code} vs #{type}. Ensure to use valid type in #{path} or ensure that you use " \
      "a valid syntax: #{TYPES.dig(type, :example)}"
  end

  # We accept an array of defaults as some features are undefined
  # and have `default_enabled: true/false`
  unless Array(default_enabled).include?(default_enabled_in_code)
    # Raise exception in test and dev
    raise Feature::InvalidFeatureFlagError, "The `default_enabled:` of `#{key}` is not equal to config: " \
      "#{default_enabled_in_code} vs #{default_enabled}. Ensure to update #{path}"
  end
end

#validate!Object


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/feature/definition.rb', line 30

def validate!
  unless name.present?
    raise Feature::InvalidFeatureFlagError, "Feature flag is missing name"
  end

  unless path.present?
    raise Feature::InvalidFeatureFlagError, "Feature flag '#{name}' is missing path"
  end

  unless type.present?
    raise Feature::InvalidFeatureFlagError, "Feature flag '#{name}' is missing type. Ensure to update #{path}"
  end

  unless Definition::TYPES.include?(type.to_sym)
    raise Feature::InvalidFeatureFlagError, "Feature flag '#{name}' type '#{type}' is invalid. Ensure to update #{path}"
  end

  unless File.basename(path, ".yml") == name
    raise Feature::InvalidFeatureFlagError, "Feature flag '#{name}' has an invalid path: '#{path}'. Ensure to update #{path}"
  end

  unless File.basename(File.dirname(path)) == type
    raise Feature::InvalidFeatureFlagError, "Feature flag '#{name}' has an invalid type: '#{path}'. Ensure to update #{path}"
  end

  if default_enabled.nil?
    raise Feature::InvalidFeatureFlagError, "Feature flag '#{name}' is missing default_enabled. Ensure to update #{path}"
  end
end