Class: Familia::Horreum::ParentDefinition

Inherits:
Data
  • Object
show all
Defined in:
lib/familia/horreum/related_fields.rb

Overview

Each related field needs some details from the parent (Horreum model) in order to generate its dbkey. We use a parent proxy pattern to store only essential parent information instead of full object reference. We need only the model class and an optional unique identifier to generate the dbkey; when the identifier is nil, we treat this as a class-level relation (e.g. model_name:related_field_name); when the identifier is not nil, we treat this as an instance-level relation (model_name:identifier:related_field_name).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#identifierObject (readonly)

Returns the value of attribute identifier

Returns:

  • (Object)

    the current value of identifier



20
21
22
# File 'lib/familia/horreum/related_fields.rb', line 20

def identifier
  @identifier
end

#model_klassObject (readonly)

Returns the value of attribute model_klass

Returns:

  • (Object)

    the current value of model_klass



20
21
22
# File 'lib/familia/horreum/related_fields.rb', line 20

def model_klass
  @model_klass
end

Class Method Details

.from_parent(parent_instance) ⇒ Object

Factory method to create ParentDefinition from a parent instance



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/familia/horreum/related_fields.rb', line 22

def self.from_parent(parent_instance)
  case parent_instance
  when Class
    # Handle class-level relationships
    new(parent_instance, nil)
  else
    # Handle instance-level relationships
    identifier = parent_instance.respond_to?(:identifier) ? parent_instance.identifier : nil
    new(parent_instance.class, identifier)
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Allow comparison with the original parent instance



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/familia/horreum/related_fields.rb', line 54

def ==(other)
  case other
  when ParentDefinition
    model_klass == other.model_klass && identifier == other.identifier
  when Class
    model_klass == other && identifier.nil?
  else
    # Compare with instance: check class and identifier match
    other.is_a?(model_klass) && other.respond_to?(:identifier) && identifier == other.identifier
  end
end

#dbclient(uri = nil) ⇒ Object

Delegation methods for common operations needed by DataTypes



35
36
37
# File 'lib/familia/horreum/related_fields.rb', line 35

def dbclient(uri = nil)
  model_klass.dbclient(uri)
end

#dbkey(keystring = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/familia/horreum/related_fields.rb', line 43

def dbkey(keystring = nil)
  if identifier
    # Instance-level relation: model_name:identifier:keystring
    model_klass.dbkey(identifier, keystring)
  else
    # Class-level relation: model_name:keystring
    model_klass.dbkey(keystring, nil)
  end
end

#logical_databaseObject



39
40
41
# File 'lib/familia/horreum/related_fields.rb', line 39

def logical_database
  model_klass.logical_database
end