Class: TranscriptViewer::ToolTemplateRegistry

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/transcript_viewer/tool_template_registry.rb

Instance Method Summary collapse

Constructor Details

#initialize(templates = nil) ⇒ ToolTemplateRegistry

Returns a new instance of ToolTemplateRegistry.



7
8
9
10
# File 'lib/transcript_viewer/tool_template_registry.rb', line 7

def initialize(templates = nil)
  @templates = {}
  merge!(templates) if templates
end

Instance Method Details

#each(&block) ⇒ Object



31
32
33
# File 'lib/transcript_viewer/tool_template_registry.rb', line 31

def each(&block)
  @templates.each(&block)
end

#merge(other) ⇒ Object



35
36
37
# File 'lib/transcript_viewer/tool_template_registry.rb', line 35

def merge(other)
  self.class.new(self).merge!(other)
end

#merge!(other) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/transcript_viewer/tool_template_registry.rb', line 39

def merge!(other)
  return self unless other

  registrations = if other.is_a?(self.class)
    other
  elsif other.respond_to?(:each_pair)
    other.each_pair
  else
    raise ArgumentError, "Tool templates must be a ToolTemplateRegistry or hash"
  end

  registrations.each { |tool_name, template_path| register(tool_name, template_path) }
  self
end

#register(tool_name, template_path) ⇒ Object

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
# File 'lib/transcript_viewer/tool_template_registry.rb', line 12

def register(tool_name, template_path)
  name = normalize_name(tool_name)
  path = File.expand_path(template_path.to_s)

  raise ArgumentError, "Tool template does not exist: #{path}" unless File.file?(path)

  @templates[name] = path
  self
end

#resolve(tool_name) ⇒ Object



27
28
29
# File 'lib/transcript_viewer/tool_template_registry.rb', line 27

def resolve(tool_name)
  @templates[tool_name.to_s]
end

#to_hObject



54
55
56
# File 'lib/transcript_viewer/tool_template_registry.rb', line 54

def to_h
  @templates.dup
end

#unregister(tool_name) ⇒ Object



22
23
24
25
# File 'lib/transcript_viewer/tool_template_registry.rb', line 22

def unregister(tool_name)
  @templates.delete(tool_name.to_s)
  self
end