webvalve icon indicating copy to clipboard operation
webvalve copied to clipboard

refactor activation logic / defaults to simplify Manager class

Open samandmoore opened this issue 6 years ago • 2 comments

based on this suggestion here from @smudge https://github.com/Betterment/webvalve/pull/34#discussion_r341619281

I wonder if in some earlier routine it would make sense to do something like this:

def set_defaults!
  if Webvalve.env.in?(ALWAYS_ENABLED_ENVS)
    if ENV.key? 'WEBVALVE_SERVICE_ENABLED_DEFAULT'
      WebValve.logger.warn SERVICE_ENABLED_DEFAULT_WARNING
    end
    if ENV.key? 'WEBVALVE_ENABLED'
      WebValve.logger.warn WEBVALVE_ENABLED_WARNING
    end
    ENV['WEBVALVE_ENABLED'] = '1'
    ENV['WEBVALVE_SERVICE_ENABLED_DEFAULT'] = '0'
  else
    ENV['WEBVALVE_ENABLED'] ||= '0'
    ENV['WEBVALVE_SERVICE_ENABLED_DEFAULT'] ||= '1'
  end
end

And then inside of Manager there doesn't need to be a concept of "always" or "explicit" - it can more naively trust that the two ENV vars are set and mean what they say they mean:

def enabled?
  ENABLED_VALUES.include?(ENV.fetch('WEBVALVE_ENABLED'))
end

def services_enabled_by_default?
  ENABLED_VALUES.include?(ENV.fetch('WEBVALVE_SERVICE_ENABLED_DEFAULT'))
end

def allowing?
  enabled? && services_enabled_by_default?
end

def intercepting?
  enabled? && !services_enabled_by_default?
end

samandmoore avatar Nov 04 '19 21:11 samandmoore

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Dec 04 '19 22:12 stale[bot]

This issue has been automatically closed because it has not had recent activity. Thank you for your contributions.

stale[bot] avatar Dec 11 '19 23:12 stale[bot]