Class: Openphar::Models::BaseEntity

Inherits:
Lutaml::Model::Serializable
  • Object
show all
Defined in:
lib/openphar/models/base_entity.rb

Overview

Base entity class for all pharmacopoeia models.

Provides model-centric serialization methods where the model IS the schema. Each model knows how to serialize itself to:

  • Neo4j (to_neo4j, to_cypher)
  • JSON-LD (to_jsonld)

Subclasses should override:

  • neo4j_labels: Array of Neo4j labels
  • jsonld_type: The @type value for JSON-LD

Examples:

class MyMonograph < BaseEntity
  attribute :name, :string

  def self.neo4j_labels
    ['Monograph', 'MyType']
  end

  def self.jsonld_type
    'MyMonograph'
  end
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.jsonld_contextString

Returns the JSON-LD context for this entity.

Returns:

  • (String)

    Context URL



70
71
72
# File 'lib/openphar/models/base_entity.rb', line 70

def jsonld_context
  'https://www.openphar.org/ontology/context/pharmacopoeia.jsonld'
end

.jsonld_typeString

Returns the JSON-LD @type for this entity. Override in subclasses.

Returns:

  • (String)

    JSON-LD type



63
64
65
# File 'lib/openphar/models/base_entity.rb', line 63

def jsonld_type
  name&.split('::')&.last || 'Entity'
end

.neo4j_labelString

Returns the primary Neo4j label (first in the list).

Returns:

  • (String)

    Primary Neo4j label



48
49
50
# File 'lib/openphar/models/base_entity.rb', line 48

def neo4j_label
  neo4j_labels.first
end

.neo4j_labelsArray<String>

Returns Neo4j labels for this entity type. Override in subclasses to specify custom labels.

Returns:

  • (Array<String>)

    Neo4j node labels



41
42
43
# File 'lib/openphar/models/base_entity.rb', line 41

def neo4j_labels
  ['Entity']
end

.neo4j_labels_stringString

Returns Neo4j label string for Cypher (e.g., "Monograph:CrudeDrug").

Returns:

  • (String)

    Colon-separated labels



55
56
57
# File 'lib/openphar/models/base_entity.rb', line 55

def neo4j_labels_string
  neo4j_labels.join(':')
end

Instance Method Details

#to_cypher(use_merge: false, variable: 'n') ⇒ String

Generates a Cypher CREATE or MERGE statement for this entity.

Parameters:

  • use_merge (Boolean) (defaults to: false)

    Use MERGE instead of CREATE

  • variable (String) (defaults to: 'n')

    Cypher variable name (default: 'n')

Returns:

  • (String)

    Cypher statement



132
133
134
135
136
137
138
139
# File 'lib/openphar/models/base_entity.rb', line 132

def to_cypher(use_merge: false, variable: 'n')
  keyword = use_merge ? 'MERGE' : 'CREATE'
  node = to_neo4j_node
  labels = node[:labels].join(':')
  props = cypher_properties_string(node[:properties])

  "#{keyword} (#{variable}:#{labels} {#{props}})"
end

#to_cypher_match(variable: 'n') ⇒ String

Generates a Cypher MATCH statement for finding this entity.

Parameters:

  • variable (String) (defaults to: 'n')

    Cypher variable name

Returns:

  • (String)

    Cypher MATCH statement



145
146
147
148
# File 'lib/openphar/models/base_entity.rb', line 145

def to_cypher_match(variable: 'n')
  labels = self.class.neo4j_labels_string
  "MATCH (#{variable}:#{labels} {id: '#{id}'})"
end

#to_jsonldHash

Returns a JSON-LD representation of this entity.

Returns:

  • (Hash)

    JSON-LD data



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/openphar/models/base_entity.rb', line 106

def to_jsonld
  data = {
    '@id' => id,
    '@type' => self.class.jsonld_type,
    '@context' => self.class.jsonld_context
  }

  # Add all attributes except id (already included)
  self.class.attributes.each_key do |attr_name|
    next if attr_name == :id

    value = send(attr_name)
    next if value.nil?

    json_key = jsonld_property_name(attr_name)
    data[json_key] = value
  end

  data
end

#to_neo4j_nodeHash

Returns a hash representation for Neo4j node.

Returns:

  • (Hash)

    Node data with id, labels, and properties



78
79
80
81
82
83
84
# File 'lib/openphar/models/base_entity.rb', line 78

def to_neo4j_node
  {
    id: id,
    labels: self.class.neo4j_labels,
    properties: to_neo4j_properties
  }
end

#to_neo4j_propertiesHash

Converts model attributes to Neo4j-compatible properties.

Returns:

  • (Hash)

    Neo4j properties



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/openphar/models/base_entity.rb', line 89

def to_neo4j_properties
  properties = {}

  self.class.attributes.each_key do |attr_name|
    value = send(attr_name)
    next if value.nil?

    prop_name = neo4j_property_name(attr_name)
    properties[prop_name] = neo4j_property_value(value)
  end

  properties
end