Class: Hiiro::RegistryEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/hiiro/registry.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_orderedObject



37
# File 'lib/hiiro/registry.rb', line 37

def self.all_ordered         = order(:resource_type, :name).all

.create_table!(db) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/hiiro/registry.rb', line 7

def self.create_table!(db)
  # Rename old table if it exists
  if db.table_exists?(:registry_entries) && !db.table_exists?(:registry)
    db.rename_table(:registry_entries, :registry)
  end
  
  db.create_table?(:registry) do
    primary_key :id
    String :resource_type, null: false   # e.g. "service", "queue", "worker"
    String :name,          null: false   # canonical identifier
    String :value                        # stored value (optional)
    String :short_name                   # alias / shorthand
    String :description
    String :meta_json                    # arbitrary JSON for extra fields
    String :created_at
    unique [:resource_type, :name]
    unique [:resource_type, :short_name], where: Sequel[short_name: nil].~
  end
end

.find_all_by_ref(ref, type: nil, substring: false) ⇒ Object

Find all entries matching ref (for showing ambiguous matches) Checks both name and short_name, returns unique entries



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/hiiro/registry.rb', line 67

def self.find_all_by_ref(ref, type: nil, substring: false)
  scope = type ? where(resource_type: type.to_s) : self
  
  # Check for exact matches first
  exact = scope.where(name: ref).all + scope.where(short_name: ref).all
  return exact.uniq(&:id) if exact.any?
  
  # Fall back to prefix/substring matching on name
  entries = scope.all
  matcher = Hiiro::Matcher.new(entries, :name)
  result = substring ? matcher.by_substring(ref) : matcher.by_prefix(ref)
  name_matches = result.matches.map(&:item)
  
  # Also match on short_name
  matcher_short = Hiiro::Matcher.new(entries.select(&:short_name), :short_name)
  result_short = substring ? matcher_short.by_substring(ref) : matcher_short.by_prefix(ref)
  short_matches = result_short.matches.map(&:item)
  
  (name_matches + short_matches).uniq(&:id)
end

.find_by_ref(ref, type: nil, substring: false) ⇒ Object

Resolve by exact name, short_name, or prefix within a type (or globally). Returns single entry or nil. Ambiguous prefix matches return nil.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/hiiro/registry.rb', line 42

def self.find_by_ref(ref, type: nil, substring: false)
  scope = type ? where(resource_type: type.to_s) : self
  
  # Try exact match first (name or short_name)
  exact = scope.where(name: ref).first || scope.where(short_name: ref).first
  return exact if exact
  
  # Fall back to prefix/substring matching
  entries = scope.all
  matcher = Hiiro::Matcher.new(entries, :name)
  result = substring ? matcher.by_substring(ref) : matcher.by_prefix(ref)
  
  # Only return if unambiguous (exactly one match)
  return result.first.item if result.one?
  
  # Also try matching against short_name
  matcher_short = Hiiro::Matcher.new(entries.select(&:short_name), :short_name)
  result_short = substring ? matcher_short.by_substring(ref) : matcher_short.by_prefix(ref)
  return result_short.first.item if result_short.one?
  
  nil
end

.fuzzy_lines(type: nil) ⇒ Object

Fuzzy-finder display lines: “type short name description”



89
90
91
92
# File 'lib/hiiro/registry.rb', line 89

def self.fuzzy_lines(type: nil)
  entries = type ? of_type(type) : all_ordered
  entries.map(&:fuzzy_line)
end

.known_typesObject



38
# File 'lib/hiiro/registry.rb', line 38

def self.known_types         = distinct.select_map(:resource_type).sort

.migrate!(db) ⇒ Object



27
28
29
30
31
32
# File 'lib/hiiro/registry.rb', line 27

def self.migrate!(db)
  # Add value column if missing (migration for existing DBs)
  unless db.schema(:registry).any? { |col, _| col == :value }
    db.alter_table(:registry) { add_column :value, String }
  end
end

.of_type(type) ⇒ Object

── Finders ──────────────────────────────────────────────────────────────



36
# File 'lib/hiiro/registry.rb', line 36

def self.of_type(type)       = where(resource_type: type.to_s).order(:name).all

Instance Method Details

#displayObject



114
115
116
117
118
119
120
121
# File 'lib/hiiro/registry.rb', line 114

def display
  lines = ["#{resource_type} / #{name}"]
  lines << "  value:  #{value}"            if value
  lines << "  alias:  #{short_name}"       if short_name
  lines << "  desc:   #{description}"      if description
  meta.each { |k, v| lines << "  #{k}: #{v}" } unless meta.empty?
  lines.join("\n")
end

#fuzzy_lineObject



107
108
109
110
111
112
# File 'lib/hiiro/registry.rb', line 107

def fuzzy_line
  parts = [resource_type.ljust(14), short_name&.ljust(16) || ' ' * 16, name]
  parts << " = #{value}" if value && !value.empty?
  parts << "  # #{description}" if description && !description.empty?
  parts.join('  ').strip
end

#metaObject

── Instance helpers ─────────────────────────────────────────────────────



96
97
98
99
100
101
# File 'lib/hiiro/registry.rb', line 96

def meta
  return {} unless meta_json
  Hiiro::DB::JSON.load(meta_json) || {}
rescue
  {}
end

#meta=(h) ⇒ Object



103
104
105
# File 'lib/hiiro/registry.rb', line 103

def meta=(h)
  self.meta_json = Hiiro::DB::JSON.dump(h)
end