Class: Detour::Feature

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Concerns::CustomHumanAttributes
Defined in:
app/models/detour/feature.rb

Overview

Represents an individual feature that may be rolled out to a set of records via individual flags, percentages, or defined groups.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.with_linesArray<Detour::Feature>

Return an array of both every feature in the database as well as every feature that is checked for in ‘@feature_search_dirs`. Features that are checked for but not persisted will be returned as unpersisted instances of this class. Each instance returned will have its `@lines` set to an array containing every line in `@feature_search_dirs` where it is checked for.

Returns:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/detour/feature.rb', line 65

def self.with_lines
  hash = all.each_with_object({}) { |feature, hash| hash[feature.name] = feature }

  Dir[*Detour.config.feature_search_dirs].each do |path|
    next unless File.file? path

    File.open path do |file|
      file.each_line.with_index(1) do |line, i|
        line.scan(feature_search_regex).each do |match, _|
          (hash[match] ||= new(name: match)).lines << "#{path}#L#{i}"
        end
      end
    end
  end

  hash.values.sort_by(&:name)
end

Instance Method Details

#flag_in_count_for(type) ⇒ Fixnum

Returns the number of flag-ins for a given type.

Examples:

feature.flag_in_count_for("users")

Returns:

  • (Fixnum)

    The number of flag-ins for the given type.



43
44
45
# File 'app/models/detour/feature.rb', line 43

def flag_in_count_for(type)
  flag_in_counts[type] || 0
end

#linesArray<String>

Returns an instance variable intended to hold an array of the lines of code that this feature appears on.

Returns:

  • (Array<String>)

    The lines that this rollout appears on (if with_lines has already been called).



29
30
31
# File 'app/models/detour/feature.rb', line 29

def lines
  @lines ||= []
end

#opt_out_count_for(type) ⇒ Fixnum

Returns the number of opt-outs for a given type.

Examples:

feature.opt_out_count_for("users")

Returns:

  • (Fixnum)

    The number of opt-outs for the given type.



53
54
55
# File 'app/models/detour/feature.rb', line 53

def opt_out_count_for(type)
  opt_out_counts[type] || 0
end

#to_sObject



33
34
35
# File 'app/models/detour/feature.rb', line 33

def to_s
  name
end