Class: Detour::Feature

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Concerns::FlagActions, Concerns::Matchers
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

Methods included from Concerns::Matchers

#match?, #match_groups?, #match_id?, #match_percentage?

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 ‘@grep_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 `@grep_dirs` where it is checked for.



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

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

  Dir[*Detour.config.grep_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(/\.has_feature\?\s*\(*:(\w+)/).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")


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

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.



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

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")


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

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

#to_sObject



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

def to_s
  name
end