Class: CanDo

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/can_do.rb

Overview

Flips your features based on a config/features.yml file or environment variables. Environment variables always take precedence over the settings in your YAML file.

Examples:

config/features.yml

defaults:
  some_feature: false
  other_feature: true
development:
  some_feature: true

test if a feature should be enabled

> RAILS_ENV=development rails console
CanDo.feature?(:some_feature) # => false

overwrite setting with environment variable

> SOME_FEATURE=true RAILS_ENV=development rails console
CanDo.feature?(:some_feature) # => true

call with a block

CanDo.feature?(:some_feature) do
  # this block get's called if some_feature is enabled
end

Constant Summary collapse

NotAFeature =
Class.new(StandardError)
THE_TRUTH =
/^(true|t|yes|y|1)$/i
DEFAULT_NAMESPACE =
"defaults".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#featuresObject (readonly)

Returns the value of attribute features.



35
36
37
# File 'lib/can_do.rb', line 35

def features
  @features
end

Class Method Details

.feature?(name, &block) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/can_do.rb', line 41

def self.feature?(name, &block)
  name     = name.to_s
  env_name = name.upcase

  fail NotAFeature.new(name) unless features.key?(name)

  is_enabled =
    if ENV.key?(env_name)
      !!(ENV[env_name] =~ THE_TRUTH)
     else
       features[name] == true
     end

  # If no block is passed, return true or false
  return is_enabled unless block_given?

  # If a block is passed, return block or nil
  yield if is_enabled
end

.featuresObject



37
38
39
# File 'lib/can_do.rb', line 37

def self.features
  instance.features
end