Class: Flagsmith::Engine::Identity

Inherits:
Object
  • Object
show all
Defined in:
lib/flagsmith/engine/identities/models.rb

Overview

IdentityModel

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Identity

Returns a new instance of Identity.



10
11
12
13
14
15
16
17
18
# File 'lib/flagsmith/engine/identities/models.rb', line 10

def initialize(params)
  @identity_uuid = params.fetch(:identity_uuid, SecureRandom.uuid)
  @created_date = params[:created_date].is_a?(String) ? Date.parse(params[:created_date]) : params[:created_date]
  @identity_traits = params.fetch(:identity_traits, [])
  @identity_features = params.fetch(:identity_features, Flagsmith::Engine::Identities::FeaturesList.new)
  @environment_api_key = params.fetch(:environment_api_key)
  @identifier = params.fetch(:identifier)
  @django_id = params.fetch(:django_id, nil)
end

Instance Attribute Details

#created_dateObject (readonly)

Returns the value of attribute created_date.



7
8
9
# File 'lib/flagsmith/engine/identities/models.rb', line 7

def created_date
  @created_date
end

#django_idObject (readonly)

Returns the value of attribute django_id.



7
8
9
# File 'lib/flagsmith/engine/identities/models.rb', line 7

def django_id
  @django_id
end

#environment_api_keyObject (readonly)

Returns the value of attribute environment_api_key.



7
8
9
# File 'lib/flagsmith/engine/identities/models.rb', line 7

def environment_api_key
  @environment_api_key
end

#identifierObject (readonly)

Returns the value of attribute identifier.



7
8
9
# File 'lib/flagsmith/engine/identities/models.rb', line 7

def identifier
  @identifier
end

#identity_featuresObject (readonly)

Returns the value of attribute identity_features.



7
8
9
# File 'lib/flagsmith/engine/identities/models.rb', line 7

def identity_features
  @identity_features
end

#identity_traitsObject (readonly)

Returns the value of attribute identity_traits.



7
8
9
# File 'lib/flagsmith/engine/identities/models.rb', line 7

def identity_traits
  @identity_traits
end

#identity_uuidObject (readonly)

Returns the value of attribute identity_uuid.



7
8
9
# File 'lib/flagsmith/engine/identities/models.rb', line 7

def identity_uuid
  @identity_uuid
end

Class Method Details

.build(json) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/flagsmith/engine/identities/models.rb', line 44

def build(json)
  identity_features = Flagsmith::Engine::Identities::FeaturesList.build(json[:identity_features])
  identity_traits = json.fetch(:identity_traits, [])
                        .map { |t| Flagsmith::Engine::Identities::Trait.build(t) }

  Identity.new(
    **json.slice(:identifier, :identity_uuid, :environment_api_key, :created_date, :django_id)
          .merge(identity_features: identity_features, identity_traits: identity_traits)
  )
end

.generate_composite_key(env_key, identifier) ⇒ Object



40
41
42
# File 'lib/flagsmith/engine/identities/models.rb', line 40

def generate_composite_key(env_key, identifier)
  "#{env_key}_#{identifier}"
end

Instance Method Details

#composite_keyObject



20
21
22
# File 'lib/flagsmith/engine/identities/models.rb', line 20

def composite_key
  Identity.generate_composite_key(@environment_api_key, @identifier)
end

#update_traits(traits) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/flagsmith/engine/identities/models.rb', line 24

def update_traits(traits)
  existing_traits = {}
  @identity_traits.each { |trait| existing_traits[trait.key] = trait }

  traits.each do |trait|
    if trait.value.nil?
      existing_traits.delete(trait.key)
    else
      existing_traits[trait.key] = trait
    end
  end

  @identity_traits = existing_traits.values
end