Class: Openphar::Exporters::Neo4j::PropertyMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/openphar/exporters/neo4j/property_mapper.rb

Overview

Maps model attributes to Neo4j-compatible property names

Neo4j doesn't allow dots in property names, so this mapper converts camelCase JSON-LD keys to snake_case Neo4j properties.

Uses the model registry to understand the structure of each model type.

Constant Summary collapse

PROPERTY_MAPPING =

Property name mapping: JSON-LD key => Neo4j property key Neo4j doesn't allow dots in property names, so we flatten

{
  '@id' => :id,
  '@type' => :type,
  'monographId' => :monograph_id,
  'prefLabel' => :pref_label,
  'altLabel' => :alt_label,
  'definition' => :definition,
  'publisher' => :publisher,
  'status' => :status,
  'belongsToEdition' => :edition_id,
  'effectiveDate' => :effective_date,
  'version' => :version,
  'referencesPreparation' => :prep_id,
  'referencesTCMProfile' => :tcm_id,
  'referencesAyurvedaProfile' => :ayurveda_id,
  'referencesWesternProfile' => :western_id,
  'referencesPlantSpecies' => :plant_id,
  'sameSubstanceAs' => :same_as,
  'hasEquivalentIn' => :equiv_in,
  'similarTo' => :similar_to,
  'testSpecification' => :test_specs,
  'storageContainer' => :storage_container,
  'storageConditions' => :storage_conds,
  'botanicalSource' => :botanical_source,
  'macroscopicDescription' => :macro_desc,
  'microscopicDescription' => :micro_desc,
  'molecularFormula' => :mol_formula,
  'molecularWeight' => :mol_weight,
  'casNumber' => :cas_number,
  'testName' => :test_name,
  'testType' => :test_type,
  'harmonizedMethod' => :harm_method,
  'testConditions' => :test_conds,
  'limitType' => :limit_type,
  'limitValue' => :limit_value,
  'lowerLimit' => :lower_limit,
  'upperLimit' => :upper_limit,
  'limitUnit' => :limit_unit,
  'appearance' => :appearance,
  'identification' => :identification,
  'purity' => :purity,
  'assay' => :assay,
  'editionNumber' => :edition_number,
  'publicationDate' => :publication_date,
  'monographCount' => :monograph_count,
  'officialLanguage' => :official_languages,
  'supplementNumber' => :supplement_number,
  # Ph.Int.-specific
  'systematicName' => :systematic_name,
  'inchi' => :inchi,
  'inchiKey' => :inchi_key,
  'smiles' => :smiles,
  'hasRadionuclide' => :radionuclide,
  'halfLife' => :half_life,
  'emissionType' => :emission_type,
  'radiochemicalPurity' => :radiochem_purity,
  'radionuclidicPurity' => :radionucl_purity,
  'photonFraction' => :photon_fraction,
  'solubility' => :solubility,
  'category' => :category,
  'chemicalName' => :chemical_name
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(registry = nil) ⇒ PropertyMapper

Returns a new instance of PropertyMapper.



78
79
80
# File 'lib/openphar/exporters/neo4j/property_mapper.rb', line 78

def initialize(registry = nil)
  @registry = registry || ModelRegistry.new
end

Instance Method Details

#convert_value(value) ⇒ String

Converts a value to Neo4j-compatible format

Parameters:

  • value (any)

    The value to convert

Returns:

  • (String)

    Neo4j-compatible value representation



109
110
111
112
113
114
115
116
117
118
# File 'lib/openphar/exporters/neo4j/property_mapper.rb', line 109

def convert_value(value)
  case value
  when Hash then value.to_json
  when Array then value.to_json
  when String then escape_string(value)
  when nil then 'null'
  when Numeric then value.to_s
  else value.to_s
  end
end

#map_properties(data) ⇒ Hash

Maps all properties from JSON-LD data to Neo4j format

Parameters:

  • data (Hash)

    The JSON-LD data as a Ruby hash

Returns:

  • (Hash)

    Mapped properties with Neo4j-compatible keys



94
95
96
97
98
99
100
101
102
103
# File 'lib/openphar/exporters/neo4j/property_mapper.rb', line 94

def map_properties(data)
  props = {}

  data.each do |key, value|
    neo4j_key = map_property(key)
    props[neo4j_key] = convert_value(value)
  end

  props
end

#map_property(jsonld_key) ⇒ Symbol

Maps a JSON-LD key to a Neo4j-compatible property key

Parameters:

  • jsonld_key (String)

    The key from JSON-LD data

Returns:

  • (Symbol)

    Neo4j-compatible property key



86
87
88
# File 'lib/openphar/exporters/neo4j/property_mapper.rb', line 86

def map_property(jsonld_key)
  PROPERTY_MAPPING[jsonld_key] || default_mapping(jsonld_key)
end

#property_count(data) ⇒ Integer

Returns the property count for analytics

Parameters:

  • data (Hash)

    The JSON-LD data

Returns:

  • (Integer)

    Number of properties



124
125
126
# File 'lib/openphar/exporters/neo4j/property_mapper.rb', line 124

def property_count(data)
  data.keys.size
end