Class: Toggly::Context
- Inherits:
-
Object
- Object
- Toggly::Context
- Defined in:
- lib/toggly/context.rb
Overview
Evaluation context containing user identity, groups, and traits.
Instance Attribute Summary collapse
-
#entity ⇒ String?
readonly
identity, groups, traits, and optional entity for ContextProperty filters.
-
#groups ⇒ String?
readonly
identity, groups, traits, and optional entity for ContextProperty filters.
-
#identity ⇒ String?
readonly
identity, groups, traits, and optional entity for ContextProperty filters.
-
#traits ⇒ String?
readonly
identity, groups, traits, and optional entity for ContextProperty filters.
Class Method Summary collapse
-
.anonymous ⇒ Context
Create an empty/anonymous context.
-
.with_identity(identity) ⇒ Context
Create a context with just an identity.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Check equality.
-
#cache_key ⇒ String
Generate cache key.
-
#hash ⇒ Integer
Hash code for use in collections.
-
#identity? ⇒ Boolean
Check if context has an identity.
-
#in_group?(group) ⇒ Boolean
Check if user is in a specific group.
-
#initialize(identity: nil, groups: [], traits: {}, entity: nil) ⇒ Context
constructor
A new instance of Context.
-
#to_h ⇒ Hash
Convert to hash for serialization.
-
#trait(key) ⇒ Object?
(also: #[])
Get a trait value.
-
#trait?(key) ⇒ Boolean
Check if a trait exists.
-
#with_groups(*new_groups) ⇒ Context
Create a new context with additional groups.
-
#with_traits(new_traits) ⇒ Context
Create a new context with additional traits.
Constructor Details
#initialize(identity: nil, groups: [], traits: {}, entity: nil) ⇒ Context
Returns a new instance of Context.
17 18 19 20 21 22 |
# File 'lib/toggly/context.rb', line 17 def initialize(identity: nil, groups: [], traits: {}, entity: nil) @identity = identity&.to_s @groups = Array(groups).map(&:to_s) @traits = normalize_traits(traits) @entity = entity end |
Instance Attribute Details
#entity ⇒ String? (readonly)
identity, groups, traits, and optional entity for ContextProperty filters
15 16 17 |
# File 'lib/toggly/context.rb', line 15 def entity @entity end |
#groups ⇒ String? (readonly)
identity, groups, traits, and optional entity for ContextProperty filters
15 16 17 |
# File 'lib/toggly/context.rb', line 15 def groups @groups end |
#identity ⇒ String? (readonly)
identity, groups, traits, and optional entity for ContextProperty filters
15 16 17 |
# File 'lib/toggly/context.rb', line 15 def identity @identity end |
#traits ⇒ String? (readonly)
identity, groups, traits, and optional entity for ContextProperty filters
15 16 17 |
# File 'lib/toggly/context.rb', line 15 def traits @traits end |
Class Method Details
.anonymous ⇒ Context
Create an empty/anonymous context
35 36 37 |
# File 'lib/toggly/context.rb', line 35 def self.anonymous new end |
.with_identity(identity) ⇒ Context
Create a context with just an identity
28 29 30 |
# File 'lib/toggly/context.rb', line 28 def self.with_identity(identity) new(identity: identity) end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Check equality
112 113 114 115 116 117 118 |
# File 'lib/toggly/context.rb', line 112 def ==(other) return false unless other.is_a?(Context) @identity == other.identity && @groups.sort == other.groups.sort && @traits == other.traits end |
#cache_key ⇒ String
Generate cache key
131 132 133 |
# File 'lib/toggly/context.rb', line 131 def cache_key "#{@identity}:#{@groups.sort.join(",")}:#{traits_cache_key}" end |
#hash ⇒ Integer
Hash code for use in collections
124 125 126 |
# File 'lib/toggly/context.rb', line 124 def hash [@identity, @groups.sort, @traits].hash end |
#identity? ⇒ Boolean
Check if context has an identity
42 43 44 |
# File 'lib/toggly/context.rb', line 42 def identity? !@identity.nil? && !@identity.empty? end |
#in_group?(group) ⇒ Boolean
Check if user is in a specific group
50 51 52 |
# File 'lib/toggly/context.rb', line 50 def in_group?(group) @groups.include?(group.to_s) end |
#to_h ⇒ Hash
Convert to hash for serialization
100 101 102 103 104 105 106 |
# File 'lib/toggly/context.rb', line 100 def to_h { identity: @identity, groups: @groups, traits: @traits } end |
#trait(key) ⇒ Object? Also known as: []
Get a trait value
58 59 60 |
# File 'lib/toggly/context.rb', line 58 def trait(key) @traits[key.to_s] end |
#trait?(key) ⇒ Boolean
Check if a trait exists
67 68 69 |
# File 'lib/toggly/context.rb', line 67 def trait?(key) @traits.key?(key.to_s) end |