Class: Openphar::Migrators::Chp::Classifier

Inherits:
Object
  • Object
show all
Defined in:
lib/openphar/migrators/chp/classifier.rb

Overview

Single dispatch point mapping a ChP API record's (book_id, directory_title) pair to the correct Openphar::Models::Chp::* subclass.

Routing is data-driven: SPECIFIC_RULES is an ordered list of {book_id, substring, klass_name} triples. Adding a new directory type = adding one line, not editing dispatch logic. Open/closed.

MECE invariant: every (book_id, directory_title) pair observed in the ChP 2025/2020 manifests classifies to exactly one subclass. spec/openphar/migrators/chp/classifier_spec.rb enforces this against every directory_title seen in the manifests.

Constant Summary collapse

SPECIFIC_RULES =
[
  { book_id: 1, substring: '药材和饮片',           klass: 'TcmCrudeDrug' },
  { book_id: 1, substring: '植物油脂和提取物',     klass: 'TcmExtract' },
  { book_id: 1, substring: '成方制剂',             klass: 'TcmFormulation' },
  { book_id: 2, substring: '第二部分',             klass: 'ChemicalPreparation' },
].freeze
VOLUME_DEFAULTS =
{
  1 => 'TcmCrudeDrug',
  2 => 'ChemicalSubstance',
  3 => 'Biologic',
  4 => 'GeneralChapter',
}.freeze
FINAL_FALLBACK =
'Monograph'.freeze

Class Method Summary collapse

Class Method Details

.all_typesArray<Class>

Enumerate every ChP type class. Used by specs to assert classification coverage.

Returns:

  • (Array<Class>)


60
61
62
63
64
65
66
# File 'lib/openphar/migrators/chp/classifier.rb', line 60

def all_types
  %i[
    TcmCrudeDrug TcmExtract TcmFormulation
    ChemicalSubstance ChemicalPreparation
    Biologic GeneralChapter
  ].map { |k| Openphar::Models::Chp.const_get(k) }
end

.classify(book_id:, directory_title: nil) ⇒ Class

Returns the subclass appropriate for the given record. Never raises — degrades gracefully to Chp::Monograph.

Parameters:

  • book_id (Integer)

    1..4

  • directory_title (String, nil) (defaults to: nil)

    e.g., "药材和饮片"

Returns:

  • (Class)

    a subclass of Openphar::Models::Chp::Monograph



43
44
45
46
# File 'lib/openphar/migrators/chp/classifier.rb', line 43

def classify(book_id:, directory_title: nil)
  klass_name = resolve(book_id: book_id, directory_title: directory_title)
  Openphar::Models::Chp.const_get(klass_name)
end

.wire_key_for(book_id:, directory_title: nil) ⇒ String

Returns the wire_key string for a record. Useful for routing without loading the class (e.g., for stats before instantiation).

Returns:

  • (String)


52
53
54
# File 'lib/openphar/migrators/chp/classifier.rb', line 52

def wire_key_for(book_id:, directory_title: nil)
  classify(book_id: book_id, directory_title: directory_title).wire_key
end