Module: Feature

Defined in:
lib/feature.rb,
lib/feature/testing.rb,
lib/feature/repository.rb,
lib/feature/repository/yaml_repository.rb,
lib/feature/generators/install_generator.rb,
lib/feature/repository/simple_repository.rb,
lib/feature/repository/active_record_repository.rb

Overview

This file provides functionality for testing your code with features activated or deactivated. This file should only be required in test/spec code!

To enable Feature testing capabilities do:

require 'feature/testing'

Defined Under Namespace

Modules: Repository Classes: InstallGenerator

Class Method Summary collapse

Class Method Details

.active?(feature) ⇒ Boolean

Requests if feature is active

Parameters:

  • feature (Symbol)

Returns:

  • (Boolean)


57
58
59
60
61
62
63
# File 'lib/feature.rb', line 57

def self.active?(feature)
  if !@repository
    raise "Feature is missing Repository for obtaining feature lists"
  end

  @active_features.include?(feature)
end

.inactive?(feature) ⇒ Boolean

Requests if feature is inactive (or unknown)

Parameters:

  • feature (Symbol)

Returns:

  • (Boolean)


70
71
72
# File 'lib/feature.rb', line 70

def self.inactive?(feature)
  !self.active?(feature)
end

.refresh!Object

Obtains list of active features from repository (for the case they change e.g. when using db-backed repository)



47
48
49
# File 'lib/feature.rb', line 47

def self.refresh!
  @active_features = @repository.active_features
end

.run_with_activated(feature) ⇒ Object

Execute the code block with the given feature active

Example usage:

Feature.run_with_activated(:feature) do
  # your test code here
end


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

def self.run_with_activated(feature)
  old_features = @active_features.dup
  @active_features.push(feature).uniq!
  yield
ensure
  @active_features = old_features
end

.run_with_deactivated(feature) ⇒ Object

Execute the code block with the given feature deactive

Example usage:

Feature.run_with_deactivated(:feature) do
  # your test code here
end


31
32
33
34
35
36
37
# File 'lib/feature/testing.rb', line 31

def self.run_with_deactivated(feature)
  old_features = @active_features.dup
  @active_features.delete(feature)
  yield
ensure
  @active_features = old_features
end

.set_repository(repository) ⇒ Object

Set the feature repository The given repository has to respond to method ‘active_features’ with an array of symbols

Parameters:

  • repository (Object)

    the repository to get the features from



36
37
38
39
40
41
42
43
# File 'lib/feature.rb', line 36

def self.set_repository(repository)
  if !repository.respond_to?(:active_features)
    raise ArgumentError, "given repository does not respond to active_features"
  end

  @repository = repository
  refresh!
end

.switch(feature, l1, l2) ⇒ Object

Return value or execute Proc/lambda depending on Feature status.

Parameters:

  • feature (Symbol)
  • value (Object)

    to be returned / lambda to be evaluated if feature is active

  • value (Object)

    to be returned / lambda to be evaluated if feature is inactive



104
105
106
107
108
109
110
# File 'lib/feature.rb', line 104

def self.switch(feature, l1, l2)
  if active?(feature)
    l1.is_a?(Proc) ? l1.call : l1
  else
    l2.is_a?(Proc) ? l2.call : l2
  end
end

.with(feature) ⇒ Object

Execute the given block if feature is active

Parameters:

  • feature (Symbol)


78
79
80
81
82
83
84
# File 'lib/feature.rb', line 78

def self.with(feature)
  if !block_given?
    raise ArgumentError, "no block given to #{__method__}"
  end

  yield if active?(feature)
end

.without(feature) ⇒ Object

Execute the given block if feature is inactive

Parameters:

  • feature (Symbol)


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

def self.without(feature)
  if !block_given?
    raise ArgumentError, "no block given to #{__method__}"
  end

  yield if inactive?(feature)
end