Class: Shamu::Features::Context

Inherits:
Object
  • Object
show all
Includes:
Attributes
Defined in:
lib/shamu/features/context.rb

Overview

Captures the environment and request specific context used to match Toggle selectors and determine if a feature should be enabled.

Attributes collapse

Instance Method Summary collapse

Methods included from Attributes

#[], #as_json, #assign_attributes, association, associations, attribute, #attribute?, attributes, #pretty_print, #set?, #slice, #to_attributes, #to_json

Constructor Details

#initialize(features_service, **attributes) ⇒ Context

Returns a new instance of Context.



43
44
45
46
# File 'lib/shamu/features/context.rb', line 43

def initialize( features_service, **attributes )
  @features_service = features_service
  super( **attributes )
end

Instance Attribute Details

#hostString

Returns the name of the host machine.

Returns:

  • (String)

    the name of the host machine.



27
28
29
# File 'lib/shamu/features/context.rb', line 27

attribute :host do
  Socket.gethostname
end

#rolesArray<Symbol>

Returns roles assigned to the current user.

Returns:

  • (Array<Symbol>)

    roles assigned to the current user.



23
# File 'lib/shamu/features/context.rb', line 23

attribute :roles

#scorpionScorpion

Returns used to dynamically look up dependencies by Shamu::Features::Conditions.

Returns:



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

attribute :scorpion

#timeTime

Returns the current time.

Returns:

  • (Time)

    the current time.



17
18
19
# File 'lib/shamu/features/context.rb', line 17

attribute :time do
  Time.zone ? Time.zone.now : Time.now
end

#user_idInteger, String

Returns id of the current user - either an Integer, or a UUID.

Returns:

  • (Integer, String)

    id of the current user - either an Integer, or a UUID.



33
# File 'lib/shamu/features/context.rb', line 33

attribute :user_id

Instance Method Details

#enabled?(name) ⇒ Boolean

Check if feature is enabled.

Returns:

  • (Boolean)


58
59
60
# File 'lib/shamu/features/context.rb', line 58

def enabled?( name )
  features_service.enabled?( name )
end

#env(name) ⇒ String

Retrieve a value from the host machine's environment. Abstracts over the ENV hash to permit some filtering and to facilitate specs.

Parameters:

  • name (String)

    of the environment variable.

Returns:

  • (String)

    the environment variable.



53
54
55
# File 'lib/shamu/features/context.rb', line 53

def env( name )
  ENV[name]
end

#sticky!Object

Remember the toggle selection in persistent storage for the user so that they will receive the same result each time.



64
65
66
67
# File 'lib/shamu/features/context.rb', line 64

def sticky!
  @sticky = true
  self
end

#sticky?Boolean

Returns true if the feature election should be remembered between requests.

Returns:

  • (Boolean)

    true if the feature election should be remembered between requests.



71
72
73
# File 'lib/shamu/features/context.rb', line 71

def sticky?
  @sticky
end