Class: Openphar::Models::BaseEntity
- Inherits:
-
Lutaml::Model::Serializable
- Object
- Lutaml::Model::Serializable
- Openphar::Models::BaseEntity
- 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
Direct Known Subclasses
Chp::Section, Monograph, PhInt::Reagent, PhInt::ReferenceSubstance, PhInt::TestMethod
Class Method Summary collapse
-
.jsonld_context ⇒ String
Returns the JSON-LD context for this entity.
-
.jsonld_type ⇒ String
Returns the JSON-LD @type for this entity.
-
.neo4j_label ⇒ String
Returns the primary Neo4j label (first in the list).
-
.neo4j_labels ⇒ Array<String>
Returns Neo4j labels for this entity type.
-
.neo4j_labels_string ⇒ String
Returns Neo4j label string for Cypher (e.g., "Monograph:CrudeDrug").
Instance Method Summary collapse
-
#to_cypher(use_merge: false, variable: 'n') ⇒ String
Generates a Cypher CREATE or MERGE statement for this entity.
-
#to_cypher_match(variable: 'n') ⇒ String
Generates a Cypher MATCH statement for finding this entity.
-
#to_jsonld ⇒ Hash
Returns a JSON-LD representation of this entity.
-
#to_neo4j_node ⇒ Hash
Returns a hash representation for Neo4j node.
-
#to_neo4j_properties ⇒ Hash
Converts model attributes to Neo4j-compatible properties.
Class Method Details
.jsonld_context ⇒ String
Returns the JSON-LD context for this entity.
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_type ⇒ String
Returns the JSON-LD @type for this entity. Override in subclasses.
63 64 65 |
# File 'lib/openphar/models/base_entity.rb', line 63 def jsonld_type name&.split('::')&.last || 'Entity' end |
.neo4j_label ⇒ String
Returns the primary Neo4j label (first in the list).
48 49 50 |
# File 'lib/openphar/models/base_entity.rb', line 48 def neo4j_label neo4j_labels.first end |
.neo4j_labels ⇒ Array<String>
Returns Neo4j labels for this entity type. Override in subclasses to specify custom labels.
41 42 43 |
# File 'lib/openphar/models/base_entity.rb', line 41 def neo4j_labels ['Entity'] end |
.neo4j_labels_string ⇒ String
Returns Neo4j label string for Cypher (e.g., "Monograph:CrudeDrug").
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.
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.
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_jsonld ⇒ Hash
Returns a JSON-LD representation of this entity.
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_node ⇒ Hash
Returns a hash representation for Neo4j node.
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_properties ⇒ Hash
Converts model attributes to Neo4j-compatible 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 |