Class: Toggly::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/toggly/context.rb

Overview

Evaluation context containing user identity, groups, and traits.

Examples:

context = Toggly::Context.new(
  identity: "user-123",
  groups: ["beta-testers", "premium"],
  traits: { country: "US", plan: "enterprise" }
)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#entityString? (readonly)

identity, groups, traits, and optional entity for ContextProperty filters

Returns:

  • (String, nil)

    User identity for percentage rollouts and targeting



15
16
17
# File 'lib/toggly/context.rb', line 15

def entity
  @entity
end

#groupsString? (readonly)

identity, groups, traits, and optional entity for ContextProperty filters

Returns:

  • (String, nil)

    User identity for percentage rollouts and targeting



15
16
17
# File 'lib/toggly/context.rb', line 15

def groups
  @groups
end

#identityString? (readonly)

identity, groups, traits, and optional entity for ContextProperty filters

Returns:

  • (String, nil)

    User identity for percentage rollouts and targeting



15
16
17
# File 'lib/toggly/context.rb', line 15

def identity
  @identity
end

#traitsString? (readonly)

identity, groups, traits, and optional entity for ContextProperty filters

Returns:

  • (String, nil)

    User identity for percentage rollouts and targeting



15
16
17
# File 'lib/toggly/context.rb', line 15

def traits
  @traits
end

Class Method Details

.anonymousContext

Create an empty/anonymous context

Returns:



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

Parameters:

  • identity (String)

    User identity

Returns:



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

Parameters:

Returns:

  • (Boolean)


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_keyString

Generate cache key

Returns:

  • (String)


131
132
133
# File 'lib/toggly/context.rb', line 131

def cache_key
  "#{@identity}:#{@groups.sort.join(",")}:#{traits_cache_key}"
end

#hashInteger

Hash code for use in collections

Returns:

  • (Integer)


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

Returns:

  • (Boolean)


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

Parameters:

  • group (String, Symbol)

    Group name

Returns:

  • (Boolean)


50
51
52
# File 'lib/toggly/context.rb', line 50

def in_group?(group)
  @groups.include?(group.to_s)
end

#to_hHash

Convert to hash for serialization

Returns:

  • (Hash)


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

Parameters:

  • key (String, Symbol)

    Trait key

Returns:

  • (Object, nil)


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

Parameters:

  • key (String, Symbol)

    Trait key

Returns:

  • (Boolean)


67
68
69
# File 'lib/toggly/context.rb', line 67

def trait?(key)
  @traits.key?(key.to_s)
end

#with_groups(*new_groups) ⇒ Context

Create a new context with additional groups

Parameters:

  • new_groups (Array<String>)

    Groups to add

Returns:



88
89
90
91
92
93
94
95
# File 'lib/toggly/context.rb', line 88

def with_groups(*new_groups)
  Context.new(
    identity: @identity,
    groups: @groups + new_groups.flatten.map(&:to_s),
    traits: @traits,
    entity: @entity
  )
end

#with_traits(new_traits) ⇒ Context

Create a new context with additional traits

Parameters:

  • new_traits (Hash)

    Traits to add

Returns:



75
76
77
78
79
80
81
82
# File 'lib/toggly/context.rb', line 75

def with_traits(new_traits)
  Context.new(
    identity: @identity,
    groups: @groups,
    traits: @traits.merge(normalize_traits(new_traits)),
    entity: @entity
  )
end