Class: Smplkit::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/smplkit/flags/types.rb

Overview

A typed entity referenced by targeting rules and registered with smplkit.

Represents a single entity (user, account, device, etc.). The type and key identify the entity; attributes (provided as a hash and/or keyword arguments) carry the data that targeting rules evaluate against.

Used for both authoring (+flag.get(context: […])+, client.set_context(), mgmt.contexts.register()) and reading (mgmt.contexts.list/get return populated Context instances with save / delete ready to call).

Examples:

Smplkit::Context.new("user", "user-123", plan: "enterprise")
Smplkit::Context.new("account", "acme-corp", { "region" => "us" }, employee_count: 500)

Constant Summary collapse

CONTEXT_FIELDS =
%i[type key name attributes created_at updated_at].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, key, attributes = nil, name: nil, created_at: nil, updated_at: nil, **kwargs) ⇒ Context

Returns a new instance of Context.

Raises:

  • (TypeError)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/smplkit/flags/types.rb', line 43

def initialize(type, key, attributes = nil, name: nil, created_at: nil, updated_at: nil, **kwargs)
  raise TypeError, "Context type must be a String, got #{type.class}: #{type.inspect}" unless type.is_a?(String)
  unless key.is_a?(String)
    raise TypeError,
          "Context key must be a String, got #{key.class}: #{key.inspect}. " \
          "If your identifier is numeric, stringify it at the SDK boundary."
  end

  @type = type
  @key = key
  @name = name
  @attributes = stringify_keys(merge_attributes(attributes, kwargs))
  @created_at = created_at
  @updated_at = updated_at
  @client = nil
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



40
41
42
# File 'lib/smplkit/flags/types.rb', line 40

def attributes
  @attributes
end

#created_atObject

Returns the value of attribute created_at.



41
42
43
# File 'lib/smplkit/flags/types.rb', line 41

def created_at
  @created_at
end

#keyObject (readonly)

Returns the value of attribute key.



40
41
42
# File 'lib/smplkit/flags/types.rb', line 40

def key
  @key
end

#nameObject

Returns the value of attribute name.



41
42
43
# File 'lib/smplkit/flags/types.rb', line 41

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



40
41
42
# File 'lib/smplkit/flags/types.rb', line 40

def type
  @type
end

#updated_atObject

Returns the value of attribute updated_at.



41
42
43
# File 'lib/smplkit/flags/types.rb', line 41

def updated_at
  @updated_at
end

Instance Method Details

#==(other) ⇒ Object



103
104
105
# File 'lib/smplkit/flags/types.rb', line 103

def ==(other)
  other.is_a?(Context) && other.type == @type && other.key == @key && other.attributes == @attributes
end

#_apply(other) ⇒ Object



120
121
122
123
124
125
126
127
# File 'lib/smplkit/flags/types.rb', line 120

def _apply(other)
  @type = other.type
  @key = other.key
  @name = other.name
  @attributes = other.attributes.dup
  @created_at = other.created_at
  @updated_at = other.updated_at
end

#_bind_client(client) ⇒ Object

Internal: associate a management client with this context so save/delete can route through it.



72
73
74
75
# File 'lib/smplkit/flags/types.rb', line 72

def _bind_client(client)
  @client = client
  self
end

#_clientObject



77
78
79
# File 'lib/smplkit/flags/types.rb', line 77

def _client
  @client
end

#deleteObject Also known as: delete!

Delete this context from the server.



92
93
94
95
96
# File 'lib/smplkit/flags/types.rb', line 92

def delete
  raise "Context was constructed without a client; cannot delete" if @client.nil?

  @client.delete(id)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/smplkit/flags/types.rb', line 111

def eql?(other)
  self == other
end

#hashObject



107
108
109
# File 'lib/smplkit/flags/types.rb', line 107

def hash
  [@type, @key, @attributes].hash
end

#idObject

Composite “#type:#key” identifier.



61
62
63
# File 'lib/smplkit/flags/types.rb', line 61

def id
  "#{@type}:#{@key}"
end

#inspectObject



115
116
117
118
# File 'lib/smplkit/flags/types.rb', line 115

def inspect
  "#<Smplkit::Context type=#{@type.inspect} key=#{@key.inspect} " \
    "name=#{@name.inspect} attributes=#{@attributes.inspect}>"
end

#saveObject Also known as: save!

Persist this context to the server (create or update).



82
83
84
85
86
87
88
# File 'lib/smplkit/flags/types.rb', line 82

def save
  raise "Context was constructed without a client; cannot save" if @client.nil?

  updated = @client._save_context(self)
  _apply(updated)
  self
end

#to_eval_hashObject



99
100
101
# File 'lib/smplkit/flags/types.rb', line 99

def to_eval_hash
  { "key" => @key, **@attributes }
end