Module: Dbwatcher::Storage::Concerns::DataNormalizer

Included in:
Api::BaseAPI, Dbwatcher::Storage::ChangeProcessor, SessionOperations, SessionStorage
Defined in:
lib/dbwatcher/storage/concerns/data_normalizer.rb

Overview

Provides consistent data normalization capabilities across storage classes

This concern standardizes how different data types are normalized to ensure consistent symbol-key usage and proper data formatting throughout the storage layer.

Examples:

class MyStorage < BaseStorage
  include Concerns::DataNormalizer

  def save(data)
    normalized_data = normalize_session_data(data)
    # ... save logic
  end
end

Instance Method Summary collapse

Instance Method Details

#extract_value(hash, key) ⇒ Object

Extract value by trying both string and symbol keys

Parameters:

  • hash (Hash)

    hash to extract from

  • key (String, Symbol)

    key to extract

Returns:

  • (Object)

    extracted value or nil



65
66
67
68
69
# File 'lib/dbwatcher/storage/concerns/data_normalizer.rb', line 65

def extract_value(hash, key)
  return nil unless hash.is_a?(Hash)

  hash[key.to_sym] || hash[key.to_s]
end

#normalize_change(change) ⇒ Hash

Normalize change data to use consistent symbol keys

Parameters:

  • change (Hash)

    change data to normalize

Returns:

  • (Hash)

    normalized change hash with symbol keys



54
55
56
57
58
# File 'lib/dbwatcher/storage/concerns/data_normalizer.rb', line 54

def normalize_change(change)
  return change unless change.is_a?(Hash)

  normalize_hash_keys(change)
end

#normalize_hash_keys(hash) ⇒ Hash

Normalizes hash keys to symbols (Rails-compatible)

Parameters:

  • hash (Hash)

    hash to normalize

Returns:

  • (Hash)

    hash with symbolized keys



40
41
42
43
44
45
46
47
48
# File 'lib/dbwatcher/storage/concerns/data_normalizer.rb', line 40

def normalize_hash_keys(hash)
  return hash unless hash.is_a?(Hash)

  if hash.respond_to?(:with_indifferent_access)
    hash.with_indifferent_access.symbolize_keys
  else
    hash.transform_keys { |key| key.to_s.to_sym }
  end
end

#normalize_operation(operation) ⇒ String

Normalize operation to uppercase string

Parameters:

  • operation (String, Symbol)

    operation to normalize

Returns:

  • (String)

    uppercase operation string



96
97
98
# File 'lib/dbwatcher/storage/concerns/data_normalizer.rb', line 96

def normalize_operation(operation)
  operation&.to_s&.upcase
end

#normalize_record_id(record_id) ⇒ String

Normalize record ID to string

Parameters:

  • record_id (String, Integer)

    record ID to normalize

Returns:

  • (String)

    normalized record ID



112
113
114
# File 'lib/dbwatcher/storage/concerns/data_normalizer.rb', line 112

def normalize_record_id(record_id)
  record_id&.to_s
end

#normalize_session_data(session) ⇒ Hash

Normalizes session input to hash with consistent symbol keys

Parameters:

  • session (Hash, Object)

    session data to normalize

Returns:

  • (Hash)

    normalized session hash with symbol keys



25
26
27
28
29
30
31
32
33
34
# File 'lib/dbwatcher/storage/concerns/data_normalizer.rb', line 25

def normalize_session_data(session)
  case session
  when Hash
    normalize_hash_keys(session)
  when ->(s) { s.respond_to?(:to_h) }
    normalize_hash_keys(session.to_h)
  else
    extract_object_attributes(session)
  end
end

#normalize_table_name(table_name) ⇒ String

Normalize table name to string

Parameters:

  • table_name (String, Symbol)

    table name to normalize

Returns:

  • (String)

    normalized table name



104
105
106
# File 'lib/dbwatcher/storage/concerns/data_normalizer.rb', line 104

def normalize_table_name(table_name)
  table_name&.to_s
end

#normalize_timestamp(timestamp) ⇒ Time

Normalize timestamp to consistent format

Parameters:

  • timestamp (String, Time, Numeric)

    timestamp to normalize

Returns:

  • (Time)

    normalized timestamp



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/dbwatcher/storage/concerns/data_normalizer.rb', line 75

def normalize_timestamp(timestamp)
  return Time.at(0) unless timestamp

  case timestamp
  when String
    Time.parse(timestamp)
  when Time
    timestamp
  when Numeric
    Time.at(timestamp)
  else
    Time.at(0)
  end
rescue ArgumentError, TypeError
  Time.at(0)
end