Module: Cased::Model

Extended by:
ActiveSupport::Concern
Included in:
CLI::Session
Defined in:
lib/cased/model.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#cased(action, category: cased_category, payload: {}) ⇒ Array

Instruments events for the model. These events are sent directly to Cased.

Parameters:

  • action (String, Symbol)

    suffix of the action.

  • category (String, Symbol) (defaults to: cased_category)

    action category name.

  • payload (Hash) (defaults to: {})

    additional payload information about the event.

Returns:

  • (Array)

    of responses from Cased.publishers



32
33
34
35
36
# File 'lib/cased/model.rb', line 32

def cased(action, category: cased_category, payload: {})
  body = cased_payload.deep_merge(payload)

  Cased.publish(body.merge(action: "#{category}.#{action}"))
end

#cased_categoryString

Defines the event key prefix for all model events.

Defaults to :my_model if the class this thing is included in is MyModel. Feel free to override to provide a more semantic, meaningful name if you so desire.

Returns:

  • (String)


45
46
47
# File 'lib/cased/model.rb', line 45

def cased_category
  self.class.cased_category
end

#cased_context(category: cased_category) ⇒ Hash

Internal: Defines the payload to describe the current subject. Can be overridden in model to change fields returned.

Examples:

model.cased_context # => { user_id: "User;1", user: "flynn" }
model.cased_context(category: :actor) # => { actor_id: "User;1", actor: "flynn" }

Parameters:

  • category (String, Symbol) (defaults to: cased_category)

    prefix that can be set to override Hash prefix.

Returns:

  • (Hash)


87
88
89
90
91
92
93
94
95
96
# File 'lib/cased/model.rb', line 87

def cased_context(category: cased_category)
  context = {}
  context["#{category}_id".to_sym] = cased_id if cased_id

  if method(:to_s).owner == self.class
    context[category] = Cased::Sensitive::String.new(to_s, label: self.class.name)
  end

  context
end

#cased_humanString

Internal: The String representation within the Cased object.

Returns:

  • (String)

    if cased_category key is present in cased_context.



73
74
75
# File 'lib/cased/model.rb', line 73

def cased_human
  cased_context[cased_category]
end

#cased_idString

Defines the Cased identifier for the current instance.

Examples:

user.cased_id # => User;1
client.cased_id # => Client;1

Returns:

  • (String)

Raises:



68
# File 'lib/cased/model.rb', line 68

def cased_id; end

#cased_payloadHash

Defines the default payload for every event.

Returns:

  • (Hash)


52
53
54
55
56
# File 'lib/cased/model.rb', line 52

def cased_payload
  {
    cased_category => self,
  }
end