Class: Shamu::Features::FeaturesService

Inherits:
Services::Service show all
Includes:
Security::Support
Defined in:
lib/shamu/features/features_service.rb

Overview

...

Constant Summary collapse

SESSION_KEY =
"shamu.toggles".freeze

Dependencies collapse

Attributes included from Security::Support

#roles_service, #security_principal

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Security::Support

#authorize!, #delegate_policy_class, #permit?, #policy, #policy_class, policy_class, #service_policy_delegation?

Methods inherited from Services::Service

#cache_for, #cached_lookup, #entity_list, #entity_lookup_list, #find_by_lookup, #lazy_association, #lookup_association

Constructor Details

#initialize(config_path) ⇒ FeaturesService

Parameters:



46
47
48
49
50
# 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

Instance Attribute Details

#env_storeShamu::Features::EnvStore

Read-only access to Rack and host ENV toggle overrides.



34
# File 'lib/shamu/features/features_service.rb', line 34

attr_dependency :env_store, Shamu::Features::EnvStore

#loggerShamu::Logger

Returns:



38
# File 'lib/shamu/features/features_service.rb', line 38

attr_dependency :logger, Shamu::Logger

#session_storeShamu::Sessions::SessionStore

A persistent storage for a user session where the feature service can persist sticky feature toggles.



21
# File 'lib/shamu/features/features_service.rb', line 21

attr_dependency :session_store, Shamu::Sessions::SessionStore

#toggle_codecShamu::Features::ToggleCodec

Used to pack and unpack sticky toggle overrides in a persistent user session.



28
# File 'lib/shamu/features/features_service.rb', line 28

attr_dependency :toggle_codec, Shamu::Features::ToggleCodec

Class Method Details

.default_config_pathString

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

Returns:

  • (String)

    the default path to load toggle information from.



157
158
159
160
161
162
163
# 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) ⇒ String

Parameters:

  • path (String)

    of the default config file.

Returns:

  • (String)


167
168
169
# File 'lib/shamu/features/features_service.rb', line 167

def default_config_path=( path ) # rubocop:disable Style/TrivialAccessors
  @default_config_path = path
end

Instance Method Details

#enabled?(name) ⇒ Boolean

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

Parameters:

  • name (String)

    of the feature.

Returns:

  • (Boolean)

    true if the feature is enabled.



56
57
58
59
60
61
62
63
64
# 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) ⇒ Hash

List all the known toggles with the given prefix.

Parameters:

  • name (String)

    prefix

Returns:

  • (Hash)

    the known toggles.



69
70
71
72
73
74
75
76
77
78
# 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