class Shamu::Features::FeaturesService

Constants

SESSION_KEY

Attributes

config_path[R]

Public Class Methods

new( config_path = nil ) click to toggle source

@!method initialize( config_path ) @param @return [FeaturesService]

Calls superclass method
# File lib/shamu/features/features_service.rb, line 46
def initialize( config_path = nil )
  @config_path = config_path || self.class.default_config_path

  super()
end

Private Class Methods

default_config_path() click to toggle source

Looks for a config/features.yml or features.yml in the current directory. Use {#ddefault_config_path=} to manually set the default config file.

@return [String] the default path to load toggle information from.

# File lib/shamu/features/features_service.rb, line 157
def default_config_path
  @default_config_path ||= begin
    path = File.expand_path( "config/features.yml" )
    path = File.expand_path( "features.yml" ) unless File.exist?( path )
    path
  end
end
default_config_path=( path ) click to toggle source

@param [String] path of the default config file. @return [String]

# File lib/shamu/features/features_service.rb, line 167
def default_config_path=( path ) # rubocop:disable Style/TrivialAccessors
  @default_config_path = path
end

Public Instance Methods

enabled?( name ) click to toggle source

Indicates if the feature is enabled for the current request/session.

@param [String] name of the feature. @return [Boolean] true if the feature is enabled.

# File lib/shamu/features/features_service.rb, line 56
def enabled?( name )
  context = build_context

  if toggle = toggles[name]
    resolve_known( toggle, context )
  else
    resolve_unknown( name )
  end
end
list( prefix = nil ) click to toggle source

List all the known toggles with the given prefix. @param [String] name prefix @return [Hash] the known toggles.

# File lib/shamu/features/features_service.rb, line 69
def list( prefix = nil )
  if prefix.present?
    toggles.each_with_object({}) do |(name, toggle), result|
      next unless name.start_with?( prefix )
      result[name] = toggle
    end
  else
    toggles.dup
  end
end

Private Instance Methods

build_context() click to toggle source
# File lib/shamu/features/features_service.rb, line 114
def build_context
  Features::Context.new self,
                        scorpion: scorpion,
                        user_id: security_principal.user_id,
                        roles: roles_service.roles_for( security_principal.user_id )
end
persist_sticky( name, result ) click to toggle source
# File lib/shamu/features/features_service.rb, line 135
def persist_sticky( name, result )
  sticky_overrides[ name ] = result
  session_store.set( SESSION_KEY, toggle_codec.pack( sticky_overrides ) )
end
resolve_known( toggle, context ) click to toggle source
# File lib/shamu/features/features_service.rb, line 105
def resolve_known( toggle, context )
  fail RetiredToggleError.new( toggle ) if toggle.retired?( context ) # rubocop:disable Style/RaiseArgs

  store_value = resolve_store_toggle( toggle )
  return store_value unless store_value.nil?

  resolve_toggle( toggle, context )
end
resolve_store_toggle( toggle ) click to toggle source
# File lib/shamu/features/features_service.rb, line 127
def resolve_store_toggle( toggle )
  # session_store is for sticky overrides
  sticky_overrides.fetch( toggle.name ) do
    # env_store is for host and service header overrides
    env_store.fetch( toggle.name )
  end
end
resolve_toggle( toggle, context ) click to toggle source
# File lib/shamu/features/features_service.rb, line 121
def resolve_toggle( toggle, context )
  toggle.enabled?( context ).tap do |result|
    persist_sticky( toggle.name, result ) if context.sticky?
  end
end
resolve_unknown( name ) click to toggle source
# File lib/shamu/features/features_service.rb, line 100
def resolve_unknown( name )
  logger.info "The '#{ name }' feature toggle has not been configured. Add to #{ config_path }."
  false
end
sticky_overrides() click to toggle source
# File lib/shamu/features/features_service.rb, line 140
def sticky_overrides
  @sticky_overrides ||= begin
    if token = session_store.fetch( SESSION_KEY )
      toggle_codec.unpack( token )
    else
      {}
    end
  end
end
toggles() click to toggle source
# File lib/shamu/features/features_service.rb, line 84
def toggles
  @toggles ||= begin
    if File.exist?( config_path )
      listener = Listen.to File.dirname( config_path ), only: File.basename( config_path ) do
        @toggles = Toggle.load( config_path )
      end
      listener.start

      Toggle.load( config_path )
    else
      logger.warn "Feature configuration file does not exist: #{ config_path }"
      {}
    end
  end
end