Module: EzLogsAgent::ResourceExtractor

Defined in:
lib/ez_logs_agent/resource_extractor.rb

Overview

ResourceExtractor provides explicit, opt-in helpers for extracting resource_ids from objects.

This module is a small, boring utility that:

  • Converts ActiveRecord model instances to resource_id hashes

  • Extracts resource_ids from arrays of models

  • Passes through existing resource_ids from hashes

  • Returns empty array for unsupported inputs (defensive)

ResourceExtractor does NOT:

  • Automatically hook into anything

  • Infer resources from SQL or context

  • Guess actor or ownership

  • Read RequestStore or global state

  • Modify EventBuilder or other components

  • Raise exceptions for unsupported input

Missing resource_ids is acceptable. Wrong or guessed resource_ids is NOT acceptable.

Examples:

Extract from ActiveRecord model

user = User.find(123)
EzLogsAgent::ResourceExtractor.from_record(user)
# => [{ resource_type: "user_id", resource_id: "123" }]

Extract from array of models

orders = [Order.find(1), Order.find(2)]
EzLogsAgent::ResourceExtractor.from_records(orders)
# => [
#   { resource_type: "order_id", resource_id: "1" },
#   { resource_type: "order_id", resource_id: "2" }
# ]

Extract from hash

hash = { resource_ids: [{ resource_type: "product_id", resource_id: "456" }] }
EzLogsAgent::ResourceExtractor.from_hash(hash)
# => [{ resource_type: "product_id", resource_id: "456" }]

Unsupported input returns empty array

EzLogsAgent::ResourceExtractor.from_record("not a model")
# => []

Class Method Summary collapse

Class Method Details

.from_hash(hash) ⇒ Array<Hash>

Extracts resource_ids from a hash containing a :resource_ids key

Examples:

Valid hash with resource_ids

hash = { resource_ids: [{ resource_type: "product_id", resource_id: "456" }] }
ResourceExtractor.from_hash(hash)
# => [{ resource_type: "product_id", resource_id: "456" }]

Hash without resource_ids key

ResourceExtractor.from_hash({ foo: "bar" })
# => []

Non-hash input

ResourceExtractor.from_hash("not a hash")
# => []

Parameters:

  • hash (Hash)

    Hash potentially containing :resource_ids key

Returns:

  • (Array<Hash>)

    Array of resource_id hashes, or empty array



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ez_logs_agent/resource_extractor.rb', line 125

def self.from_hash(hash)
  return [] unless hash.is_a?(Hash)
  return [] unless hash.key?(:resource_ids)

  resource_ids = hash[:resource_ids]
  return [] unless resource_ids.is_a?(Array)

  resource_ids
rescue => e
  # Defensive: never raise exceptions, return empty array
  []
end

.from_record(record) ⇒ Array<Hash>

Extracts resource_id from a single ActiveRecord model instance

Examples:

Valid ActiveRecord model

user = User.find(123)
ResourceExtractor.from_record(user)
# => [{ resource_type: "user_id", resource_id: "123" }]

Unsupported input

ResourceExtractor.from_record("not a model")
# => []

Nil input

ResourceExtractor.from_record(nil)
# => []

Parameters:

  • record (Object)

    Potentially an ActiveRecord model instance

Returns:

  • (Array<Hash>)

    Array containing single resource_id hash, or empty array



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ez_logs_agent/resource_extractor.rb', line 64

def self.from_record(record)
  return [] if record.nil?
  return [] unless active_record_model?(record)

  [{
    resource_type: resource_type_from_class(record.class),
    resource_id: record.id.to_s
  }]
rescue => e
  # Defensive: never raise exceptions, return empty array
  []
end

.from_records(records) ⇒ Array<Hash>

Extracts resource_ids from an array of ActiveRecord model instances

Examples:

Valid ActiveRecord models

orders = [Order.find(1), Order.find(2)]
ResourceExtractor.from_records(orders)
# => [
#   { resource_type: "order_id", resource_id: "1" },
#   { resource_type: "order_id", resource_id: "2" }
# ]

Mixed array (filters invalid entries)

ResourceExtractor.from_records([user, "not a model", nil])
# => [{ resource_type: "user_id", resource_id: "123" }]

Empty array

ResourceExtractor.from_records([])
# => []

Parameters:

  • records (Array)

    Array of potentially ActiveRecord model instances

Returns:

  • (Array<Hash>)

    Array of resource_id hashes, or empty array



98
99
100
101
102
103
104
105
# File 'lib/ez_logs_agent/resource_extractor.rb', line 98

def self.from_records(records)
  return [] unless records.is_a?(Array)

  records.flat_map { |record| from_record(record) }.compact
rescue => e
  # Defensive: never raise exceptions, return empty array
  []
end