Module: Featuring::Flaggable

Defined in:
lib/featuring/flaggable.rb

Overview

Internal concerns related to defining feature flags on a module or class.

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(object) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/featuring/flaggable.rb', line 11

def self.extended(object)
  super

  case object
  when Class
    object.include object.internal_feature_checks_module
  when Module
    object.extend object.internal_feature_checks_module
  end
end

.setup_flaggable_object(object) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/featuring/flaggable.rb', line 22

def self.setup_flaggable_object(object)
  case object
  when Class
    object.extend ClassMethods
    object.prepend InstanceMethods
  when Module
    object.extend Flaggable
    object.extend Delegatable
    object.extend Serializable
  end
end

Instance Method Details

#define_feature_flag(name, default, &block) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/featuring/flaggable.rb', line 46

def define_feature_flag(name, default, &block)
  # Define a feature check method that returns the default value or the block's return value.
  #
  internal_feature_module.module_eval do
    if method_defined?(name)
      undef_method(name)
    end

    if block
      define_method name, &block
    else
      define_method name do
        default
      end
    end
  end

  # Define a method that typecasts the value returned from the delegator to true/false. This is
  # the method that's called when calling code asks if a feature is enabled. It guarantees that
  # only true or false is returned when checking a feature flag.
  #
  internal_feature_checks_module.module_eval do
    method_name = "#{name}?"

    if method_defined?(method_name)
      undef_method(method_name)
    end

    define_method method_name do |*args|
      fetch_feature_flag_value(name, *args)
    end
  end

  # Keep track of the feature flag we just defined.
  #
  feature_flags << name
end

#feature_flagsObject



84
85
86
# File 'lib/featuring/flaggable.rb', line 84

def feature_flags
  @_feature_flags ||= []
end

#internal_feature_checks_moduleObject

Contains methods that wrap the original values to typecast them into true or false values.



42
43
44
# File 'lib/featuring/flaggable.rb', line 42

def internal_feature_checks_module
  @_internal_feature_checks_module ||= Module.new
end

#internal_feature_moduleObject

Contains methods that return the feature’s original default value, or block value.



36
37
38
# File 'lib/featuring/flaggable.rb', line 36

def internal_feature_module
  @_internal_feature_module ||= Module.new
end