Module: Feat::ContextResolver

Defined in:
lib/feat/context.rb

Class Method Summary collapse

Class Method Details

.read_context_key(ctx, kind_key) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/feat/context.rb', line 46

def read_context_key(ctx, kind_key)
  obj = read_kind(ctx, kind_key)
  return nil if obj.nil?

  key = obj["key"]
  key.is_a?(String) ? key : nil
end

.read_kind(ctx, kind_key) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/feat/context.rb', line 54

def read_kind(ctx, kind_key)
  if kind_key == "user"
    obj = ctx.kinds["user"]
    return obj if obj.is_a?(Hash)
    return { "key" => ctx.targeting_key } if ctx.targeting_key

    return nil
  end
  obj = ctx.kinds[kind_key]
  obj.is_a?(Hash) ? obj : nil
end

.resolve_attribute(ctx, attribute_path) ⇒ Object

Walks “user.email”, “organization.plan”, “user.address.city” against the EvalContext. Returns nil if any segment is missing — operators treat nil as a non-match.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/feat/context.rb', line 27

def resolve_attribute(ctx, attribute_path)
  return nil if attribute_path.nil? || attribute_path.empty?

  parts = attribute_path.split(".", 2)
  kind_obj = read_kind(ctx, parts[0])
  return nil if kind_obj.nil?
  return kind_obj["key"] if parts.length == 1

  rest = parts[1]
  cur = kind_obj
  rest.split(".").each do |p|
    return nil unless cur.is_a?(Hash)
    return nil unless cur.key?(p)

    cur = cur[p]
  end
  cur
end