Class: FlipperTrail::Actor

Inherits:
Object
  • Object
show all
Defined in:
lib/flipper_trail/actor.rb

Overview

The party a flag change is attributed to, normalized to a ‘type`, `id`, and `label`. Build one with Actor.wrap.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, id:, label:) ⇒ Actor

Returns a new instance of Actor.



15
16
17
18
19
# File 'lib/flipper_trail/actor.rb', line 15

def initialize(type:, id:, label:)
  @type = type
  @id = id&.to_s
  @label = label
end

Instance Attribute Details

#idString? (readonly)

Returns the actor id, coerced to a string.

Returns:

  • (String, nil)

    the actor id, coerced to a string



13
# File 'lib/flipper_trail/actor.rb', line 13

attr_reader :type, :id, :label

#labelObject (readonly)

Returns the value of attribute label.



13
# File 'lib/flipper_trail/actor.rb', line 13

attr_reader :type, :id, :label

#typeString? (readonly)

Returns the actor type (e.g. ‘“user”`, `“system”`).

Returns:

  • (String, nil)

    the actor type (e.g. ‘“user”`, `“system”`)



13
14
15
# File 'lib/flipper_trail/actor.rb', line 13

def type
  @type
end

Class Method Details

.from_model(object) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/flipper_trail/actor.rb', line 37

def self.from_model(object)
  id = object.respond_to?(:id) ? object.id : object.to_s
  label =
    if object.respond_to?(:email) then object.email
    elsif object.respond_to?(:name) then object.name
    else object.to_s
    end
  new(type: 'user', id: id, label: label)
end

.wrap(object) ⇒ Actor?

Coerces an arbitrary object into an FlipperTrail::Actor.

Parameters:

  • object (Actor, Hash, Object, nil)

    an Actor (returned as-is), a Hash with ‘:type`/`:id`/`:label`, a model (deriving id and label), or nil

Returns:

  • (Actor, nil)

    the wrapped actor, or nil when given nil



26
27
28
29
30
31
32
33
34
35
# File 'lib/flipper_trail/actor.rb', line 26

def self.wrap(object)
  case object
  when nil then nil
  when Actor then object
  when Hash then new(type: object[:type] || object['type'],
                     id: object[:id] || object['id'],
                     label: object[:label] || object['label'])
  else from_model(object)
  end
end

Instance Method Details

#to_hObject



47
48
49
# File 'lib/flipper_trail/actor.rb', line 47

def to_h
  { type: type, id: id, label: label }
end