Module: Dbwatcher::Storage::Concerns::DataNormalizer
- 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.
Instance Method Summary collapse
-
#extract_value(hash, key) ⇒ Object
Extract value by trying both string and symbol keys.
-
#normalize_change(change) ⇒ Hash
Normalize change data to use consistent symbol keys.
-
#normalize_hash_keys(hash) ⇒ Hash
Normalizes hash keys to symbols (Rails-compatible).
-
#normalize_operation(operation) ⇒ String
Normalize operation to uppercase string.
-
#normalize_record_id(record_id) ⇒ String
Normalize record ID to string.
-
#normalize_session_data(session) ⇒ Hash
Normalizes session input to hash with consistent symbol keys.
-
#normalize_table_name(table_name) ⇒ String
Normalize table name to string.
-
#normalize_timestamp(timestamp) ⇒ Time
Normalize timestamp to consistent format.
Instance Method Details
#extract_value(hash, key) ⇒ Object
Extract value by trying both string and symbol keys
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
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)
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
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
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
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
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
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 () return Time.at(0) unless case when String Time.parse() when Time when Numeric Time.at() else Time.at(0) end rescue ArgumentError, TypeError Time.at(0) end |