Module: Familia::Features::Relationships::ModelClassMethods

Includes:
ScoreEncoding
Defined in:
lib/familia/features/relationships.rb

Constant Summary

Constants included from ScoreEncoding

ScoreEncoding::MAX_METADATA, ScoreEncoding::METADATA_PRECISION, ScoreEncoding::PERMISSION_CATEGORIES, ScoreEncoding::PERMISSION_FLAGS, ScoreEncoding::PERMISSION_ROLES

Instance Method Summary collapse

Methods included from ScoreEncoding

add_permissions, #add_permissions, categorize_scores, category?, category_score_range, current_score, #current_score, decode_permission_flags, decode_score, #decode_score, encode_score, #encode_score, filter_by_category, meets_category?, permission?, #permission?, permission_decode, #permission_decode, permission_encode, #permission_encode, permission_level_value, permission_range, #permission_range, permission_tier, remove_permissions, #remove_permissions, score_range, #score_range

Instance Method Details

#create_temp_key(base_name, ttl = 300) ⇒ Object

Class method wrapper for create_temp_key



157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/familia/features/relationships.rb', line 157

def create_temp_key(base_name, ttl = 300)
  timestamp = Familia.now.to_i
  random_suffix = SecureRandom.hex(3)
  temp_key = "temp:#{base_name}:#{timestamp}:#{random_suffix}"

  # UnsortedSet immediate expiry to ensure cleanup even if operation fails
  if respond_to?(:dbclient)
    dbclient.expire(temp_key, ttl)
  else
    Familia.dbclient.expire(temp_key, ttl)
  end

  temp_key
end

#identifier(field = nil) ⇒ Symbol

Define the identifier for this class (replaces identifier_field) This is a compatibility wrapper around the existing identifier_field method

Examples:

identifier :domain_id

Parameters:

  • field (Symbol) (defaults to: nil)

    The field to use as identifier

Returns:

  • (Symbol)

    The identifier field



116
117
118
119
120
# File 'lib/familia/features/relationships.rb', line 116

def identifier(field = nil)
  return identifier_field(field) if field

  identifier_field
end

#validate_relationships!Object

Validate relationship configurations

Raises:



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/familia/features/relationships.rb', line 124

def validate_relationships!
  errors = []

  # Check for method name collisions
  method_names = []

  if respond_to?(:participation_relationships)
    participation_relationships.each do |config|
      target_name = config[:target_class_name].downcase
      collection_name = config[:collection_name]

      method_names << "in_#{target_name}_#{collection_name}?"
      method_names << "add_to_#{target_name}_#{collection_name}"
      method_names << "remove_from_#{target_name}_#{collection_name}"
    end
  end

  # Check for duplicates
  duplicates = method_names.group_by(&:itself).select { |_, v| v.size > 1 }.keys
  errors << "Method name collisions detected: #{duplicates.join(', ')}" if duplicates.any?

  # Validate identifier field exists
  id_field = identifier
  unless instance_methods.include?(id_field) || method_defined?(id_field)
    errors << "Identifier field '#{id_field}' is not defined"
  end

  raise RelationshipError, "Relationship validation failed: #{errors.join('; ')}" if errors.any?

  true
end