module Corral

Constants

VERSION

Public Class Methods

corral(env = nil, &block) click to toggle source
# File lib/corral.rb, line 14
def self.corral(env = nil, &block)
  self.environment = env.to_s
  instance_eval(&block)
end
disable(feature, options = {}) click to toggle source
# File lib/corral.rb, line 19
def self.disable(feature, options = {})
  flip_feature(feature, options.merge(enable: false))
end
disabled?(feature, *arguments) click to toggle source
# File lib/corral.rb, line 27
def self.disabled?(feature, *arguments)
  !self.enabled?(feature, *arguments)
end
enable(feature, options = {}) click to toggle source
# File lib/corral.rb, line 23
def self.enable(feature, options = {})
  flip_feature(feature, options.merge(enable: true))
end
enabled?(feature, *arguments) click to toggle source
# File lib/corral.rb, line 31
def self.enabled?(feature, *arguments)
  (feature = Feature.get(feature)) && (condition = feature.condition) or
    return false

  call_condition(feature, condition, *arguments)
end
environment() click to toggle source
# File lib/corral.rb, line 6
def self.environment
  @environment
end
environment=(env) click to toggle source
# File lib/corral.rb, line 10
def self.environment=(env)
  @environment = env.to_sym
end

Private Class Methods

call_condition(feature, condition, *arguments) click to toggle source
# File lib/corral.rb, line 72
def self.call_condition(feature, condition, *arguments)
  if feature.disabled
    !condition.call(*arguments)
  else
    condition.call(*arguments)
  end
end
environment_override(feature, enable, *environments) click to toggle source
# File lib/corral.rb, line 40
def self.environment_override(feature, enable, *environments)
  envs = environments.map(&:to_sym)
  condition = -> { envs.any? { |env| env == self.environment } }
  push_feature(enable, feature, condition)
end
flip_feature(feature, options = {}) click to toggle source
# File lib/corral.rb, line 55
def self.flip_feature(feature, options = {})
  enable = options[:enable]
  environments = options[:in] and
    return environment_override(feature, enable, *environments)
  condition = process_condition(options)

  push_feature(enable, feature, condition)
end
process_condition(options = {}) click to toggle source
# File lib/corral.rb, line 46
def self.process_condition(options = {})
  condition = options[:when] || options[:if]

  (condition && !condition.respond_to?(:call)) and
    raise "'when' or 'if' condition must be a callable object"

  condition
end
push_feature(enable, feature, condition) click to toggle source
# File lib/corral.rb, line 64
def self.push_feature(enable, feature, condition)
  if enable
    Feature.enable(feature, condition)
  else
    Feature.disable(feature, condition)
  end
end