Class: FlipperTrail::Actor
- Inherits:
-
Object
- Object
- FlipperTrail::Actor
- 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
-
#id ⇒ String?
readonly
The actor id, coerced to a string.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#type ⇒ String?
readonly
The actor type (e.g. ‘“user”`, `“system”`).
Class Method Summary collapse
- .from_model(object) ⇒ Object
-
.wrap(object) ⇒ Actor?
Coerces an arbitrary object into an Actor.
Instance Method Summary collapse
-
#initialize(type:, id:, label:) ⇒ Actor
constructor
A new instance of Actor.
- #to_h ⇒ Object
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
#id ⇒ String? (readonly)
Returns the actor id, coerced to a string.
13 |
# File 'lib/flipper_trail/actor.rb', line 13 attr_reader :type, :id, :label |
#label ⇒ Object (readonly)
Returns the value of attribute label.
13 |
# File 'lib/flipper_trail/actor.rb', line 13 attr_reader :type, :id, :label |
#type ⇒ String? (readonly)
Returns 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.
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_h ⇒ Object
47 48 49 |
# File 'lib/flipper_trail/actor.rb', line 47 def to_h { type: type, id: id, label: label } end |