Module: ConcernsOnRails::Models::Storable::ClassMethods

Includes:
Support::ColumnGuard
Defined in:
lib/concerns_on_rails/models/storable.rb

Instance Method Summary collapse

Methods included from Support::ColumnGuard

#ensure_columns!, #ensure_columns_on!, #schema_reachable?

Instance Method Details

#storable_by(column, keys = {}, prefix: nil, suffix: nil, **kw_keys) ⇒ Object

Declare typed accessors over column. Key specs may arrive as the positional keys Hash or as trailing keyword arguments (they are merged); the positional form is the escape hatch for keys literally named prefix/suffix. See the module docs.



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/concerns_on_rails/models/storable.rb', line 105

def storable_by(column, keys = {}, prefix: nil, suffix: nil, **kw_keys)
  column = column.to_sym
  ensure_columns!(LABEL, column)

  prepared = storable_merge_key_specs(keys, kw_keys).map do |key, raw_spec|
    key = key.to_sym
    [key, storable_normalize_spec(key, raw_spec, prefix, suffix)]
  end

  storable_install_keys(column, prepared)
end

#storable_native_hash_column?(column) ⇒ Boolean

Lazily decide — once per class/column, at first write when a DB connection exists — whether the column's attribute type stores a Hash natively (a :json column, or a host-app serialized column) so we can hand it the Hash; everything else gets a generated JSON String. Mutex-guarded (first writes can race on request threads), and a decision made without a reachable schema is never memoized — booting without a database must not pin a Hash-native column to the JSON-String path forever.

Returns:

  • (Boolean)


125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/concerns_on_rails/models/storable.rb', line 125

def storable_native_hash_column?(column)
  name = column.to_sym
  cache = @storable_native_hash_cache
  return cache[name] if cache&.key?(name)

  STORABLE_NATIVE_HASH_MUTEX.synchronize do
    cache = (@storable_native_hash_cache ||= {})
    return cache[name] if cache.key?(name)

    detected = storable_detect_native_hash(column)
    cache[name] = detected if schema_reachable?
    detected
  end
end