Class: Pubid::IdentifierRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/pubid/identifier_registry.rb

Overview

Global registry of all identifier types across all flavors Provides searchable index for machine-readable lookups

Class Method Summary collapse

Class Method Details

.all_for_flavor(flavor) ⇒ Array<Class>

Get all identifiers for a flavor

Parameters:

  • flavor (String, Symbol)

    The flavor

Returns:

  • (Array<Class>)


53
54
55
# File 'lib/pubid/identifier_registry.rb', line 53

def all_for_flavor(flavor)
  flavor_index[flavor.to_s]&.values || []
end

.all_identifiersHash<Class, Metadata>

Get all registered identifiers

Returns:

  • (Hash<Class, Metadata>)


59
60
61
# File 'lib/pubid/identifier_registry.rb', line 59

def all_identifiers
  @all_identifiers ||= {}
end

.clear!Object

Clear all registrations (useful for testing)



131
132
133
134
135
136
# File 'lib/pubid/identifier_registry.rb', line 131

def clear!
  @all_identifiers = {}
  @flavor_index = nil
  @type_key_index = nil
  @abbr_index = nil
end

.find_by_abbr(flavor, abbr) ⇒ Array<Class>

Find identifier class by abbreviation

Parameters:

  • flavor (String, Symbol)

    The flavor

  • abbr (String)

    The abbreviation

Returns:

  • (Array<Class>)

    All matching classes



46
47
48
# File 'lib/pubid/identifier_registry.rb', line 46

def find_by_abbr(flavor, abbr)
  abbr_index[flavor.to_s]&.dig(abbr.to_s.downcase) || []
end

.find_by_type(flavor, type_key) ⇒ Class?

Find identifier class by flavor and type key

Parameters:

  • flavor (String, Symbol)

    The flavor

  • type_key (String, Symbol)

    The type key

Returns:

  • (Class, nil)


38
39
40
# File 'lib/pubid/identifier_registry.rb', line 38

def find_by_type(flavor, type_key)
  flavor_index[flavor.to_s]&.dig(type_key.to_s)
end

.metadata_for(identifier_class) ⇒ Metadata?

Get metadata for an identifier class

Parameters:

  • identifier_class (Class)

    The identifier class

Returns:

  • (Metadata, nil)


66
67
68
# File 'lib/pubid/identifier_registry.rb', line 66

def (identifier_class)
  all_identifiers[identifier_class]
end

.register(identifier_class, **metadata) ⇒ Object

Register an identifier class with its metadata

Parameters:

  • identifier_class (Class)

    The identifier class

  • metadata (Hash)

    Metadata attributes



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pubid/identifier_registry.rb', line 13

def register(identifier_class, **)
  # Extract class name from full class name
  identifier_class_name = identifier_class.name.to_s.split("::")&.last

  all_identifiers[identifier_class] = IdentifierMetadata::Metadata.new(
    flavor: .fetch(:flavor),
    identifier_class: identifier_class_name,
    **,
  )

  # Index by various keys for fast lookup
  index_by_flavor([:flavor], identifier_class)
  if [:type_key]
    index_by_type_key([:type_key],
                      identifier_class)
  end
  index_by_abbr([:abbr], identifier_class) if [:abbr]

  identifier_class
end

.search(**criteria) ⇒ Array<Hash>

Search identifiers by criteria

Parameters:

  • criteria (Hash)

    Search criteria

Options Hash (**criteria):

  • :flavor (String, Symbol)

    Filter by flavor

  • :type_key (String, Symbol)

    Filter by type key

  • :abbr (String)

    Filter by abbreviation

  • :title (String)

    Filter by title (partial match)

  • :base_class (Boolean)

    Filter by base class status

  • :supplement_class (Boolean)

    Filter by supplement class status

Returns:

  • (Array<Hash>)

    Array of Class, metadata: Metadata



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/pubid/identifier_registry.rb', line 79

def search(**criteria)
  results = []

  all_identifiers.each do |klass, |
    matches = true

    if criteria[:flavor]
      matches &= .flavor.to_s == criteria[:flavor].to_s
    end

    if criteria[:type_key]
      matches &= .type_key.to_s == criteria[:type_key].to_s
    end

    if criteria[:abbr]
      matches &= .abbr.map(&:to_s).map(&:downcase).include?(criteria[:abbr].to_s.downcase)
    end

    if criteria[:title]
      matches &= .title.to_s.downcase.include?(criteria[:title].to_s.downcase)
    end

    if criteria[:base_class]
      matches &= !.base_class.nil? == criteria[:base_class]
    end

    if criteria[:supplement_class]
      matches &= !.supplement_class.nil? == criteria[:supplement_class]
    end

    results << { class: klass, metadata:  } if matches
  end

  results
end

.summaryString

Generate a summary report

Returns:

  • (String)

    Human-readable summary



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/pubid/identifier_registry.rb', line 140

def summary
  flavors = all_identifiers.values.map(&:flavor).uniq.sort
  total = all_identifiers.size

  summary = "Identifier Registry Summary\n"
  summary += "#{'=' * 50}\n"
  summary += "Total identifiers: #{total}\n"
  summary += "Flavors: #{flavors.join(', ')}\n\n"

  flavors.each do |flavor|
    flavor_ids = all_identifiers.values.select do |m|
      m.flavor.to_s == flavor
    end
    summary += "#{flavor.upcase} (#{flavor_ids.size}):\n"
    flavor_ids.sort_by(&:type_key).each do ||
      summary += "  - #{.type_key}: #{.title}\n"
    end
  end

  summary
end

.to_json(*args) ⇒ String

Export registry to machine-readable format (JSON)

Returns:

  • (String)

    JSON representation



117
118
119
# File 'lib/pubid/identifier_registry.rb', line 117

def to_json(*args)
  all_identifiers.values.map(&:to_hash).to_json(*args)
end

.to_yamlString

Export registry to machine-readable format (YAML)

Returns:

  • (String)

    YAML representation



123
124
125
126
127
128
# File 'lib/pubid/identifier_registry.rb', line 123

def to_yaml
  require "json"
  hash = all_identifiers.values.map(&:to_h)
  # Convert to YAML format (basic implementation)
  hash.map(&:to_json).join("\n")
end